#! /bin/sh

#
# /etc/rc.d/init.d/S20network - Start/Stop the network interface(s).
#

# Comment out the following exit line to enable this script.
# Before doing so, you need to edit the network address information below.
#exit 0

#######################################
##### network address information #####
#######################################
# The netmask is in vlsm format (variable length subnet masking)
# Example: NETMASK = 25 is the same as 255.255.255.128

if [ ! -x /sbin/ifconfig ]; then
	echo "$0: Missing /sbin/ifconfig"
	exit 1
fi

case "$1" in

    start)
	echo "Setting up link for loopback: "
	ifconfig lo 127.0.0.1 up
	route add 127.0.0.0 dev lo
	
	
	echo -n "Enable internal interface: "
	ifconfig eth1 `getcfg lanip` netmask `getcfg lannm`
	/sbin/getcfg dhcps_enable
        if [ "$?" != "1" ]; then
                echo "dhcp server disable"
	else
                echo "dhcp server enable"
		/sbin/dhcpd
        fi
	
	
	echo -n "Setting WAN interface: "
	
	#get wan interface
	/sbin/getcfg wanif
	case "$?" in
	"1") 	echo "Using PPPoE as external interface"
		ifconfig eth0 down
		getcfg updateresolv
		getcfg gen_pppoe_conf
		/usr/sbin/adsl-stop
		/usr/sbin/adsl-start	
		echo -n >> /etc/resolv.conf
		if [ -f /etc/ppp/resolv.conf ];then
			cat /etc/ppp/resolv.conf >> /etc/resolv.conf
		fi
		;;
	"2")	echo "Using PPTP Client as external interface"
		getcfg updateresolv
		getcfg gen_pptpc_conf
		ifconfig eth0 `getcfg get_pptpc_myip` up
		/sbin/pptp `getcfg get_pptpc_serverip` file /etc/ppp/options.pptpc
		;;	
	"3") 	echo "Using dhcpcd to get external ip address"
		getcfg cleanresolv
		getcfg updateresolv
		ifconfig eth0 up
		/sbin/dhcpcd
		/sbin/getcfg bpa_enable
        	if [ "$?" != "1" ]; then
                	echo "bpa disable"
	        else
        	        echo "bpa enable"
			/sbin/getcfg gen_bpaconf
			/bin/mkdir -p /var/lock/subsys
			touch /var/lock/subsys/bpalogin
			/sbin/bpalogin -c /etc/bpalogin.conf
        	fi
		;;
	"4") 	echo "Using fixed WAN ip"
		getcfg updateresolv
		ifconfig eth0 down
		ifconfig eth0 `getcfg wanip` netmask `getcfg wannm` up
		echo "add default gw"
		route add default gw `getcfg wangw` metric 1 dev eth0
		;;
	esac

	# set routing rules
	if [ -f /sbin/iptables ]; then
		echo "enabling forwarding, dynamic address"
		echo "1" > /proc/sys/net/ipv4/ip_forward
		echo "1" > /proc/sys/net/ipv4/ip_dynaddr
		echo "clearing routing rules"
		/sbin/iptables -P INPUT ACCEPT
		/sbin/iptables -F INPUT
		/sbin/iptables -P OUTPUT ACCEPT
		/sbin/iptables -F OUTPUT
		/sbin/iptables -P FORWARD ACCEPT
		/sbin/iptables -F FORWARD
		/sbin/iptables -t nat -F
	fi

	getcfg gen_mnat_conf
	if [ -f /sbin/multinat.sh ]; then
		echo "Enable Multiple NAT"
		/sbin/multinat.sh
	fi

	
	
	echo "set hostname, /etc/hosts"
	hostname `getcfg hostname`
	getcfg sethostip
	
	stty erase ^h
	sguard&
	#echo "update time"
	#date 010101012002
	#telnetd

	;;

    stop)
    	echo "shutdown network interface"
    	
	/usr/sbin/adsl-stop
	ifconfig eth0 down
	ifconfig eth1 down
	kill -USR1 `pidof dhcpcd`
	;;
    restart)
	$0 stop
	sleep 1
	$0 start
	;;

    *)
	echo "Usage: $0 (start|stop|restart)"
	exit 1
	;;

esac

exit 0

