
#
# 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=
NETNAME=
IPADDR=
NETADDR=
SUBNET=
BRDADDR=
PATH=/usr/etc:$PATH

# 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 = "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() {

mstring=`cat $HOSTFILE | egrep "$2" | cut -f2,2`
if [ "${mstring}" = "$2" ]
then 
ed -s $HOSTFILE <<! > /dev/null
/$2/
c
$1	$2
.
w
q
!
else
ed -s $HOSTFILE <<! > /dev/null
a
$1	$2
.
w
q
!
fi

mstring=`cat $NETFILE | egrep "$4" | cut -f1,1`
if [ "${mstring}" = "$4" ]
then 
ed -s $NETFILE <<! > /dev/null
/$4/
c
$4	$3
.
w
q
!
else 
ed -s $NETFILE <<! > /dev/null
a
$4	$3
.
w
q
!
fi

}

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

mstring=`cat $RCFILE | egrep "ifconfig $1" | cut -d" " -f2,2`
if [ "${mstring}" = "$1" ]
then 
ed -s $RCFILE <<! > /dev/null
/ifconfig $1/
c
/usr/etc/ifconfig $1 $2 netmask $3
.
w
q
!
else
ed -s $RCFILE <<! > /dev/null
/IFCONFIG/
i
/usr/etc/ifconfig $1 $2 netmask $3
.
w
q
!
fi

}


# add network to configuration file
addx25net() {
mstring=`cat $INETFILE | egrep $1 | cut -f1,1`
if [ "${mstring}" = "$1" ]
then 
hostname=`cat $INETFILE | egrep $1 | cut -f4,4`
ed -s $HOSTFILE <<! > /dev/null
/$hostname/
d
w
q
!
ed -s $INETFILE <<! > /dev/null
/$1/
c
$1	$2	/dev/ip0	$3
.
w
q
!
else
ed -s $INETFILE <<! > /dev/null
/lo0/
i
$1	$2	/dev/ip0	$3
.
w
q
!
fi
}


# main()
# format : tcp_sndcp_link sn? IP_address

IPADDR=$2
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 $1 $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 $1 /dev/sndcp $HOSTNAME
exit 0
# end
