#!/bin/sh
#
# start/stop networking daemons.
#
# chkconfig: S 40 0
#
#-----detect USB ESM----------
echo "USB ESM detection: it will takes at most 15 (2) seconds to detect..." 

my_test="1"
mfgchk
if [ "$?" = "1" ]; then 	
	echo "mfg already done"
else			
	#----mfg not done----
	insmod /lib/modules/omap_udc.ko
	insmod /lib/modules/g_serial.ko
	sleep 1
	sd 5
	my_test="$?"
       if [ "$my_test" = "1" ]; then
             rmmod /lib/modules/g_serial.ko
             rmmod /lib/modules/omap_udc.ko
       fi

fi

if [ "$my_test" = "9" ]; then
    echo "ESM MFG mode: Insert ESM Wifi Module..."
    echo 9 > /tmp/sdESMMODE
else
    if [ "$my_test" = "0" ]; then
	echo "ESM Perf mode: Insert Normal Wifi Module..."
    	echo 1 > /tmp/sdESMMODE
    	insmod /lib/modules/gspi_io.ko
    	insmod /lib/modules/wlan.ko
    else
        echo "Normal Mode: Insert Normal Wifi Module..."
        echo 0 > /tmp/sdESMMODE
        insmod /lib/modules/omap_udc.ko
        insmod /lib/modules/g_serial.ko
        insmod /lib/modules/gspi_io.ko
        insmod /lib/modules/wlan.ko
    fi
fi
#-----end of detect USB ESM----------

if ! [ -x /sbin/ifup ]; then
    exit 0
fi

if [ -e /etc/network/spoof-protect ]; then
    . /etc/network/spoof-protect
fi

spoofprotect_rp_filter () {
    # This is the best method: turn on Source Address Verification and get
    # spoof protection on all current and future interfaces.
    
    if [ -e /proc/sys/net/ipv4/conf/all/rp_filter ]; then
        for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
            echo 1 > $f
        done
        return 0
    else
        return 1
    fi
}

spoofprotect () {
    echo -n "Setting up IP spoofing protection: "
    if spoofprotect_rp_filter; then
        echo "rp_filter."
    else
        echo "FAILED"
    fi
}

ip_forward () {
    if [ -e /proc/sys/net/ipv4/ip_forward ]; then
        echo -n "Enabling packet forwarding: "
        echo 1 > /proc/sys/net/ipv4/ip_forward
        echo "done."
    fi
}

syncookies () {
    if [ -e /proc/sys/net/ipv4/tcp_syncookies ]; then
        echo -n "Enabling TCP/IP SYN cookies: "
        echo 1 > /proc/sys/net/ipv4/tcp_syncookies
        echo "done."
    fi
}

disable_tcp_ecn () {
    if [ -e /proc/sys/net/ipv4/tcp_ecn ]; then
        echo -n "Disable TCP/IP Explicit Congestion Notification: "
        echo 0 > /proc/sys/net/ipv4/tcp_ecn
        echo "done."
    fi
}

doopt () {
    optname=$1
    default=$2
    opt=`grep "^$optname=" /etc/network/options`
    if [ -z "$opt" ]; then
        opt="$optname=$default"
    fi
    optval=${opt#$optname=}
    if [ "$optval" = "yes" ]; then
        eval $optname
    fi
}

case "$1" in
    start)
#	doopt spoofprotect yes
#        doopt syncookies no
#        doopt ip_forward no
#        doopt disable_tcp_ecn yes

        echo -n "Configuring network interfaces: "
        /sbin/ifup -a
	echo "done."
	;;
    stop)
        if sed -n 's/^[^ ]* \([^ ]*\) \([^ ]*\) .*$/\1 \2/p' /proc/mounts | 
          grep -q "^/ nfs$"
        then
            echo "NOT deconfiguring network interfaces: / is an NFS mount"
        elif sed -n 's/^[^ ]* \([^ ]*\) \([^ ]*\) .*$/\1 \2/p' /proc/mounts |  
          grep -q "^/ smbfs$"
        then
            echo "NOT deconfiguring network interfaces: / is an SMB mount"
	elif sed -n 's/^[^ ]* \([^ ]*\) \([^ ]*\) .*$/\2/p' /proc/mounts | 
          grep -qE '^(nfs|smbfs)$'
        then
            echo "NOT deconfiguring network interfaces: NFS/SMB shares still mounted."
        else
            echo -n "Deconfiguring network interfaces: "
            /sbin/ifdown -a
	    echo "done."
        fi
	;;
    reload)
	;;
    force-reload)
	$0 restart
	;;
    restart)
        echo -n "Reconfiguring network interfaces: "
        /sbin/ifdown -a
        /sbin/ifup -a
	echo "done."
	;;
    *)
	echo "Usage: /etc/init.d/networking {start|stop|reload|restart}"
	exit 1
	;;
esac

exit 0

