#!/bin/bash
#
# VERSION=3
# CHANGES="export uuid and appfs-version"

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/bin/X11:/usr/local/bin:/usr/local/sbin:/usr/fallback

IFACE=eth0
DEBUG=no
BEROCONF=/usr/fallback/beroconf

#
# debug helper
#
if [ "$1" == "-d" ]; then
	DEBUG=yes
fi

function debug () {
	if [ "$DEBUG" == "yes" ]; then
		echo "$1"
	fi
}

#
# system parsing
#
function get_hw () {
	t=$(ifconfig $IFACE | grep $IFACE)
	echo $t | cut -d " " -f 5
}

function get_ip () {
	t=$(ifconfig $IFACE | grep inet)
	echo $t | cut -d " " -f 2 | cut -d ":" -f 2
}

function get_nm () {
	t=$(ifconfig $IFACE | grep inet)
	echo $t | cut -d " " -f 4 | cut -d ":" -f 2
}

function get_mtu () {
	ifconfig $IFACE | sed -n 's/^.*MTU:\([^ ]*\).*$/\1/p'
}

function get_ns () {
	t=$(grep nameserver /etc/resolv.conf)
	echo $t | cut -d " " -f 2
}

function get_gw () {
	t=$(route -n | grep $IFACE | grep UG)
	echo $t | cut -d " " -f 2
}

function get_dhcp () {
	if grep "^iface $IFACE inet dhcp$" /etc/network/interfaces > /dev/null ; then
		echo 1;
	fi

	echo 0;
}

function get_serial () {
	expr match "$(cat /proc/cmdline)" ".*carduuid=\([0-9\-]*\)"
}

function get_appfs_version () {
	expr match "$(cat /usr/local/FILENAME)" "appfs-\(.*\).tar.gz"
}

#
# Packetizing
#
function send_netpacket () {
	echo $1 | nc -q0 -ub 255.255.255.255 65002
}

function get_netpacket () {
	t=$(echo | nc -q0 -lup 65001)
	echo $t
}

#
# System config
#
function change_network () {

	unset ip nm gw ns dhcp
	t=$(echo $1 | cut -d " " -f 2 | sed -n "s/^[^a-z]*\(.*;\)[^;]*$/\1/p")
	eval $t
	hw=$(get_hw)

	if ! netconfigverify "$ip" "$nm" "$gw" > /dev/null 2>&1
	then
		debug "verification of ip=$ip nm=$nm gw=$gw failed"
		return
	fi

	if [ -z "$mtu" ] ; then mtu=1500 ; fi

	if [ $mtu -lt 500 -o $mtu -gt 1500 ]
	then
		debug "verification of mtu=$mtu failed"
		return
	fi

	debug "sending: ok hw=$hw"
	send_netpacket "ok hw=$hw"
	debug "sent"

    # advanced boolean logic :)
    if [ ! -z "$ip" -o ! -z "$dhcp" ] ; then
		if [ "$dhcp" == "1" ] ; then
			${BEROCONF} set root lan-dhcp yes
			${BEROCONF} delete root lan-ipaddr
			${BEROCONF} delete root lan-netmask
			${BEROCONF} delete root lan-gateway
		else
			/usr/bin/killall -9 dhcpc-watchdog
			/usr/bin/killall -9 udhcpc
			${BEROCONF} delete root lan-dhcp
			${BEROCONF} set root lan-ipaddr $ip
			${BEROCONF} set root lan-netmask $nm
			${BEROCONF} set root lan-gateway $gw
		fi

		if [ -n "$ns" ] ; then
			${BEROCONF} set root lan-nameserver $ns
		else
			${BEROCONF} delete root lan-nameserver
		fi

		${BEROCONF} set root lan-mtu $mtu

		debug "$(${BEROCONF} dump root | grep lan)"
		/usr/fallback/update-network
    fi
}

while true ; do

	debug "waiting..."
	t=$(get_netpacket)
	debug "received: $t"

	if echo $t | grep find > /dev/null; then
		hw=$(get_hw)
		ip=$(get_ip)
		nm=$(get_nm)
		mtu=$(get_mtu)
		gw=$(get_gw)
		dhcp=$(get_dhcp)
		ns=$(get_ns)
		serial=$(get_serial)
		appfs=$(get_appfs_version);

		debug "sending: hw=$hw;ip=$ip;nm=$nm;mtu=$mtu;gw=$gw;ns=$ns;dhcp=$dhcp;serial=${serial};appfs=${appfs};"
		send_netpacket "hw=$hw;ip=$ip;nm=$nm;mtu=$mtu;gw=$gw;ns=$ns;dhcp=$dhcp;serial=${serial};appfs=${appfs};"
		debug "sent"
	fi

	hw=$(get_hw)
	if echo $t | grep $hw > /dev/null; then
	    change_network "$t"
	fi

done
