#
# SCCS_ID - '@(#)setup_driver        2.8      18:31:54 - 89/06/30 '
#
#   @(#) Copyright 1986. The Wollongong Group, Inc.  All Rights Reserved.
PATH=$PATH:.;export PATH

#
# install the drivers from the driver disk
#
RCFILE=/etc/rc2.d/S86wintcp
INETFILE=/usr/etc/inetinit.cf
HOSTFILE=/etc/hosts
NETFILE=/etc/networks
HOSTNAME=$1
NETNAME=
IPADDR=
NETADDR=
SUBNET=
BRDADDR=

# 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 - $1 ignored,  $2 = String to display
# output - value in "$ansvalue"
askipvalue() {
	isitok=
	while [ "$isitok" = ""  ]
	do
		echo "Please enter $2 > \c"
		read ansvalue
		if [ "$ansvalue" = "" ]
		then
			echo "\tYou must give an answer to the question"
		else
			ansvalue=`inet_addr $ansvalue`
			code=$?
			if [ $code != 0 ]
			then
				echo "\tThat is not a valid IP address, \c"
				echo "please try again"
			else
				echo "\t$2 = $ansvalue"
				askyn "\tIs this OK?" && {
					# answered yes
					isitok=ok
				}
			fi
		fi
	done
}

# input - $1 = "must" or other,  $2 = 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 give an answer"
			else
				isitok=ok
			fi
		else
			echo "\t$2 = $ansvalue"
			askyn "\tIs this OK?" && {
				# answered yes
				isitok=ok
			}
		fi
	done
}

# add network info to host/network database files
addnetwork() {

ed -s $HOSTFILE <<! > /dev/null
a
$1	$2
.
w
q
!

ed -s $NETFILE <<! > /dev/null
a
$4	$3
.
w
q
!

}

# add ifconfig command to the /etc/rc2.d/S86wintcp startup file
addifconfig() {

if [ "$4" = "" ]
then

ed -s $RCFILE <<! > /dev/null
/IFCONFIG/
i
/usr/etc/ifconfig $1 $2 netmask $3
.
w
q
!

else

ed -s $RCFILE <<! > /dev/null
/IFCONFIG/
i
/usr/etc/ifconfig $1 $2 netmask $3 broadcast $4
.
w
q
!

fi

}

# add network to configuration file
addethernet() {

ed -s $INETFILE <<! > /dev/null
/lo0/
i
$1	$2	/dev/arp	$3
$1	/dev/arp	/dev/ip0	$3
.
w
q
!

}

# add network to configuration file
addx25net() {

ed -s $INETFILE <<! > /dev/null
/lo0/
i
$1	$2	/dev/ip0	$3
.
w
q
!

}

# test for ethernet interface and configure if found

askether() {
if [ -c /dev/$1 ]
then
    echo "Please enter the configuration for the $2 ethernet interface"
    askipvalue "must" "the IP address for this interface" && {
	IPADDR=$ansvalue
    }
    SUBNET=`inet_mask $IPADDR`
    test "$3" != "en0" && askvalue "must" "the IP address symbolic name" && {
	HOSTNAME=$ansvalue
    }
    echo "\tThe symbolic name for this IP address is $HOSTNAME"
    askyn "\tDo you want to specify a subnet mask?" && {
	askvalue "must" "the network subnet mask" && {
	    SUBNET=$ansvalue
	}
    }
    NETADDR=`inet_net $IPADDR $SUBNET`
    code=$?
    echo "\tThe network address for this interface will be $NETADDR"
    askyn "\tDo you want to use a 4.2BSD style broadcast address?" && {
	BRDADDR=`inet_brd $IPADDR $SUBNET`
	addifconfig $3 $HOSTNAME $SUBNET $BRDADDR
    } || {
	BRDADDR=
	addifconfig $3 $HOSTNAME $SUBNET
    }
    askvalue "must" "the symbolic name for this network" && {
	NETNAME=$ansvalue
    }
    addethernet $3 /dev/$1 $HOSTNAME
    addnetwork $IPADDR $HOSTNAME $NETADDR $NETNAME
fi
}

#
# Test for SNDCP/X.25 IP interface
#
askx25() {
    echo "Please enter the configuration info for the $2 interface"
    askipvalue "must" "the IP address for this interface" && {
	IPADDR=$ansvalue
    }
    SUBNET=`inet_mask $IPADDR`
    askvalue "must" "the IP address symbolic name" && {
	HOSTNAME=$ansvalue
    }
    askyn "\tDo you want to specify a subnet mask?" && {
	askvalue "must" "the network subnet mask" && {
	    SUBNET=$ansvalue
	}
	addifconfig $3 $HOSTNAME $SUBNET
    }
    NETADDR=`inet_net $IPADDR $SUBNET`
    code=$?
    echo "\tThe network address for this interface will be $NETADDR"
    askvalue "must" "the symbolic name for this network" && {
	NETNAME=$ansvalue
    }
    addnetwork $IPADDR $HOSTNAME $NETADDR $NETNAME
    addx25net $3 /dev/$1 $HOSTNAME
}

# main()

#
# Test for first ethernet interface
#
askether ln0 first	en0
askether ln1 second	en1
askether ln2 third	en2
askether ln3 fourth	en3
askether ln4 fifth	en4

if [ -c /dev/sndcp ]
then
	askyn "\tDo you want to specify  X.25 Port 0 for TCP/IP?" && {
		askx25   sndcp X.25	sn0 
	}
	askyn "\tDo you want to specify  X.25 Port 1 for TCP/IP?" && {
		askx25   sndcp X.25	sn1 
	}
	askyn "\tDo you want to specify  X.25 Port 2 for TCP/IP?" && {
		askx25   sndcp X.25	sn2 
	}
	askyn "\tDo you want to specify  X.25 Port 3 for TCP/IP?" && {
		askx25   sndcp X.25	sn3 
	}
fi
# end
