#! /bin/sh

change_pap_secrets() {
	cd /
	if [ -e /etc/ppp/pap-tmp.new.* ]; then
           busybox rm /etc/ppp/pap-tmp.new.*
	fi
	echo "$username	*	$password" >> /etc/ppp/pap-tmp.new.$$
	while read line ; do
	   for i in $line ; do
	      if [ "$i" != "$username" ] ; then
	         echo "$line" >> /etc/ppp/pap-tmp.new.$$
	      fi
	      break
	   done
	done < /etc/ppp/pap-secrets
	busybox rm /etc/ppp/pap-secrets
	busybox mv /etc/ppp/pap-tmp.new.$$ /etc/ppp/pap-secrets
	
	# testing --> if exit code equals 0 then print error message and exit.
	[ "$?" -ne 0 ] && {
	   echo "[Fail] can not Modify pap-secrets file"
	   echo "check whether there is enough space on disk or not"
	   exit 1
	}
}

change_chap_secrets() {
	cd /
	if [ -e /etc/ppp/pap-tmp.new.* ]; then
           busybox rm /etc/ppp/pap-tmp.new.*
	fi
	echo "$username	*	$password" >> /etc/ppp/pap-tmp.new.$$
	while read line ; do
	   for i in $line ; do
	      if [ "$i" != "$username" ] ; then
	         echo "$line" >> /etc/ppp/pap-tmp.new.$$
	      fi
	      break
	   done
	done < /etc/ppp/chap-secrets
	busybox rm /etc/ppp/chap-secrets
	busybox mv /etc/ppp/pap-tmp.new.$$ /etc/ppp/chap-secrets
	
	# testing --> if exit code doesn't equal 0 then print error message and exit.
	[ "$?" -ne 0 ] && {
	   echo " "
	   echo "[Fail] can not modify chap-secrets"
	   echo "Please check whether there's enough space on disk or not"
	   exit 1
	}
}
change_ppp_options() {
	cd /
	if [ -e /etc/ppp/opts.new.* ]; then
           busybox rm /etc/ppp/opts.new.*
	fi
	while read line ; do
	   for i in $line ; do
              if [ "$i" != "user" -a "$i" != "rp_pppoe_service" ] ; then
                 echo "$line" >> /etc/ppp/opts.new.$$
              fi
	      break
	   done
	done < /etc/ppp/options
	
	echo "user $username" >> /etc/ppp/opts.new.$$
	busybox rm /etc/ppp/options
	busybox mv /etc/ppp/opts.new.$$ /etc/ppp/options
	
	# testing --> if exit code doesn't equal 0 then print error message and exit
	[ "$?" -ne 0 ] && {
	   echo " "
	   echo "[Failed] can not modify options file"
	   echo "Please check whether there's enough space on disk or not"
	   exit 1
	}
}


case "$1" in
	start)
		server=$(cat /etc/xl2tpd/xl2tpd.conf|grep 'lns ='|cut -d '=' -f 2)
		if [ "$server" = "" ]
		then
		   echo "[Failed] Please configure the arguments first"
		   echo "	$0 config name password server [debug]"
		   exit 1 
		fi
		
		if [ -s /var/run/xl2tpd.pid ] ; then
		   if [ -s /var/run/ppp0.pid ] ; then
		      server_ip=$(route -n|grep UGH|cut -d ' ' -f 1)
		      localgw=$(route -n|grep UGH|cut -d ' ' -f 3-4)
		      route del -host $server_ip
		      route del -net 0.0.0.0
		      route add -net 0.0.0.0 gw $localgw 
		   fi
		   
		   PID=`cat /var/run/xl2tpd.pid`
		   echo "PID file /var/run/xl2tpd.pid exists. Will try to kill process #$PID"
		   kill $PID
		   sleep 1
		fi
		
	 	echo "starting xl2tpd daemon ..."
		/mnt/system/l2tp/xl2tpd -C /var/run/l2tp-control
		ret=$?
		if [ $ret -ne 0 ]
		then
			# xl2td can not find config file /etc/xl2tpd/xl2tpd.conf
			# or can not create control file /var/run/l2tp-control
			# Is your disk out of space?
			
			echo "[Failed] xl2tpd daemon exit code : $ret"
			echo "Can not start the xl2tpd daemon."
			echo "Plese configure the arguments using:"
			echo "$0 config name secret server"
			exit 1
		else
			echo "xl2tpd daemon is running..."
		fi
	 	
	 	sleep 1
		
		echo "connecting to l2tp server ..."
		echo 'c ESCENE' > /var/run/l2tp-control
	 	
	 	for i in 1 2 3 4 5
	 	do
	  	   echo -n ..
	  	   sleep 1
	  	   if [ "$(ifconfig|grep ppp0)" != "" ]
	  	   then
	  	     break
	  	   fi
	 	done
	 	
	 	   defaultgw="$(ifconfig|grep P-t-P|cut -d ':' -f 3|cut -d ' ' -f 1)"
		   if [ "$defaultgw" != "" ] 
		   then 
		      read A oldgw C << EOF
		   	$(IFS=' '; echo "`route -n|grep ^0.0.0.0`")
