#!/bin/sh

IFACE=/etc/default/iface
WORKDIR=/tmp/cfg$$

clear
echo "                        NETWORK INTERFACE CONFIGURATION"
echo "                        ==============================="
echo

# Create an empty working directory.
rm -fr $WORKDIR
mkdir $WORKDIR > /dev/null 2>&1
cd $WORKDIR


function clear_spec()
{
	family="AF_INET"
	device="none"

	io="probe"
	aux="probe"
	irq="probe"
	dma="probe"
	maddr="probe"
	msize="probe"

	ipaddr=
	netmask="default"
	broadcast="default"
	dstaddr="none"

	metric=0
	mtu="default"

	arp="default"
	trailers="default"
	up=yes
}

function show_spec()
{
	echo
	echo "Family and device are used to link     A) Family             : $family"
	echo "DDI drivers to network layers.         B) Device             : $device"
	echo
	echo "Hardware setup is only relevent to     C) I/O base address   : $io"
	echo "DDI drivers. The default is to use     D) I/O aux address    : $aux"
	echo "values established during the boot     E) IRQ                : $irq"
	echo "process by the kernel probe routines.  F) DMA                : $dma"
	echo "                                       G) Memory address     : $maddr"
	echo "                                       H) Memory size        : $msize"
	echo
	echo "You must define the internet address   I) Internet address   : $ipaddr"
	echo "for the interface. The netmask and     J) Netmask            : $netmask"
	echo "the broadcast address should default   K) Broadcast address  : $broadcast"
	echo "unless you are using subnets.          L) Destination address: $dstaddr"
	echo
	echo "                                       M) Default metric     : $metric"
	echo "                                       N) MTU                : $mtu"
	echo
	echo "                                       O) Use ARP protocol?  : $arp"
	echo "                                       P) Use trailers?      : $trailers"
	echo "                                       Q) Allow use?         : $up"
	echo
}

function set_option()
{
	case "$1" in
		a|A)	echo -n "Family ($family): "
			read val
			if [ -n "$val" ]; then
				family=$val
			fi
			;;
		b|B)	echo -n "Device ($device): "
			read val
			if [ -n "$val" ]; then
				device=$val
			fi
			;;
		c|C)	echo -n "I/O base address ($io): "
			read val
			if [ -n "$val" ]; then
				io=$val
			fi
			;;
		d|D)	echo -n "I/O aux address ($aux): "
			read val
			if [ -n "$val" ]; then
				aux=$val
			fi
			;;
		e|E)	echo -n "IRQ ($irq): "
			read val
			if [ -n "$val" ]; then
				irq=$val
			fi
			;;
		f|F)	echo -n "DMA ($dma): "
			read val
			if [ -n "$val" ]; then
				dma=$val
			fi
			;;
		g|G)	echo -n "Memory address ($maddr): "
			read val
			if [ -n "$val" ]; then
				maddr=$val
			fi
			;;
		h|H)	echo -n "Memory size ($msize): "
			read val
			if [ -n "$val" ]; then
				msize=$val
			fi
			;;
		i|I)	echo -n "Internet address ($ipaddr): "
			read val
			if [ -n "$val" ]; then
				ipaddr=$val
			fi
			;;
		j|J)	echo -n "Netmask ($netmask): "
			read val
			if [ -n "$val" ]; then
				netmask=$val
			fi
			;;
		k|K)	echo -n "Broadcast address ($broadcast): "
			read val
			if [ -n "$val" ]; then
				broadcast=$val
			fi
			;;
		l|L)	echo -n "Destination address ($dstaddr): "
			read val
			if [ -n "$val" ]; then
				dstaddr=$val
			fi
			;;
		m|M)	echo -n "Default metric ($metric): "
			read val
			if [ -n "$val" ]; then
				metric=$val
			fi
			;;
		n|N)	echo -n "MTU ($mtu): "
			read val
			if [ -n "$val" ]; then
				mtu=$val
			fi
			;;
		o|O)	echo -n "Use ARP protocol? ($arp): "
			read val
			if [ -n "$val" ]; then
				case "$val" in
					y|Y|yes|YES|n|N|no|NO|default)
						arp=$val
						;;
					*)
						echo "Allowed values are yes, no or default"
						;;
				esac
			fi
			;;
		p|P)	echo -n "Use trailers? ($trailers): "
			read val
			if [ -n "$val" ]; then
				case "$val" in
					y|Y|yes|YES|n|N|no|NO|default)
						trailers=$val
						;;
					*)
						echo "Allowed values are yes, no or default"
						;;
				esac
			fi
			;;
		q|Q)	echo -n "Allow use? ($up): "
			read val
			if [ -n "$val" ]; then
				case "$val" in
					y|Y|yes|YES|n|N|no|NO)
						up=$val
						;;
					*)
						echo "Allowed values are yes or no"
						;;
				esac
			fi
			;;
	esac
}

