#!/bin/sh

################### shell file helpers

PATH="/usr/lib/alterator-net-common:$PATH"

shell_add_or_subst()
{
	local name="$1" && shift
	local value="$1" && shift
	local file="$1" && shift

	[ -f "$file" ] || touch "$file"
	if grep -qs "^$name" "$file"; then
		/bin/sed -r -i "s,^$name.*,$name$value," -- "$file"
		return 0
	fi
	printf %s\\n "$name$value" >> "$file"
}

shell_get()
{
	local name="$1" && shift
	local file="$1" && shift

	[ -f "$file" ] &&
	grep -qs "^$name=" "$file" &&
	grep "^$name=" "$file"|sed "s,^$name=,,"
}

################### interface modificators

list_mask()
{
    for i in `seq 32 -1 0`; do 
        printf '("%s" label "/%s (%s)")' "$i" "$i" "$(maskname "$i")"
    done
}

list_iface()
{
    iflist|
        while read iface mac; do
            printf '("%s" label "%s (%s)")' "$iface" "$iface" "$(ifaceinfo "$iface")"
        done
}

restart_iface()
{
    ifdown "$1" && ifup "$1"
}

restart_all_ifaces() {
    iflist|
        while read iface mac; do
		restart_iface "$iface"
        done >&2
}

read_iface()
{
	local name="$1" && shift
	echo "("
	
	#collect general information
	local info=
	ifcheckup "$name" && info="`_ "interface is up"`" || info="`_ "interface is down"`"
	
	if ! ifcheckwireless "$name"; then
	    ifcheckplug "$name"
	    case "$?" in
		2) info="$info, `_ "plugged"`"   ;;
		3) info="$info, `_ "unplugged"`" ;;
	    esac
	fi

	printf 'info "%s"' "$info"
	printf 'wireless %s' "$(ifcheckwireless "$name" && echo "#t" || echo "#f")"

	local addr= ip= mask= bootproto=
	[ ! -s "/etc/net/ifaces/$name/ipv4address" ] ||
		addr=$(grep '^[0-9]' "/etc/net/ifaces/$name/ipv4address" | head -n1 || echo "")

	if [ -n "$addr" ]; then
		ip="${addr%%/*}"
		mask="${addr#$ip}"; mask="${mask#/}"
		
		printf 'ip "%s" mask "%s"\n' "$ip" "${mask:-32}"
	fi

	[ ! -s "/etc/net/ifaces/$name/ipv4route" ] ||
		printf 'default "%s"\n' \
			$(grep '^default' "/etc/net/ifaces/$name/ipv4route" | sed -r 's,default[[:space:]]+via[[:space:]],,' || echo "")

	bootproto="$(ifvar "$name" "BOOTPROTO")"
	[ "$bootproto" = "dhcp" ] && echo "dhcp #t" || echo "dhcp #f"

	echo ")"
}

write_iface()
{
	local name="$1" && shift
	local ifacedir="/etc/net/ifaces/$name"
	local options="$ifacedir/options"

	[ -d "$ifacedir" ] || mkdir "$ifacedir"
	
	[ -n "$in_mask" ] && [ -n "$in_ip" ] && \
		printf '%s/%s\n' "$in_ip" "$in_mask" >"/etc/net/ifaces/$name/ipv4address"

	[ -n "$in_default" ] && [ -s "/etc/net/ifaces/$name/ipv4address" ] && \
		printf 'default via %s\n' "$in_default" >"/etc/net/ifaces/$name/ipv4route"

	shell_add_or_subst "TYPE=" "eth" "$options"

	[ -n "$in_dhcp" ] &&
		shell_add_or_subst "BOOTPROTO=" \
			$([ "$in_dhcp" = "#t" ] && echo "dhcp" || echo "static")\
			"$options"
}

_()
{
LANG=${in_language%%;*}.utf8 gettext "alterator-net-eth" "$1"
}

. /usr/share/alterator/build/backend3.sh

#initial actions
ifup lo
iftabupdate

on_message()
{
	case "$in_action" in
		constraints)
			echo '('
			printf 'info (label "%s")' "`_ "Interface state"`"
			printf 'dhcp (exclude (#t ip) exclude (#t default) exclude (#t mask) default #f label "%s")' \
				"`_ "Use DHCP"`"
			printf 'ip (required #t ipv4-address #t label "%s")' \
				"`_ "IP address"`"
			printf 'default (ipv4-address #t label "%s")' \
				"`_ "Default gateway"`"
			printf 'mask (label "%s")' \
				"`_ "NetMask"`"
			echo ')'
			;;
		list)
			echo '('
			if [ "$in__objects" == "/" ];then
				list_iface
			else
				list_mask
			fi
			echo ')'
			;;
		read) 
			if [ "$in__objects" == "/" ];then
				echo '()'
			else
				read_iface "${in__objects}"
			fi
			;;
		write)
			write_iface "${in__objects}"
			[ "$in_restart" = "#f" ] || restart_iface "$in__objects"
			echo '()'
			;;
		restart)
			restart_all_ifaces
			echo '()'
			;;
		*)
			echo '#f'
			;;
	esac
}

message_loop
