#!/bin/sh

function show_help()
{
	clear
	cat << !!SEND_MIKE_CHOCCY_NOW!!
No help yet - sorry!


!!SEND_MIKE_CHOCCY_NOW!!

	echo -n "Press <return> to continue: "
	trap "stty echo" 0
	stty -echo
	read i
	stty echo
	trap "" 0
}


function show_gateways()
{
	gfile=$1

	clear
	echo "                              GATEWAY CONFIGURATION"
	echo "                              ====================="
	echo
	echo
	echo "  Type  Destination             Gateway          Metric     Mode"
	echo "  ----  -----------             -------          ------     ----"
	echo

	awk '{
		printf "  %-4.4s  %-15.15s         %-15.15s  %-6d          %s\n",
		$1, $2, $3, $4, $5
	}' $gfile

	echo
	echo
	echo
}


function get_gateways()
{
	gfile=$1

	while read i
	do
		set -- $i
		if [ "$1" = "net" -o "$1" = "host" ]; then
			echo "$1 $2 $4 $6 $7"
		fi
	done < /etc/gateways > $gfile
}


function set_gateways()
{
	gfile=$1

	# Remove gateway lines from /etc/gateways
	awk '$1 !~ /net|host/ { print $0 }' /etc/gateways > /etc/.gateways

	# Add new set of gateway lines
	awk '{
		printf "%-4s %-15s gateway %-15s metric %s %s\n",
			$1, $2, $3, $4, $5
	}' $gfile >> /etc/.gateways

	# Install the new /etc/gateways
	ln -f /etc/.gateways /etc/gateways
	rm -f /etc/.gateways
}


function add_gateway()
{
	gfile=$1
	dest=$2

	echo

	# See if this destination is already known.
	it=`awk "\\\$2 == \"$2\" { print \\\$0 }" $gfile`
	set -- $it
	norh=$1
	gateway=$3
	metric=$4
	mode=$5

	# Get net or host.
	it=
	while [ "$it" != "net" -a "$it" != "host" -a "$it" != "quit" ]
	do
		echo -n "Net or host destination? (net or host): "
		[ -n "$norh" ] && echo -n "[$norh]: "
		read it
		if [ -z "$it" ]; then
			it=$norh
		fi
	done
	if [ "$it" = "quit" ]; then
		return
	fi
	norh="$it"

	# Get the gateway
	it=
	while [ -z "$it" ]
	do
		echo -n "Gateway: "
		[ -n "$gateway" ] && echo -n "[$gateway]: "
		read it
		if [ -z "$it" ]; then
			it=$gateway
		fi
	done
	if [ "$it" = "quit" ]; then
		return
	fi
	gateway=$it

	# Get the metric
	it=
	while [ -z "$it" ]
	do
		echo -n "Metric: "
		[ -n "$metric" ] && echo -n "[$metric]: "
		read it
		if [ -z "$it" ]; then
			it=$metric
		fi
	done
	if [ "$it" = "quit" ]; then
		return
	fi
	metric=$it

	# Get the mode
	it=
	while [ "$it" != "active" -a "$it" != "passive" -a "$it" != "external" -a "$it" != "quit" ]
	do
		echo -n "Mode (active, passive or external): "
		[ -n "$mode" ] && echo -n "[$mode]: "
		read it
		if [ -z "$it" ]; then
			it=$mode
		fi
	done
	if [ "$it" = "quit" ]; then
		return;
	fi
	mode=$it

	# Delete the old details (if any) and add the new.
	awk "\$2 != \"$2\" { print \$0 }" $gfile > /tmp/.tmp$$
	ln -f /tmp/.tmp$$ $gfile
	rm -f /tmp/.tmp$$
	echo "$norh $dest $gateway $metric $mode" >> $gfile
}


function del_gateways()
{
	gfile=$1

	awk "\$2 !~ /$2/ { print \$0 }" $gfile > /tmp/.tmp$$
	ln -f /tmp/.tmp$$ $gfile
	rm -f /tmp/.tmp$$
}


get_gateways /tmp/g$$
show_gateways /tmp/g$$
opt=
while [ "$opt" != "quit" ]
do
	echo "Options: add <destination>, delete <destination>, help or quit"
	echo
	echo -n "Option: "
	read opt
	case "$opt" in
		help)
			show_help
			show_gateways /tmp/g$$
			;;
		add*)
			set -- $opt
			if [ -z "$2" ]; then
				echo
				echo "usage: add <destination>"
				echo
			else
				add_gateway /tmp/g$$ $2
				show_gateways /tmp/g$$
			fi
			;;
		del*)
			set -- $opt
			if [ -z "$2" ]; then
				echo
				echo "usage: delete <destination>"
				echo
			else
				del_gateways /tmp/g$$ $2
				show_gateways /tmp/g$$
			fi
			;;
		quit)
			;;
		*)
			echo
			echo "unknown option $1"
			echo
			;;
	esac
done
set_gateways /tmp/g$$
rm -f /tmp/g$$
