#!/bin/sh

echo "PPP connection established" > /dev/console
echo "RUN: .$0 '$1' '$2' '$3' '$4' '$5' '$6' '$7'" > /dev/console

. /usr/sbin/functions.sh

IP="$4"

equal()
{
    case "$1" in
	"$2") return 0 ;;
        *) return 255 ;;
    esac
}

nequal()
{
    case "$1" in
	"$2") return 255 ;;
        *) return 0 ;;
    esac
}

dns.add $DNS1 $DNS2

IFACE=$1
DEFGW=$5
IPPARAM=$6

def_gw_enable="no"
chan=""
def_gw_apply="no"

usb_modem_mode=`grep "USB_MODEM_MODE" /tmp/net_variables | cut -d"=" -f2`
preferred_chan=`grep "PREFERRED_CHANNEL" /tmp/net_variables | cut -d"=" -f2`
ping_servers=`grep "PING_SERVERS" /tmp/net_variables | cut -d"=" -f2`
INTERNET_SERVICE_IFACE=`grep "INTERNET_SERVICE_IFACE"  /tmp/net_variables | cut -d"=" -f2`

SERVICE="$7"
if [ "$SERVICE" == "" ] ; then
	network_mode=`buci get network common_settings network_mode`
	if [ "$network_mode" == "advanced" ] ; then
		SERVICE="Internet"
	else
		SERVICE="Basic"
	fi
fi
RT_TABLE="`grep "$SERVICE""_RT_TABLE" /tmp/net_variables | cut -d"=" -f2`"
echo "$SERVICE RT_TABLE: $RT_TABLE" > /dev/console

PPTP_DHCP_IFACE=`grep "PPTP_DHCP_IFACE" /tmp/net_variables | cut -d"=" -f2`
PPTP_SERVER_IP=`grep "PPTP_SERVER_IP" /tmp/net_variables | cut -d"=" -f2`

ipnm=`convert_ipmask $IP 255.255.255.0`

/usr/sbin/ip rule del from $ipnm
/usr/sbin/ip rule add from $ipnm table $RT_TABLE
/usr/sbin/ip rule del iif $IFACE
/usr/sbin/ip rule add iif $IFACE table $RT_TABLE
/usr/sbin/ip route del $IP table main
/usr/sbin/ip route del $IP table $RT_TABLE
#/usr/sbin/ip route del $DEFGW table main
#/usr/sbin/ip route del $DEFGW table $RT_TABLE
/usr/sbin/ip route add $IP dev $IFACE src $IP scope link table $RT_TABLE
/usr/sbin/ip route add $DEFGW dev $IFACE src $IP table $RT_TABLE
#for pptpip in `getip $PPTP_SERVER_IP --full -4` ; do
#	ip route add $pptpip dev $PPTP_DHCP_IFACE table $RT_TABLE
#done

add_def_gw="/usr/sbin/ip route add default via $DEFGW dev $IFACE table $RT_TABLE"
del_def_gw="/usr/sbin/ip route del default table $RT_TABLE"
IS_JUMBO=`buci get network common_settings jumbo_mode`
PPP_IFACE="`grep "$SERVICE""_PPP_IFACE" /tmp/net_variables | cut -d"=" -f2`"
if [ "$PPP_IFACE" == "$IFACE" ] ; then
    echo "$SERVICE PPP interface: $PPP_IFACE" > /dev/console
else
    echo "$Service interface is not $PPP_IFACE" > /dev/console
