#!/bin/bash

PIDFILE=/var/run/if-up.pid
LOGGER="/bin/logger -t $0"

. /etc/sysconfig/rc.conf
. /etc/sysconfig/network.conf
. /var/run/interfaces

# PHYS_WAN will be modified.. but we need it later..
ORI_PHYS_WAN=$PHYS_WAN

# wanmac.cgi
#	PPP_MTU=<value>
#	PPP_DIAL=manual, always
#	PPP_KEEPALIVE=true, false
#	PPP_NAME=[string]
#	PPP_IPASSIGN=fixed, dynamic
#	PPP_FIXEDIP=[string]
#   PPP_RESTART_AT=true, false
#   PPP_RESTART_HOUR=value
#   PPP_RESTART_MINUTE=value

# check pid file
while [ -r "$PIDFILE" ] ; do
	/bin/sleep 2
done

echo "$$" > $PIDFILE

if [ "$PPP_TYPE" = "pppoa" ] ; then
	. /etc/sysconfig/atm.conf
	PLUGIN="plugin /etc/ppp-client/plugins/pppoatm.so $PPPOA_ENCAPS $PPPOA_VCC"
	# PPPOA does not use an ethernet interface
	PHYS_WAN=""
else
	PLUGIN="plugin /etc/ppp-client/plugins/rp-pppoe.so"
fi

# set ppp options

if [ "$PPP_IPASSIGN" = "fixed" ] ; then
	ASSIGN="$PPP_FIXEDIP: ipcp-accept-remote ipcp-addr-insist"
else
	ASSIGN="noipdefault ipcp-accept-remote ipcp-accept-local"
fi

if [ "x$PPP_NAME" != "x" ] ; then
	NAME="name $PPP_NAME"
else
	NAME=""
fi

if [ "x$PPP_MTU" != "x" ] ; then
	MTU="mtu $PPP_MTU mru $PPP_MTU"
else
	MTU=""
fi

if [ "x$LCP_ECHO_FAILURES" = "x" ] ; then
	LCP_ECHO_FAILURES="5"
fi

if [ "$START_PPTP_UPLINK" = "true" ] ; then
	PPTP="pty \"/bin/pptp $PPTP_SERVER_IP --nolaunchpppd\" +mschap-v2 $PPTP_ENCRYPT -mschap noauth"
	PLUGIN=""
	ASSIGN=""
	PHYS_WAN=""
else
	PPTP=""
fi

# clear the interface
if [ "x$PHYS_WAN" != "x" ] ; then
	/bin/ip address flush dev $PHYS_WAN > /dev/null 2>&1
fi

# start pppd using the /etc/ppp/options file - specify only additional options

CMDLINE="/bin/setsid /bin/ppp-client $PPTP $PLUGIN $PHYS_WAN debug $NAME ktune $ASSIGN $MTU lcp-echo-failure $LCP_ECHO_FAILURES $PPP_ADD_OPTIONS"

$LOGGER "Command line: $CMDLINE"

# Enable dynamic  socket  address rewriting on interface address change. This is
# useful for dialup interface with changing IP addresses.
echo "1" > /proc/sys/net/ipv4/ip_dynaddr

# if stop mark exists.. erase it..
/bin/rm -f /tmp/stop-adsl

TRY_COUNT=0

while [ true ] ; do

	while [ -r /var/run/ip-down ] ; do
		$LOGGER "ip-down still running - delayed"
		/bin/sleep 5
	done

	if [ -e /tmp/stop-adsl ] ; then
		$LOGGER Found stop-adsl mark - exiting...
		rm -f $PIDFILE
		rm -f /tmp/ppp-debug
		exit 0;
	fi

	if [ -r /tmp/ppp-tryagain ] ; then
		. /tmp/ppp-tryagain
		#TRY_COUNT=$(/bin/expr $TRY_COUNT + 1)
		TRY_COUNT=$(($TRY_COUNT+1))
	else
		TRY_COUNT=1
	fi
	echo "TRY_COUNT=$TRY_COUNT" > /tmp/ppp-tryagain

	# remember if debugging is switched ON or OFF
	echo "DEBUG=true" > /tmp/ppp-debug

	if [ "$PPP_RESTART_AT" = "true" ] ; then
		/bin/ppptimer $PPP_RESTART_HOUR $PPP_RESTART_MINUTE
	fi

	# add static route in case of pptp where pptp server is in different net
	if [ "$START_PPTP_UPLINK" = "true" ] ; then
		/bin/addr -i $ORI_PHYS_WAN -c $PPTP_SERVER_IP/32
		RC=$?
		if [ "$RC" = "0" ] ; then
			DEF_GATEWAY=$(/bin/ip route show | /bin/grep default | /bin/cut -f 3 -d ' ')
			$LOGGER "pptp mode. adding special route: /bin/ip route add $PPTP_SERVER_IP/32 via $DEF_GATEWAY"
			/bin/ip route add $PPTP_SERVER_IP/32 via $DEF_GATEWAY
		fi
	fi

	eval $CMDLINE > /dev/null 2>&1 &
	wait

	if [ -f /var/run/ppptimer.pid ] ; then
		/bin/kill $(/bin/cat /var/run/ppptimer.pid)
	fi

	$LOGGER Connection lost

	if [ "x$DEF_GATEWAY" != "x" ] ; then
		$LOGGER "pptp mode. removing special route: /bin/ip route del $PPTP_SERVER_IP/32 via $DEF_GATEWAY"
		/bin/ip route del $PPTP_SERVER_IP/32 via $DEF_GATEWAY
	fi

	if [ -e /tmp/stop-adsl ] ; then
		$LOGGER Found stop-adsl mark - exiting...
		/bin/rm -f /tmp/stop-adsl
		rm -f $PIDFILE
		rm -f /tmp/ppp-debug
		exit 0;
	else
		$LOGGER Attempting re-connection after 30 seconds.
		/bin/sleep 30
	fi
done
