#!/bin/sh

echo "PPP connection established" > /dev/console
echo "RUN: .$0 '$1' '$2' '$3' '$4' '$5' '$6'" > /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`
VPN_RT_TABLE=`grep "VPN_RT_TABLE"  /tmp/net_variables | cut -d"=" -f2`
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 $VPN_RT_TABLE
/usr/sbin/ip rule del iif $IFACE
/usr/sbin/ip rule add iif $IFACE table $VPN_RT_TABLE
/usr/sbin/ip route del $IP table main
/usr/sbin/ip route del $IP table $VPN_RT_TABLE
#/usr/sbin/ip route del $DEFGW table main
#/usr/sbin/ip route del $DEFGW table $VPN_RT_TABLE
/usr/sbin/ip route add $IP dev $IFACE src $IP scope link table $VPN_RT_TABLE
/usr/sbin/ip route add $DEFGW dev $IFACE src $IP table $VPN_RT_TABLE
#for pptpip in `getip $PPTP_SERVER_IP --full -4` ; do
#	ip route add $pptpip dev $PPTP_DHCP_IFACE table $VPN_RT_TABLE
#done

add_def_gw="/usr/sbin/ip route add default via $DEFGW dev $IFACE table $VPN_RT_TABLE"
del_def_gw="/usr/sbin/ip route del default table $VPN_RT_TABLE"
IS_JUMBO=`buci get network common_settings jumbo_mode`

if [ "$IPPARAM" == "pppoe" ] ; then

    chan="wired"

    PPPOE_GETDEF_GW=`grep "PPPOE_GETDEF_GW_RT$VPN_RT_TABLE" /tmp/net_variables | cut -d"=" -f2`

    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 | cut -d" " -f2`
		if [ "$MTU" -le "3556" ] ; then
			ifconfig ppp0 mtu $MTU
		fi
	fi
elif [ "$IPPARAM" == "pptp" ] ; then

    chan="wired"

    PPTP_GETDEF_GW=`grep "PPTP_GETDEF_GW_RT$VPN_RT_TABLE" /tmp/net_variables | cut -d"=" -f2`

    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 ppp0 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 $VPN_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 $VPN_RT_TABLE"
	    	#echo $cmd > /dev/console
	    	#$cmd;$cmd;$cmd
	    	cmd="/usr/sbin/ip route add $i via $DEFGW src $IP table $VPN_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 $VPN_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 [ "$VPN_RT_TABLE" != "internet" ] && [ "$VPN_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 [ "$INTERNET_SERVICE_IFACE" == "ppp0" ] ; then 
    echo "Service 'Internet'" > /dev/console
    /sbin/ledctl green
fi
echo "$0 done." > /dev/console

exit 0