EOF
		      route add -host $server gw $oldgw dev eth0
		      route del -net 0.0.0.0
		      route add -net 0.0.0.0 gw $defaultgw dev ppp0
		      echo [OK]
		   else
		      echo "[Failed] Can not Set up L2TP tunnel"
		   fi
		
		;;
	stop)
		echo -n "Stopping xl2tpd... "
        
        	if [ -s /var/run/xl2tpd.pid ] 
        	then
        	   if [ -s /var/run/ppp0.pid ]
        	   then
        	      server=$(cat /etc/xl2tpd/xl2tpd.conf|grep 'lns ='|cut -d '=' -f 2)
		      localgw=$(route -n|grep 'UGH'|cut -d ' ' -f 3-4)
		      route del -host $server
		      route del -net 0.0.0.0
		      echo "localgw :$localgw"
		      route add -net 0.0.0.0 gw $localgw
        	   fi
        	   
		   PID=`cat /var/run/xl2tpd.pid`
		   echo "PID file /var/run/xl2tpd.pid exists. Will try to kill process #$PID"
		   kill $PID
		   sleep 2
		fi
		echo "[Done]"
		;;
	status)
		echo "Checking for xl2tpd... "
		if [ -s /var/run/xl2tpd.pid ] ; then
		   PID=`cat /var/run/xl2tpd.pid`
		   echo "xl2tpd is running with PID $PID"
		   if [ -s /var/run/ppp0.pid ]; then
		      PID=`cat /var/run/ppp0.pid`
		      echo "    PPP link is established, on interface ppp0"
		   else
		      echo "    PPP link is NOT established "
		   fi 
		else
		   echo "xl2tpd is not running"
		fi
		;;
	config)
		if [ "$#" -lt 4 -o "$#" -gt 5 ]
		then
		   echo "Please enter the username, secret and server"
		   echo "	$0 config name password server [debug]"
		   exit 1
		fi
		echo -n "Changing configuration for xl2tpd... "
		   username=$2
		   password=$3
		   server=$4  
		   
		   change_ppp_options   
		   change_pap_secrets 
		   change_chap_secrets
		   
		   if [ ! -d /etc/xl2tpd/ ]
		   then
		      mkdir /etc/xl2tpd/ 
		   fi
		   
		   busybox cp /mnt/system/l2tp/xl2tpd.conf.example /etc/xl2tpd/xl2tpd.conf
		   
		   # testing --> if exit code doesn't equal 0 then print error message and exit
		   [ "$?" -ne 0 ] && {
		      echo " "
		      echo "[Fail] can not create xl2tpd config file (xl2tpd.conf) "
		      echo "Please check whether there's enough space on disk or not"
		      exit 1
		   }
		   
		   if [ "$5" = "debug" ]
		   then
		      # start syslogd to write daemon logs to /var/log/message
		      [ "$(ps -ax|grep syslogd|grep -v grep)" = "" ] && syslogd
		      echo 'ppp debug = yes' >> /etc/xl2tpd/xl2tpd.conf 
		      echo 'Debugging : see debug message in /var/log/message'
           	   fi
           	   
		   echo "name = $username" >> /etc/xl2tpd/xl2tpd.conf
		   ret1=$?
		   echo "lns = $server" >> /etc/xl2tpd/xl2tpd.conf
		   ret2=$?
		   
		   #testing --> either ret1 or ret2 doesn't equal 0, print error message and exit
		   [ "$ret1" -ne 0 ] && {
		      echo " "
		      echo "[Fail] could not append USERNAME or SERVER to xl2tpd.conf"
		      exit 1
		   }
		   
		     
		   echo "[Done]"
		;;
	*)
		echo "Usage:"
	 	echo Step 1 -- configure
		echo "	$0 config name password server [debug]"
		echo Step 2 -- start, stop xl2tpd, or check its status
		echo "	$0 start|stop|status"
		exit 1
		;;
esac