fi
if [ "$IPPARAM" == "pppoe" ] ; then

    chan="wired"
    DEFGW_IFACE="`grep "GETDEFGW_IFACE_RT""$RT_TABLE" /tmp/net_variables | cut -d"=" -f2`"
    if [ "$DEFGW_IFACE" == "$IFACE" ] ; then
    	PPPOE_GETDEF_GW="enable"
    else
    	PPPOE_GETDEF_GW="disable"
    fi
    if [ "$PPPOE_GETDEF_GW" == "1" ] || [ "$PPPOE_GETDEF_GW" == "enable" ] ; then
		def_gw_enable="yes"
	if [ "$usb_modem_mode" != "reserve" ] ; then
	    def_gw_apply="yes"
	fi
    fi
	if [ "$IS_JUMBO" == "1" ] ; then
		MTU=`grep "mtu" /etc/ppp/options_$SERVICE | cut -d" " -f2`
		if [ "$MTU" -le "3556" ] ; then
			ifconfig $IFACE mtu $MTU
		fi
	fi
    is_ipv6=`grep "PPPOE_DUAL_STACK_$SERVICE=" /tmp/net_variables | cut -d"=" -f2`
    echo "is_ipv6 = $is_ipv6"
    if [ "$is_ipv6" == "yes" ]; then
    	echo "dual-stack enabled"
    	wan_mode=`grep "PPPOE_DUAL_STACK_WAN_$SERVICE=" /tmp/net_variables | cut -d"=" -f2`
    	if [ "$wan_mode" != "stateful" ]; then
    		echo 1 > /proc/sys/net/ipv6/conf/ppp0/autoconf
    		echo 2 > /proc/sys/net/ipv6/conf/ppp0/accept_ra
    		if [ "$wan_mode" == "stateless" ]; then
    			/bin/rdnssd &
    		fi
    	fi
    	if [ "$wan_mode" == "stateless" ]; then
    		sleep 2
    	fi 
    	echo 2 > /proc/sys/net/ipv6/conf/ppp0/accept_ra
    	if [ -f "/etc/dibbler/client-ppp0.conf" ]; then 
    		echo "run dhcpv6-client"
    		dibbler-client run -i ppp0 &
    	fi
    fi
   
elif [ "$IPPARAM" == "pptp" ] ; then

    chan="wired"

    DEFGW_IFACE="`grep "GETDEFGW_IFACE_RT""$RT_TABLE" /tmp/net_variables | cut -d"=" -f2`"
    if [ "$DEFGW_IFACE" == "$IFACE" ] ; then
    	PPTP_GETDEF_GW="enable"
    else
    	PPTP_SERVER_IP_GETDEF_GW="disable"
    fi

    if [ "$PPTP_GETDEF_GW" == "1" ] || [ "$PPTP_GETDEF_GW" == "enable" ] ; then
	def_gw_enable="yes"
	if [ "$usb_modem_mode" != "reserve" ] ; then
	    def_gw_apply="yes"
	fi
    fi
	if [ "$IS_JUMBO" == "1" ] ; then
		is_l2tp=`ps | grep xl2tpd | grep -v "grep xl2tpd"`
		if [ "$is_l2tp" != "" ] ; then
			file="options.xl2tpd"
		else
			file="options.pptp"
		fi
		MTU=`grep "mtu" /etc/ppp/$file | cut -d" " -f2`
		if [ "$MTU" -le "3556" ] ; then
			ifconfig $IFACE mtu $MTU
		fi
	fi
elif [ "$IPPARAM" == "3G" ] ; then

    chan="wireless"

    #echo $PPID > /tmp/pid/pppd.3G.pid

    /sbin/ledctl green

    USB_MODEM_GETDEF_GW=`grep "USB_MODEM_GETDEF_GW" /tmp/net_variables | cut -d"=" -f2`

    if [ "$USB_MODEM_GETDEF_GW" == "1" ] || [ "$USB_MODEM_GETDEF_GW" == "enable" ] ; then
	def_gw_enable="yes"
	if [ "$usb_modem_mode" != "reserve" ] ; then
	    def_gw_apply="yes"
	fi
    fi

fi

#####

enable_check_for_reserve="no"

if [ "$chan" == "wired" ] && [ "$INTERNET_SERVICE_IFACE" == "ppp0" ] ; then
	enable_check_for_reserve="yes"
