#!/bin/bash

yum -y install wget
wget http://sourceforge.net/projects/poptop/files/pptpd/pptpd-1.3.4/pptpd-1.3.4.tar.gz/
dir=/usr/src/local
IP=`ifconfig | awk '{print $2}' | grep  'addr:' | cut -d ':' -f2 | grep -v '127.0.0.1' | sed '/^$/d'`
if [ -d "$dir" ];then
tar zxvf pptpd-1.3.4.tar.gz -C  $dir                               ##The path make sure have pptpd tar##
else
mkdir -p $dir
tar zxvf pptpd-1.3.4.tar.gz -C $dir
fi
cd $dir/pptpd-1.3.4/
echo "#####pptpd configure-- make-- make install####"
yum -y install gcc
./configure  --prefix=/usr/local/pptpd  --enable-bcrelay  --with-libwrap && make && make install && echo "====ok====="
mkdir -p /usr/local/pptpd/etc/ppp
##copy pptpd configuration file##
cp $dir/pptpd-1.3.4/samples/pptpd.conf   /usr/local/pptpd/etc/ 
cp $dir/pptpd-1.3.4/samples/options.pptpd /usr/local/pptpd/etc
cp $dir/pptpd-1.3.4/samples/chap-secrets /usr/local/pptpd/etc/ppp
CONF=/usr/local/pptpd/etc/pptpd.conf
##Modify the pptpd.conf file
sed -i '/option/s/\/etc\/ppp/\/usr\/local\/pptpd\/etc/g' $CONF    ##this sed command "/etc/ppp"---to---> "/usr/local/pptpd/etc"
sed -i 's/#debug/debug/g' $CONF                                   ##Remove the debug annotation
sed -i '$a stimeout 30\nremoteip 192.168.10.100-120\n' $CONF      ##(the same as) echo "stimeout 30\nremoteip 192.168.10.100-120\n" >> $CONF
echo "localip  $IP" >> $CONF
##Modify the options.pptpd file
echo "debug" >> /usr/local/pptpd/etc/options.pptpd 
sed -i '$a ms-dns 8.8.8.8' /usr/local/pptpd/etc/options.pptpd
##Set the account(loginuser) and password
cat >> /usr/local/pptpd/etc/ppp/chap-secrets << EOF
admin       pptpd     admin123         192.168.10.100
tom         pptpd     tom123                *
EOF
##Open the routing forwarding##
sed -i '/net.ipv4.ip_forward/s/0/1/g' /etc/sysctl.conf
sysctl -p
##
/usr/local/pptpd/sbin/pptpd -c /usr/local/pptpd/etc/pptpd.conf -o /usr/local/pptpd/etc/options.pptpd