#!/bin/sh

# When the ppp link comes up, this script is called with the following
# parameters
#       $1      the interface name used by pppd (e.g. ppp3)
#       $2      the tty device name
#       $3      the tty device speed
#       $4      the local IP address for the interface
#       $5      the remote IP address
#       $6      the parameter specified by the 'ipparam' option to pppd
#

# Source the network definitions.
#
. /app/config/network_eth0.ini

# write ip address to /tmp/pppfile
ppp_file=/tmp/pppfile
ppp_old_file=/tmp/ppp_oldfile
bbmd_cfg_file=/app/config/bbmd.cfg
bbmd_tmp_cfg_file=/tmp/bbmd.cfg
# local ip address for ppp0 interface, passed in $4 by pppd
echo $4 > $ppp_file

#line with ip address and subnet mask from ifconfig command
ip_string=$(echo $(ifconfig ppp0 | grep "inet addr:"))

# get subnet mask
mask=$(echo $ip_string | cut -d: -f4)
echo $mask >> $ppp_file

if [ "$BOOTPROTO_ETH0" = "ppp" ] || [ "$BOOTPROTO_ETH0" = "pptp" ]; then 
	if [ ! -e /tmp/configRtrDone ]
	then # first time setup
		/app/bin/config-router
		touch /tmp/configRtrDone
		cp $ppp_file $ppp_old_file
		#If BBMD and Firewall Enabled
		if [ "$EIPR_BBMD" = "YES" ] && [ "$EIPR_FW" = "YES" ]; then
			#update BBMD config file with new WAN side IP
			grep -v publicip $bbmd_cfg_file > $bbmd_tmp_cfg_file
			echo publicip=$4 >> $bbmd_tmp_cfg_file
			mv $bbmd_tmp_cfg_file $bbmd_cfg_file
			/app/bin/bbmd & # start BBMD application
		fi
	else # NOT first time setup
		#check if IP address changed, then reinit iptables and configure router again
		read line < $ppp_old_file
		if [ "$line" != "$4" ]
		then
			/scripts/iptables_reinit
			/app/bin/config-router
			cp $ppp_file $ppp_old_file
			#If BBMD and Firewall Enabled
			if [ "$EIPR_BBMD" = "YES" ] && [ "$EIPR_FW" = "YES" ]; then
				#update BBMD config file with new WAN side IP
				grep -v publicip $bbmd_cfg_file > $bbmd_tmp_cfg_file
				echo publicip=$4 >> $bbmd_tmp_cfg_file
				mv $bbmd_tmp_cfg_file $bbmd_cfg_file
				touch /tmp/restart-bbmd # restart bbmd application
			fi
			
		fi
	fi
fi

# Setup routes for PPTP
if [ "$BOOTPROTO_ETH0" = "pptp" ]
then
	# add route to the PPTP server through WANIF
	/sbin/route add $PPTP_SERVER_IP eth0.2

	# delete default route through WANIF
	/sbin/route del default eth0.2

	# add default route through ppp0/tunnel interface
	/sbin/route add default ppp0

fi

num=1
while [ $num -eq 1 ]; do
	sleep 2
	if [ -e /etc/ppp/resolv.conf ]
	then
		num=2
		# create symbolic link
		ln -sf /etc/ppp/resolv.conf /etc/resolv.conf
	
		# extract nameserver values from resolv file
		file=/etc/ppp/resolv.conf
		tempfile=/tmp/ppp_resolvfile
		
		# and save lines with nameserver to the tempfile
		exec $(cat $file | grep nameserver > $tempfile)
	
		#read tempfile line by line, and copy the dns values to ppp_file
		while read line;do
		dns=$(echo $(echo $line | cut -d" " -f2))
		echo $dns >> $ppp_file
		done < $tempfile
	
		# delete tempfile
		rm $tempfile
		
		# start maradns
		/app/scripts/startdns &
	
	fi
done