fi

if [ "$chan" == "wireless" ] ; then
	enable_check_for_reserve="yes"
fi

echo "Script is running for Internet service or 3G: $enable_check_for_reserve" > /dev/console

if [ "$usb_modem_mode" == "reserve" ] && [ "$enable_check_for_reserve" == "yes" ] ; then
  if [ "$def_gw_enable" == "yes" ] && [ "$DEFGW" != "" ] ; then
    if [ "$preferred_chan" == "$chan" ] ; then
	dns_routes=""
	dns=${DNS1}" "${DNS2}
	[ -n "$dns" ] && {
		for j in $dns ; do
			/usr/sbin/interface_monitor check auto $j
			if [ "$?" != "0" ] && [ "`/bin/check_route $j`" == "" ] ; then
				echo "DNS-server $j is unreachable, adding route to it." > /dev/console
				/usr/sbin/ip route add $j via $DEFGW src $IP table $RT_TABLE
				dns_routes=${dns_routes}" "${j}
			else
				echo "There is an access to DNS-server $j. No adding route." > /dev/console
			fi
		done
	}
         
	echo "Addig routes to ping servers..." > /dev/console 
	echo "Ping-servers: $ping_servers" > /dev/console       
	for i in $ping_servers ; do
	    is_access=0
	    /usr/sbin/interface_monitor check $IFACE $i && {
	    is_access=1
	    }
	    if [ "$is_access" == 0 ] ; then 
	    	echo "Ping-server $i is not accessible, adding route to it." > /dev/console
	    	#cmd="/usr/sbin/ip route del $i table $RT_TABLE"
	    	#echo $cmd > /dev/console
	    	#$cmd;$cmd;$cmd
	    	cmd="/usr/sbin/ip route add $i via $DEFGW src $IP table $RT_TABLE"
	    	echo $cmd > /dev/console
	    	$cmd
	    else
	    	echo "Ping-server $i is accessible, no adding route to it." > /dev/console
	    fi
	done
        
	echo "Checking access to ping servers..." > /dev/console

	/usr/sbin/interface_monitor check $IFACE $ping_servers && { 
	    echo "Connection OK" > /dev/console
	    def_gw_apply="yes"
	}
	
	echo "Deleting routes to DNS-servers..." > /dev/console
	[ -n "$dns_routes" ] && {
		for j in $dns_routes ; do
			/usr/sbin/ip route del $j gw $DEFGW table $RT_TABLE
		done
	}
    else
        def_gw_apply="yes"
    fi
  fi
fi

#####

if [ "$usb_modem_mode" == "reserve" ] && [ "$enable_check_for_reserve" == "yes" ] ; then
  if [ "$def_gw_enable" == "yes" ] && [ "$DEFGW" != "" ] ; then
    if [ "$preferred_chan" == "$chan" ] ; then
	echo "$DEFGW" > /tmp/gw
    fi
  fi
fi

#####
if [ "$def_gw_apply" == "yes" ] ; then
    $del_def_gw;$del_def_gw;$del_def_gw
    if [ "$RT_TABLE" != "internet" ] && [ "$RT_TABLE" != "254" ] ; then
	/usr/sbin/ip route add $DEFGW dev $IFACE table internet
    fi
    echo "Add default GW $DEFGW" > /dev/console
    $add_def_gw
fi

#####


INTERNET_SERVICE_IFACE=`grep "INTERNET_SERVICE_IFACE" /tmp/net_variables | cut -d"=" -f2`

echo "INTERNET_SERVICE_IFACE=$INTERNET_SERVICE_IFACE" > /dev/console

if [ "$IFACE" == "$INTERNET_SERVICE_IFACE" ] || [ "$IFACE" == "ppp1" ] ; then 
    echo "Service 'Internet'" > /dev/console
    /sbin/ledctl green
fi
echo "$0 done." > /dev/console

exit 0
