#!/bin/sh
#
# This will bring the network interface up.
#

# Splash.
#
echo "Executing /scripts/network_up..."

# Source Router type
#
. /app/scripts/eipr_type

if [ "$EIPR_TYPE" = "REALTEK" ]; then
	WANIF=eth0.2
	LANIF=eth0.1
else
	WANIF=eth0
	LANIF=eth1
fi

# Hostname
if [ -e /app/config/hn ]; then
	. /app/config/hn
	echo $EIPR_HN > /proc/sys/kernel/hostname
fi

# Domainname
if [ -e /app/config/dn ]; then
	. /app/config/dn
	echo $EIPR_DN > /proc/sys/kernel/domainname
fi


# Source the network definitions.
#
. /app/config/network_eth0.ini
. /app/config/network_eth1.ini

# Source MAC addresses
#
. /app/scripts/eth0.mac
if [ "$EIPR_TYPE" != "REALTEK" ]; then
. /app/scripts/eth1.mac
fi

# Read MAC from Serial Flash
macVar=$(getmac)
if [ "$macVar" != "FF:FF:FF:FF:FF:FF" ]; then
ETH0_MAC=$macVar
fi

# Setup the interfaces with MAC addresses
#
/sbin/ifconfig eth0 hw ether $ETH0_MAC
if [ "$EIPR_TYPE" != "REALTEK" ]; then
/sbin/ifconfig eth1 hw ether $ETH1_MAC
fi

# Bring up the local interface.
#
/sbin/ifconfig lo 127.0.0.1 up
/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo


# Setup VLAN intefaces on CPU for Realtek Chip
if [ "$EIPR_TYPE" = "REALTEK" ]; then
/sbin/ifconfig eth0 up
/sbin/vconfig add eth0 1
/sbin/vconfig add eth0 2

# For CPU port 5, add VLAN tags. 
/app/bin/miireg 6 22 0x873e
/app/bin/miireg 0 16 0x03fa # default 0x07fa, change bit 16.10 to 0
fi

#
# Setup eth1/eth0.1 (LAN) port. It will be fixed ip
#
echo "Manually initializing eth1/eth0.1/LANIF to $IPADDR_ETH1..."
/sbin/ifconfig $LANIF $IPADDR_ETH1 netmask $NETMASK_ETH1 up

##   *******  Wifi Stuff below

#
# If LAN has Wifi Enabed, setup
#

if [ "$EIPR_WIFI" = "YES" ]
then
	# load wifi driver
	/app/scripts/wifi.sh
	
	# set LAN interface to be bridge
	LANIF=br0	
	/sbin/ifconfig $LANIF $IPADDR_ETH1 netmask $NETMASK_ETH1 up
fi

#initial iptables rule that flush and set chain rules
/scripts/iptables_Init1

#
# If LAN CLients need DHCP server, start it
#
if [ "$BOOTPROTO_ETH1_CLIENT" = "dhcp" ]
then
	#copy the old leases file that was present before reboot
	if [ -e /app/config/udhcpd.leases ]; then
		mv /app/config/udhcpd.leases /tmp/udhcpd.leases
	else
		touch /tmp/udhcpd.leases
	fi

	echo "Starting DHCP Server on LAN Port..."
        udhcpd >/dev/null 2>&1

fi

#
# Delete resolv.conf files (both original and ppp)
#
rm /etc/ppp/resolv.conf
rm /etc/resolv.conf
#
# Setup eth0/eth0.2 (WAN) port. It will be either dhcp, fixed ip or ppp
#
if [ "$BOOTPROTO_ETH0" = "ppp" ]
then
	# Bring up the eth0/eth0.2/WAN interface for ppp
	/sbin/ifconfig $WANIF up
	/usr/bin/pon &
elif [ "$BOOTPROTO_ETH0" = "pptp" ]
then
	# Bring up the eth0/eth0.2/WAN interface for pptp
	/sbin/ifconfig $WANIF $IPADDR_ETH0 netmask $NETMASK_ETH0 up
	# Add a route for it.
	/sbin/route add default gw $GATEWAY_ETH0 $WANIF
	# Start PPTP
	/usr/bin/pon &
elif [ "$BOOTPROTO_ETH0" = "dhcp" ]
then
	# Start the dhcp daemon daemon.
	# This should try forever to start DHCP.
	# It will also create /etc/resolv.conf and add routes
	echo "Starting DHCP Client on WAN Port..."
	/sbin/ifconfig $WANIF mtu $MTU_ETH0
	if [ -e /app/config/hn ]; then
		udhcpc -i $WANIF -b -R -h $EIPR_HN -p /var/run/udhcpc.$WANIF -s /usr/share/udhcpc/modified.script >/dev/null 2>&1
	else
		udhcpc -i $WANIF -b -R -p /var/run/udhcpc.$WANIF -s /usr/share/udhcpc/modified.script >/dev/null 2>&1
	fi
else
	# Bring up the interface manually.
	echo "Manually initializing eth0/eth0.2/WAN to $IPADDR_ETH0..."
	/sbin/ifconfig $WANIF $IPADDR_ETH0 netmask $NETMASK_ETH0 mtu $MTU_ETH0 up
	# Create resolv.conf
	echo "nameserver $DNS_SERVER1_ETH0"  > /etc/resolv.conf
	echo "nameserver $DNS_SERVER2_ETH0" >> /etc/resolv.conf
	# Add a route for it.
	/sbin/route add default gw $GATEWAY_ETH0 $WANIF
fi

# if WAN port (eth0/eth0.2) is set for DHCP, then dhcp script starts DNS
# if WAN port (eth0/eth0.2) is set for PPPoE/PPTP, then PPPoE/PPTP starts DNS. 
# else we start it here for fixed ip.
if [ "$BOOTPROTO_ETH0" = "none" ];then
	/app/scripts/startdns &
fi

# And exit.
#
exit 0
