#!/bin/sh

echo "$$" > /var/run/ip-up

. /etc/sysconfig/rc.conf
PIDFILE=/var/run/ppp-ppp_client.pid
LOGGER="/bin/logger -t $0"

$LOGGER "Start"

# save parameters

INTERFACE_NAME=$1
TTY_DEVICE=$2
SPEED=$3
LOCAL_IP_ADDRESS=$4
REMOTE_IP_ADDRESS=$5
IPPARAM=$6

LASTVALIDADDR=/var/state/last_valid_ipaddress.ppp

# The management / firewall needs this file
echo "$IPLOCAL" > /tmp/ppp-connection
echo "TRY_COUNT=0" > /tmp/ppp-tryagain

# set default route - but remember already set one
echo $(/bin/ip route show | grep default) > /tmp/ppp-rembered-default-route
/bin/ip route del default
$LOGGER "setting default route: /bin/ip route add default via $IPREMOTE dev $INTERFACE_NAME"
/bin/ip route add default via $IPREMOTE dev $INTERFACE_NAME

# source in the features
if [ -e "/etc/image_features" ] ; then
    . /etc/image_features
fi

if [ -e "/tmp/featurestatus.conf" ] ; then
    . /tmp/featurestatus.conf
fi

# Patch Nameserver relevant settings
if [ -n "$DNS1" ] ; then
	echo "nameserver $DNS1" > /var/run/dynforward.conf.ppp
	if [ -n "$DNS2" ] ; then
		echo "nameserver $DNS2" >> /var/run/dynforward.conf.ppp
	fi
fi

if [ -f /var/run/keepalive.pid ] ; then
	PID=$(/bin/cat /var/run/keepalive.pid)
	#PID=`/bin/cat /var/run/keepalive.pid`
	/bin/kill $PID > /dev/null 2>&1
	/bin/rm -rf /var/run/keepalive.pid
fi

if [ "$PPP_KEEPALIVE" = true ] ; then
	/bin/setsid /bin/ping -i 60 $DNS1 > /dev/null 2>&1 &
	echo "$!" > /var/run/keepalive.pid
fi

ipDownCheck ()
{
	if [ -r /var/run/ip-down.pid ] ; then
		$LOGGER ip down is running. exiting now...
		/bin/rm -f /var/run/ip-up
		exit 1;
	fi
}

#
# configure firewalling / iptables
#

ipDownCheck
if [ "$FEATURE_FIREWALL" = "1" ] ; then
    $LOGGER "Configuring firewall:"
    if [ "$START_FIREWALL" != "true" ] ; then
		$LOGGER "  skipped"
    else
		/bin/fw_ci --add changeip --ip $LOCAL_IP_ADDRESS
    fi
fi

echo 1 > /proc/sys/net/ipv4/conf/$INTERFACE_NAME/hidden

if [ "$ARP_ANTIDOTE" = "true" ] ; then
	echo 1 > /proc/sys/net/ipv4/neigh/$INTERFACE_NAME/arp_antidote
fi

#
# add routes via dynamic interface
#

/bin/addroute -b -s allWAN > /dev/null 2>&1
/bin/addroute -b -p allWAN > /dev/null 2>&1


#
# configure named
#

ipDownCheck
$LOGGER "Restarting named:"
if [ "$START_NAMED" != "true" ] ; then
	$LOGGER "  skipped"
else
	/etc/init.d/named restart && $LOGGER "  done" || $LOGGER "  failed"
fi

#
# configure ntp
#

ipDownCheck
$LOGGER "Starting ntp:"
if [  "$START_NTP" != "true" ] ; then
	$LOGGER "  skipped"
else
	/etc/init.d/ntp start && $LOGGER "  done" || $LOGGER "  failed"
fi

#
#	Starting PPTP/L2TP Daemon
#

if [ "$SEC_START_VPN" = "true" ] && [ "$FEATURE_VPN" = "1" ] ; then
    ipDownCheck
    $LOGGER "Starting PPTP/L2TP Daemon:"
    if [ "$START_VPND" = "false" ]
    then
	    $LOGGER "  skipped"
    else
	    /etc/init.d/vpnd restart && $LOGGER "  done" || $LOGGER "  failed"
    fi
fi

#
#	Starting IPSec Daemon
#

if [ "$SEC_START_VPN" = "true" ] && [ "$FEATURE_VPN" = "1" ] ; then
    ipDownCheck
    $LOGGER "Starting IPSec Daemon:"
    if [ "$START_IPSECD" = "false" ]
    then
	    $LOGGER "  skipped"
    else
	    /etc/init.d/ipsecd restart && $LOGGER "  done" || $LOGGER "  failed"
    fi
fi

#
# configure ipsec
#

if [ "$SEC_START_VPN" = "true" ] && [ "$FEATURE_VPN" = "1" ] ; then
    ipDownCheck
    $LOGGER "Starting ipsec:"
    if [ "$START_IPSEC" != "true" ] ; then
	$LOGGER "  skipped"
	/bin/changepsk
    else
	/etc/init.d/ipsec restart && $LOGGER "  done" || $LOGGER "  failed"
    fi
fi

#
# configure dyndns
#

ipDownCheck
$LOGGER "Starting dyndns:"
if [ "$START_DYNDNS" != "true" ] ; then
	echo "skipped"
else
	if [ -f $LASTVALIDADDR ] ; then
		. $LASTVALIDADDR
	fi
	if [ "$LAST_IP_ADDRESS" != "$IPLOCAL" ] ; then
		/etc/init.d/dyndnsd start $INTERFACE_NAME $IPLOCAL > /dev/null  && echo "done" || echo "failed"
	fi
fi

echo "LAST_IP_ADDRESS=$IPLOCAL" > $LASTVALIDADDR

# toggle debug mode
if [ -r "$PIDFILE" ] ; then
	# that dump pppd removes the pid file if connection goes down. so ip-down has no
    # pid file to toggle debug mode.
    # for that case - create copy of pid file
	/bin/cp -f $PIDFILE "$PIDFILE."last
	#PID=`/bin/cat $PIDFILE.last`
	PID=$(/bin/cat $PIDFILE.last)

	# get current debug state
	. /tmp/ppp-debug

	if [ "$DEBUG" = "true" ] ; then
		# switch off debug mode
		/bin/kill -10 $PID > /dev/null 2>&1

		# remember new state
		echo "DEBUG=false" > /tmp/ppp-debug
	fi
fi

/bin/raise_event -a PPP -n "link up" -t "PPP connection established. New IP address: $IPLOCAL" > /dev/null 2>&1

#ipDownCheck
#/bin/notifier -mount $INTERFACE_NAME $TTY_DEVICE
#/bin/notifier $INTERFACE_NAME up &

/bin/rm -f /var/run/ip-up

$LOGGER "End"

exit 0

