#! /bin/sh
#
######################################
#    Bring up Network Interfac       #
######################################
cd /etc/sss_script/network
. /etc/sss_script/network/ifcfg-egiga0 > /dev/null 2>&1

[ -f /etc/hostname ] && HOSTNAME="$(cat /etc/hostname)"
[ -z "$HOSTNAME" ] && HOSTNAME="$(hostname)"


if [ "$1" == '' ]
	then
	/etc/init.d/servers stop
	
	#Clear route
	ifconfig egiga0 inet 0
fi
 
BOOTPROTO=$BOOTPROTO

if [ "$BOOTPROTO" == 'dhcp' ]
	then 
		echo "send host-name \"$HOSTNAME\";" > /etc/dhclient
		ifconfig $DEVICE mtu $MTU up
		start-stop-daemon -K -x /sbin/dhclient		
		cp /var/lib/dhcp/dhclient.leases /usr/local/samba/var/.
		/sbin/dhclient -sf /sbin/dhclient-script -lf /usr/local/samba/var/dhclient.leases -cf /etc/dhclient.conf egiga0
		
	else

		DEVICE=$DEVICE
		echo "DEVICE:"$DEVICE

		IPADDR=$IPADDR
		echo "IP:"$IPADDR

		NETMASK=$NETMASK
		echo "NETMASK:"$NETMASK

		GATEWAY=$GATEWAY
		echo "GATEWAY:"$GATEWAY
	
		MTU=$MTU
		echo "MTU :"$MTU
		
		start-stop-daemon -K -x /sbin/dhclient		

		ifconfig $DEVICE $IPADDR netmask $NETMASK mtu $MTU up

		route add default gw $GATEWAY

fi

sleep 3

ipaddr=`ifconfig egiga0 |grep 'inet addr'| awk '{print $2}' | awk -F':' '{print $2}'`
echo "127.0.0.1 localhost localhost">/etc/hosts
echo "$ipaddr $HOSTNAME">>/etc/hosts 
. /etc/sss_script/event/lib_io && SSS_SetIpAddr

if [ "$1" == '' ]
	then
/etc/init.d/servers start
fi