function save_spec()
{
	(
	[ "$family" != "AF_INET" ]    && echo "family=$family"
	[ "$device" != "none" ]       && echo "device=$device"

	[ "$io" != "probe" ]          && echo "io=$io"
	[ "$aux" != "probe" ]         && echo "aux=$aux"
	[ "$irq" != "probe" ]         && echo "irq=$irq"
	[ "$dma" != "probe" ]         && echo "dma=$dma"
	[ "$maddr" != "probe" ]       && echo "maddr=$maddr"
	[ "$msize" != "probe" ]       && echo "msize=$msize"

	[ "$ipaddr" != "" ]           && echo "ipaddr=$ipaddr"
	[ "$netmask" != "default" ]   && echo "netmask=$netmask"
	[ "$broadcast" != "default" ] && echo "broadcast=$broadcast"
	[ "$dstaddr" != "none" ]      && echo "dstaddr=$dstaddr"

	[ "$metric" != 0 ]            && echo "metric=$metric"
	[ "$mtu" != "default" ]       && echo "mtu=$mtu"

	[ "$arp" != "default" ]       && echo "arp=$arp"
	[ "$trailers" != "default" ]  && echo "trailers=$trailers"

	echo "up=$up"
	) > $1
}

function change_config()
{
	iface=$1

	clear_spec
	if [ -r ./$iface ]; then
		. ./$iface
	fi
	show_spec

	opt=
	while [ "$opt" != "quit" ]
	do
		echo -n "Interface $iface: Enter option (blank to redisplay, quit when done) "
		read opt
		if [ -z "$opt" ]; then
			echo
			show_spec
		elif [ "$opt" != "quit" ]; then
			set_option $opt
		fi
	done

	save_spec $iface
}


# If we can see a /proc/net/dev file then we use the data in it to
# create a list of interfaces and create a file for each.
if [ -r /proc/net/dev ]; then
	tail +3 /proc/net/dev | awk -F: '!/[ \t]*(ppp|sl)/ { print $1 }' | while read i
	do
		touch $i
	done
fi

# Split the interface configuration file into individual chunks and
# fill in the files for each interface found.
if [ -r "$IFACE" ]; then
	awk '/^[ \t]*[^#]/' $IFACE | while read i
	do
		set -- $i

		if [ "$1" = "interface" ]; then
			exec 9> $2
		else
			echo "$1=$2" 1>&9
		fi
	done
else
	echo "ipaddr=127.0.0.1" > lo
fi

echo
echo "Available interfaces:"
echo
ls -C
echo

iface=
while [ "$iface" != "quit" ]
do
	echo -n "Enter interface name (blank for list, quit when done): "
	read iface
	if [ -z "$iface" ]; then
		echo
		echo "Available interfaces:"
		echo
		ls -C
		echo
	elif [ "$iface" != "quit" ]; then
		change_config $iface
		echo
	fi
done

# Rebuild the master interface configuration file from the interface
# files in the current directory which are not empty.
for iface in *
do
	if [ -s "$iface" ]; then
		echo "interface $iface"

		while read i
		do
			IFS="="
			set -- $i
			echo "	$1 $2"
		done < $iface

		echo
	fi
done > $IFACE

# Clean up...
cd /
rm -fr $WORKDIR
