#
# SCCS_ID = '@(#)setup_gated (TWG)        1.1      14:24:00 - 89/08/17'
#
# @(#) Copyright 1989. The Wollongong Group, Inc.  All Rights Reserved.
#
PATH=$PATH:.;export PATH

#
# install the drivers from the driver disk
#
GCONFFILE="/usr/etc/gated.conf"

# Prompt for yes or no answer - returns non-zero for no
askyn() {
	while	echo "$* (y/n) \c">&2
	do	read yn rest
		case $yn in
		     [yY]) return 0 		                  ;;
	             [nN]) return 1		                  ;;
		        *) echo "\nPlease answer y, n, or q" >&2  ;;
		esac
	done
}

# input - String to display
# output - value in "$ansvalue"
askvalue() {
	isitok=
	while [ "$isitok" = ""  ]
	do
		echo "Please enter $2 > \c"
		read ansvalue
		if [ "$ansvalue" = "" ]
		then
			if [ "$1" = "must" ]
			then
				echo "\tYou must give an answer"
			else
				isitok=ok
			fi
		else
			echo "\t$2 = $ansvalue"
			askyn "\tIs this OK?" && {
				# answered yes
				isitok=ok
			}
		fi
	done
}

# Add line to configuration file
addgate() {

ed - $GCONFFILE <<! > /dev/null
/$1/+1
i
$2
.
w
q
!

}

# substitute for a string in a file
editavar() {

ed - $1 <<! > /dev/null
1,\$s/$2/$3/
w
q
!

}

# main
# Gated setup
#

egp="EGP no"
editavar $GCONFFILE "EGP yes" "$egp"
askyn "Do you wish to setup EGP" && {
	egp="EGP yes"
	editavar $GCONFFILE "EGP no" "$egp"
	askvalue "must" "the autonomous system number"
	auto=$ansvalue
	egp="autonomoussystem $ansvalue"
	addgate "#autonomoussystem" "$egp"
	echo "\tEnter your EGP neighbors (EOF when done)"
	echo "\t\c"
	read neighbor
	egp="egpneighbor $neighbor"
	addgate "Format: egpneighbor" "$egp"
	echo "\t\c"
	count=1
	while read neighbor 
	do
		egp="egpneighbor $neighbor"
		addgate "Format: egpneighbor" "$egp"
		count=`expr $count + 1`
		echo "\t\c"
	done
	echo
	echo "\tYou entered $count neighbors"
	askvalue "must" "maximum number of neighbors to acquire"
	egp="egpmaxacquire $ansvalue"
	addgate "#egpmaxacquire" "$egp"
	askyn "\tDo you wish to setup the egp metricin option" && {
		askvalue "must" "the egp metricin metric"
		egp="egpneighbor metricin $ansvalue"
		addgate "Format: egpneighbor" "$egp"
	}
	askyn "\tDo you wish to setup the egp metricout option" && {
		askvalue "must" "the egp metricout metric"
		egp="egpneighbor metricout $ansvalue"
		addgate "Format: egpneighbor" "$egp"
	}
	askyn "\tDo you wish to setup the egp ASin option" && {
		egp="egpneighbor ASin $auto"
		addgate "Format: egpneighbor" "$egp"
	}
	askyn "\tDo you wish to setup the egp nogendefault option" && {
		egp="egpneighbor nogendefault"
		addgate "Format: egpneighbor" "$egp"
	}
	askyn "\tDo you wish to setup the egp acceptdefault option" && {
		egp="egpneighbor acceptdefault"
		addgate "Format: egpneighbor" "$egp"
	}
	if [ "$egp" != "egpneighbor acceptdefault" ] 
	then
		askyn "\tDo you wish to setup the egp defaultout option" && {
		askvalue "must" "the egp defaultout metric"
		egp="egpneighbor defaultout $ansvalue"
		addgate "Format: egpneighbor" "$egp"
		}
	fi
	askyn "\tDo you wish to setup the egp validate option" && {
		egp="egpneighbor validate"
		addgate "Format: egpneighbor" "$egp"
	}
}

askyn "Do you wish to setup RIP" && {
	askvalue "must" "RIP type (yes, supplier, pt-pt, quiet, gateway)"
	if [ "$ansvalue" = "pt-pt" ]
	then
		ansvalue=pointtopoint
	fi
	rip="RIP $ansvalue"
	editavar $GCONFFILE "RIP no" "$rip"
	rtype=$ansvalue
	if [ "$rtype" = "gateway" ] 
	then
		askvalue "must" "RIP Gateway metric"
		rip2="$rip $ansvalue"
		editavar $GCONFFILE "$rip" "$rip2"
	fi
	if [ "$rtype" = "pointtopoint" ] 
	then
		echo "\tEnter direct RIP gateways (EOF when done)"
		echo "\t\c"
		read rip 
		echo "\t\c"
		while read gw 
		do
			rip="$rip $gw "
			echo "\t\c"
		done
		rip2="sourceripgateways $rip"
		addgate "Format: sourceripgateways" "$rip2"
		echo
	fi
}
