#!/bin/bash
#
# Start the network....
#
#VERSION=5
#CHANGES=fixed issues with vlan / dead dhcp

PATH=$PATH:/sbin/:/bin:/usr/sbin:/usr/bin

# find a working beroconf
if [ -x /usr/local/bin/beroconf ]; then
	BEROCONF=/usr/local/bin/beroconf
else
	BEROCONF=/usr/fallback/beroconf
fi

# find a working update-interfaces
if [ -x /usr/local/sbin/update-interfaces ]; then
	UPD_IFACES=/usr/local/sbin/update-interfaces
else
	UPD_IFACES=/usr/fallback/update-interfaces
fi

function add_routes() {

a[0]=0.0.0.0
a[1]=1.0.0.0
a[2]=3.0.0.0
a[3]=7.0.0.0
a[4]=15.0.0.0
a[5]=31.0.0.0
a[6]=63.0.0.0
a[7]=127.0.0.0
a[8]=255.0.0.0
a[9]=255.1.0.0
a[10]=255.3.0.0
a[11]=255.7.0.0
a[12]=255.15.0.0
a[13]=255.31.0.0
a[14]=255.63.0.0
a[15]=255.127.0.0
a[16]=255.255.0.0
a[17]=255.255.1.0
a[18]=255.255.3.0
a[19]=255.255.7.0
a[20]=255.255.15.0
a[21]=255.255.31.0
a[22]=255.255.63.0
a[23]=255.255.127.0
a[24]=255.255.255.0
a[25]=255.255.255.1
a[26]=255.255.255.3
a[27]=255.255.255.7
a[28]=255.255.255.15
a[29]=255.255.255.31
a[30]=255.255.255.63
a[31]=255.255.255.127
a[32]=255.255.255.255

routing=$(${BEROCONF} get root routing)

IFS=,
for i in $routing ; do
    net=$(echo $i | cut -d "/" -f1)
    netmask_bits=$(echo $i | cut -d "/" -f2)
    netmask=${a[$netmask_bits]}
    gateway=$(echo $i | cut -d "/" -f3)
    echo "Adding: route add -net $net netmask $netmask gw $gateway"
    /sbin/route add -net $net netmask $netmask gw $gateway
done

}


ncs_stop() {
	killall -9 netconfigserver
        killall -9 nc
}

ncs_start() {
	/sbin/start-stop-daemon -S -b -x /usr/sbin/netconfigserver
}

start() {
	lan_if="eth0"
	msp_if="eth1"

 	echo "Starting network..."
	ncs_stop

	# retrieve vlan-settings
	vlan_enabled=$(${BEROCONF} get root vlan-enable | grep -v failed)
	if [ ! -z "${vlan_enabled}" ]; then
		vlan_id=$(${BEROCONF} get root vlan-id | grep -v failed)
	fi

	# create vlan-interface
	if [ "${vlan_enabled}" = "1" ] && [ ! -z ${vlan_id} ]; then
		lan_if="${lan_if}.${vlan_id}"
	fi

	# run update-interfaces
	${UPD_IFACES}

	if grep dhcp /etc/network/interfaces > /dev/null; then
		/sbin/ifconfig eth0 mtu 1500
		/sbin/ifconfig ${lan_if} mtu 1500
	fi

	/sbin/ifup -a

	# check if ${lan_if} (eth0 or eth0.vlanid) is configured correctly
	# disable eth0 if vlan is active
	lan_line=$(/sbin/ifconfig ${lan_if} | grep "inet addr:")
	lan_addr=$(expr match "${lan_line}" ".*inet addr:\([0-9\.]*\)")
	if [ "${vlan_enabled}" = "1" ] && [ ! -z "${vlan_id}" ]; then
		/sbin/ifconfig eth0 0.0.0.0
		if [ -z "${lan_addr}" ]; then
			/sbin/ifconfig ${lan_if} 10.0.0.2 netmask 255.255.255.0 up
			/sbin/route add default ${lan_if}
		fi
	elif [ -z "${lan_addr}" ]; then
		/sbin/ifconfig eth0 10.0.0.2 netmask 255.255.255.0 up
		/sbin/route add default eth0
	fi

	ncs_start

        # setup eth1, if not configured yet
	if [ -z "$(/sbin/ifconfig ${msp_if} | grep 'inet addr')" ]; then
		lan_line=$(/sbin/ifconfig ${lan_if} | grep "inet addr:")
		lan_part=$(expr match "${lan_line}" ".*inet addr:\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\)\.[0-9]\{1,3\}")

		msp_net="192.168.173"
		if [ "${lan_part}" = "${msp_net}" ]; then
			msp_net="192.168.174"
		fi

		/sbin/ifconfig ${msp_if} ${msp_net}.1 netmask 255.255.255.0 up
	fi

	add_routes
}

stop() {
	echo "Stopping network..."
	ncs_stop
	/sbin/ifdown -a

	for iface in $(/sbin/ifconfig | grep "Ethernet" | grep "eth" | cut -d" " -f1); do
		/sbin/ifconfig ${iface} down
	done
}

case "${1}" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart|reload)
		stop
		start
		;;
	*)
		echo "Usage: $0 [start|stop|restart|reload]"
		exit 1
esac
