:
#      @(#)sco_tcp	5.13.1.6 Lachman System V STREAMS TCP  source
#      SCCS IDENTIFICATION
#
#	System V STREAMS TCP - Release 4.0
#
#	Copyright 1987, 1988, 1989 Lachman Associates, Incorporated (LAI)
#
#	All Rights Reserved.
#
#	The copyright above and this notice must be preserved in all
#	copies of this source code.  The copyright above does not
#	evidence any actual or intended publication of this source
#	code.
#
#	This is unpublished proprietary trade secret source code of
#	Lachman Associates.  This source code may not be copied,
#	disclosed, distributed, demonstrated or licensed except as
#	expressly authorized by Lachman Associates.
#
#	System V STREAMS TCP was jointly developed by Lachman
#	Associates and Convergent Technologies.
#
#
: ${OK=0} ${FAIL=1} ${RELINK=2}
PATH=/bin:/usr/bin:/etc:/etc/conf/bin

LOWER_PRD=$1
LOWER_IF=$2
UPPER_PRD=$3
UPPER_IF=$4
CONFIGURE=$5

# Shell Variables from lli Driver
#------------------------------------------------------------------------
# NODE - Required to create chain
# UNITS - Optional, only usable if the lli driver supports the MACUNITSEL
#	  ioctl.
#------------------------------------------------------------------------

#
# Miscellaneous variable declarations
#
tmp=/tmp/sco_tcp$$
ND=/etc/conf/node.d
FLAGS=-trailers				# Options for ifconfig line
change="N"				# Assume no re-link is required
IQM_NP=/usr/lib/netconfig/tmp/sco_tcp.np	# no_prompt install data
TCPDRVR="tcp"				# configure number of tcp endpoints
UDPDRVR="udp"				# configure number of udp endpoints
SOCKDRVR="socket"			# configure number of sockets

#
# Initialise configuration variables
#
PPP_HOST_IP=
PPP_DEST_IP=
PPP_NM=
PPP_DEST_NAME=
PPP_PAP=
PPP_LOCAL_NAME=
PPP_LOCAL_PASSWD=
SLIP_HOST_IP=
SLIP_DEST_IP=
SLIP_NM=
SLIP_DEST_NAME=
SLIP_BAUD=
SLIP_TTY=
SLIP_LOCAL_NAME=
IP=
NM=
BD=
LOCAL_NAME=
NODENAME=`cat /etc/systemid 2>/dev/null`
DOMAIN=`grep DOMAIN /etc/default/tcp | sed -e 's/DOMAIN=//'`
GATEWAY=N
N_PTTY=16			# No of PTTY's configured by default
N_SOCKET=256			# No of TCP connections configured by default

#
# Parameters used by the functions in tcp_nfs.comm
#
PRODUCT="TCP/IP"
PERMSLIST=/etc/perms/tcprt
LKPERM=/etc/perms/inst
DRIVERDIR=/usr/lib/tcprt/ID
DRIVERS="arp cp ip rip socket arpproc icmp llcloop tcp udp"
KPARAMS="NINODE +150 \
	 NS5INODE +150 \
	 NFILE +150 \
	 NQUEUE +768 \
	 NSTREAM +128 \
	 NBLK4096 +12 \
	 NBLK2048 +32 \
	 NBLK1024 +32 \
	 NBLK512 +64 \
	 NBLK256 +128 \
	 NBLK128 +256 \
	 NBLK64 +256 \
	 NBLK16 +256 \
	 NBLK4 +256 \
	 NMUXLINK 64 \
	 MAXSEPGCNT 1 \
	 NUMTIM 32 \
	 NUMTRW 32"

#
# If the common functions file doesn't exist, we won't get very far
#
COMMONFILE=/usr/lib/netconfig/bin/tcp_nfs.comm

if [ ! -r $COMMONFILE ]
then
	echo "\nError: Missing or unreadable $COMMONFILE." >&2
	exit ${FAIL}
fi

. $COMMONFILE

##############################################################################
# Stub routines used to get configuration details. Redefined in the case
# of interactive operation in /usr/lib/netconfig/bin/sco_tcp.ui
#
get_ppp_param() { : ; }
get_slip_param() { : ; }
get_ether_param() { : ; }
getnodename() { : ; }
getdomname() { : ; }
getgateway() { : ; }

##############################################################################
# Print a warning message to the screen telling the user that there might
# be a conflict between the chosen /etc/hosts values and an already existing
# entry in /etc/hosts.
#
warn_hosts() {
	message "WARNING: The selected values:"
	message "$1"
	message "May conflict with other entries. Edit /etc/hosts to resolve the problem."
	sleep 6
}

##############################################################################
# Do the actual work of putting an entry in /etc/hosts.  If doing this
# causes conflicts, then go ahead and add entry, but warn the user.
# Usage: put_hosts <IP addr> <hostname> <fully qual. name>
#
put_hosts() {
    _IP=$1
    _HOST=$2
    _FULL=$3
    if [ "$_FULL" ] ; then
	if grep -i "^$_IP[ 	][ 	]*$_HOST[ 	][ 	]*$_FULL" \
			/etc/hosts > /dev/null 2>&1
	then
		: done
	else
		if grep -i "^$_IP" /etc/hosts > /dev/null 2>&1 \
		|| grep -i "[ 	]$_HOST[ 	]" /etc/hosts > /dev/null 2>&1 \
		|| grep -i "[ 	]$_FULL[ 	]" /etc/hosts > /dev/null 2>&1 \
		|| grep -i "[ 	]$_FULL$" /etc/hosts > /dev/null 2>&1
		then
			warn_hosts "	$_IP	$_HOST $_FULL"
		fi
		echo "$_IP\t$_HOST $_FULL" >> /etc/hosts
		message "Adding host $_FULL to /etc/hosts"
	fi
    else	# Case where SLIP or PPP remote dest entry is being added.
	if grep -i "^$_IP[ 	][ 	]*$_HOST$" /etc/hosts > /dev/null 2>&1 \
	|| grep -i "^$_IP[ 	][ 	]*$_HOST[ 	]" \
		/etc/hosts > /dev/null 2>&1 
	then
		: done
	else
		if grep -i "^$_IP" /etc/hosts > /dev/null 2>&1 \
		|| grep -i "[ 	]$_HOST[ 	]" /etc/hosts > /dev/null 2>&1 \
		|| grep -i "[ 	]$_HOST$" /etc/hosts > /dev/null 2>&1 
		then
			warn_hosts "	$_IP	$_HOST"
		fi
		echo "$_IP\t$_HOST" >> /etc/hosts
		message "Adding host $_HOST to /etc/hosts"
	fi
    fi
    _IP="" ; _HOST="" ; _FULL=""
}


##############################################################################
# Make sure IP address $1 is in /etc/hosts as us
#
check_hosts() {
	ip_addr=$1
	HOSTNAME=$2
	if [ ! -f /etc/hosts ] ; then
		echo "127.0.0.1\tlocalhost loopback lb" > /etc/hosts
		chmod 644 /etc/hosts
		chown bin /etc/hosts
		chgrp bin /etc/hosts
	fi
	# Don't do any more if we are configuring a loopback driver
	if [ "$LOWER_PRD" = "lo0" ] ; then
	    return 0
	fi
	set -- `cat /etc/systemid`
	HOSTNAME=${HOSTNAME:-$1} # Supplied HOSTNAME overrides default
	#
	# There should not be a default for the domain
	#
	if [ X$DOMAIN = X ]; then
		FULLNAME=${HOSTNAME}
	else
		FULLNAME=${HOSTNAME}.$DOMAIN
	fi
	put_hosts $ip_addr $HOSTNAME $FULLNAME
}

##############################################################################
# Make sure slip destination machine is in /etc/hosts
#
update_sliphosts() {
	put_hosts $SLIP_DEST_IP $SLIP_DEST_NAME ""
}

##############################################################################
# Update ppphosts file
#
update_ppphosts() {
	# If in an upgrade situation, these config files will be retained ...
	[ "$_upgrade" ] && return 0
	put_hosts $PPP_DEST_IP $PPP_DEST_NAME ""

	message "Updating /etc/ppphosts"
	grep "\*nppp[	 ]" /etc/ppphosts > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
		# Get rid of old entry
		grep "\*nppp[	 ]" /etc/ppphosts|grep [\\]$ >/dev/null 2>&1
		if [ $? -eq 0 ] ; then
			sed "/\*nppp/,/[^\\]$/d" /etc/ppphosts > /tmp/ppphosts$$
		else
			sed "/\*nppp/d" /etc/ppphosts > /tmp/ppphosts$$
		fi
		safemv /tmp/ppphosts$$ /etc/ppphosts	
	fi
	echo "*nppp - - idle=5 tmout=3" >> /etc/ppphosts

	grep "[ 	]$PPP_DEST_NAME" /etc/ppphosts > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
		# Get rid of old entry
		grep "[ 	]$PPP_DEST_NAME" /etc/ppphosts | grep [\\]$ >/dev/null 2>&1
		if [ $? -eq 0 ] ; then
			sed "/$PPP_DEST_NAME/,/[^\\]$/d" /etc/ppphosts > /tmp/ppphosts$$
		else
			sed "/$PPP_DEST_NAME/d" /etc/ppphosts > /tmp/ppphosts$$
		fi
		safemv /tmp/ppphosts$$ /etc/ppphosts	
	fi
	echo "$PPP_DEST_IP - $PPP_DEST_NAME $PPP_PAP idle=5 tmout=3" >> /etc/ppphosts

	# PAP stuff is optional
	if [ "$PPP_PAP" = "pap" -a ! "$_reconfig" ] ; then
		message "Updating /etc/pppauth"
		grep "$PPP_LOCAL_NAME[	 ]" /etc/pppauth > /dev/null 2>&1
		if [ $? -eq 0 ] ; then
			# Get rid of old entry
			sed "/$PPP_LOCAL_NAME/d" /etc/pppauth > /tmp/pppauth$$
			safemv /tmp/pppauth$$ /etc/pppauth	
		fi
		echo "*$PPP_LOCAL_NAME $PPP_LOCAL_PASSWD" >> /etc/pppauth
		warning "For PAP to function correctly, please put\n\t '$PPP_LOCAL_NAME $PPP_LOCAL_PASSWD' in remote machine's /etc/pppauth."
	fi

	grep $PPP_DEST_NAME /usr/lib/uucp/Systems 2>&1 > /dev/null || {
		warning "Please add $PPP_DEST_NAME to the uucp Systems file (/usr/lib/uucp/Systems).\n\t Refer to Configuring PPP section of the TCP/IP Administrators Guide"
	}
}

##############################################################################
# Modify /etc/tcp and /etc/strcf for the ppp line case
#
ppp_modify()
{
	ed -s /etc/strcf <<-EOF > /dev/null
	/pppip[ 	]*ip/s/^#//
	w
	q
	EOF
	if [ -n "$OUTPUT" ] ; then
		ed -s /etc/strcf <<-EOF > /dev/null
		/pppip[	 ]*{/
		a
		$OUTPUT
		.
		w
		q
		EOF
	fi

	if [ -n "$IFCONFIG" ] ; then
		ed -s /etc/tcp <<-EOF > /dev/null
		/#	ifconfig/
		i
		$IFCONFIG
		.
		w
		q
		EOF
	fi
}

##############################################################################
# Modify /etc/tcp for the slip line case
#
slip_modify()
{
	if [ -n "$IFCONFIG" ] ; then
		if grep "^[ 	]*slattach" /etc/tcp > /dev/null 2>&1 \
		   || grep "^[ 	]*/etc/slattach" /etc/tcp > /dev/null 2>&1
		then
			search='?slattach'
		else
			search='?ifconfig[ 	][ 	]*lo0'
		fi
		ed -s /etc/tcp <<-EOF > /dev/null
		\$
		$search
		a
		$IFCONFIG
		.
		w
		q
		EOF
	fi
}

##############################################################################
# Modify /etc/tcp and /etc/strcf and /etc/default/tcp for the ethernet case
#
ether_modify()
{
	if [ -n "$OUTPUT" ] ; then
		ed -s /etc/strcf <<-EOF > /dev/null
		/	loopback/
		i
		$OUTPUT
		.
		w
		q
		EOF
	fi

	if [ -n "$IFCONFIG" ] ; then
		ed -s /etc/tcp <<-EOF > /dev/null
		/ifconfig/
		a
		$IFCONFIG
		.
		w
		q
		EOF
	fi
	default_tcp
}

##############################################################################
# Modify /etc/default/tcp
#
default_tcp() {
	[ X$DOMAIN = X ] && return
	ed -s /etc/default/tcp <<-EOF > /dev/null
		/^DOMAIN/
		d
		-1
		a
		DOMAIN=$DOMAIN
		.
		w
		q
		EOF
}

##############################################################################
# Returns the numerical suffix of the last alphabetically tty entry 
# in the /etc/conf/node.d directory.
# If no entries exist then -1 is returned.
# The prefix of the tty node is passed as the argument
# Function is called as
#		last_tty <prefix of the nodes to be searched>
#
last_tty() {
	tmplast=-1
	search_file=/etc/conf/node.d/mpt
	tmp_re="^ptyp"

	[ -f "$search_file" ] && tmplast=`awk -e \
		'BEGIN { last_='"-1"' }
		 $2 ~ /'"$tmp_re"'[0-9]+$/ {
			if (last_ < $4)
				last_ = $4
		 }
	 	 END { print last_ }' $search_file`
	 return
}

##############################################################################
# Update the number of pseudo ttys configured in the kernel
#
chkptty() {
	last_tty
	tmplast=`expr $tmplast + 1`
addstr="How many additional pseudo ttys you want to be created"
rmvstr="How many pseudo ttys you want to be removed"
accpt_nl="no"
	cd /
	while mesg="\n$tmplast Pseudo ttys are currently configured, do you want to:\n
		1. Add Pseudo ttys
		2. Remove Pseudo ttys

Select an option"
		quit="quit"
	do prompt "$mesg" q || break
	select=$cmd
	case $select in
		1) if [ "$tmplast" -lt $N_PTTY ] ; then
			extra_ptty=`expr $N_PTTY - $tmplast`
		   else
			extra_ptty=0
		   fi
		   accpt_nl=yes
		   addno=`getnumber "$addstr" "$extra_ptty"`
		   accpt_nl=no
		   [ "$addno" -gt 0 ] && {
				echo "Adding $addno psuedo terminals ... \c"
				mkdev_ptty -a $addno
				echo "done."
		    }
		    # break out of while loop so we are not prompted again
		    # for addition or removal of pttys
		    break
		    ;;
		2) accpt_nl=yes
		   rmvno=`getnumber "$rmvstr" "$tmplast"`
		   accpt_nl=no
		   [ "$rmvno" -gt 0 ] && {
				echo "removing $rmvno psuedo terminals ... \c"
				mkdev_ptty -d $rmvno
				echo "done."
		    }
		    # break out of while loop so we are not prompted again
		    # for addition or removal of pttys
		    break
		    ;;
		Q|q)
		    # break out of while loop so we are not prompted again
		    # for addition or removal of pttys
		    break
		    ;;

		*) echo "\n	Please select either 1 or 2"
		   continue	;;
	esac
	done
}

##############################################################################
# Set the number of pseudo ttys configured in the kernel to N_PTTY
# Note: This will only up the number of pttys - its not our responsibility
# to down this parameter
#
setptty() {
	last_tty
	tmplast=`expr $tmplast + 1`
	if [ "$tmplast" -lt $N_PTTY ] ; then
		extra_ptty=`expr $N_PTTY - $tmplast`
		message "Adding $extra_ptty more ptty's."
		mkdev_ptty -a $extra_ptty
		message "done."
	fi
}

##############################################################################
# Invoke mkdev ptty without the kernel relink option
# $1 = flags, $2 = no of pttys
#
mkdev_ptty() {
	# environment variable _RELINK prevents
	# mkdev ptty from relinking the kernel
	_RELINK=TRUE /usr/lib/mkdev/ptty $1 $2
	change="Y"		# We will have to relink the kernel
}

##############################################################################
# Set IPFORWARDING and IPSENDREDIRECTS for gateways
# Clear if not a gateway to be conformant with RFC 1122
# Changed by SCO Networking to trigger a kernel relink prompt
# if the new values are different from the old ones.  This 
# is necessary to get the new values compiled into the kernel.
#
set_ipforwarding() {
	__PWD=`pwd`
	cd /etc/conf/cf.d
	_oldval=`./configure -y IPFORWARDING`
	[ "$?" != "0" -o "$_oldval" != "$1" ] && {
		/etc/conf/bin/idtune -f IPFORWARDING $1
		change="Y"
	}
	_oldval=`./configure -y IPSENDREDIRECTS`
	[ "$?" != "0" -o "$_oldval" != "$1" ] && {
		/etc/conf/bin/idtune -f IPSENDREDIRECTS $1
		change="Y"
	}
	cd $__PWD
	__PWD=""
}

##############################################################################
# Modify tty line state
# $1 = driver name (slip/ppp) ; $2 = enable/disable ; $3 = device name
#
line_state() {
	dev_name=$3
	l_state=`grep "$dev_name" /etc/inittab | awk -F: '{ print $3 }'`
	on_off="enable"
	if [ "$l_state" = "OFF" -o "$l_state" = "off"  -o "$l_state" = "" ]
	then
		on_off="disable"
	fi
	if [ "$2" != "$on_off" ] ; then
		echo "\nThe current line state for tty line $dev_name is ${on_off}d."
		echo "For $1 to function correctly the tty line should be $2d."
		while	echo "Do you wish to $2 the line now? (y/n) \c"
		do	read ANSWER
			case $ANSWER in
			Y|y)	eval "$2 $dev_name"
				# break out of while loop so prompt isn't
				# continually presented
				break
				;;
			N|n)
				# break out of while loop so prompt isn't
				# continually presented
				break
				;;
			*)	echo "Please answer 'y' or 'n'. \c"
				;;
			esac
		done
	fi
}

##############################################################################
# Give a warning about the tty line state
# $1 = driver name (slip/ppp) ; $2 = enable/disable
#
line_state_warning() {
	warning "For $1 to function correctly please ensure that the tty\n\t line is $2d."
}

##############################################################################
# Check to see if nouserid exists, if not create it here
#
add_xuser()
{
	grep "^nouser:" /etc/passwd > /dev/null || {
		who=`who am i | awk '{ print $1 }'`
		[ "$who" = "" ] && who=root
		if [ "$who" = "root" ]
		then
			/usr/lib/tcprt/adduser
		else
			warning "You are not logged in as root.\nAfter this installation is finished please log in as root and run the\ncommand /usr/lib/tcprt/adduser."
		fi
	}
}

#####################################################################
# If extra entries are necessary to accommodate devices with
# extended minor device number then this functions creates them
# by calling sconfigxtr_mdev() for the three different devices required.
# Called as:	sconfigxtr <No of devices asked to be created>
#
sconfigxtr () {
	sconfigxtr_mdev $1 1
	sconfigxtr_mdev $1 2
	sconfigxtr_mdev $1 3
	change="Y"		# We will have to relink the kernel
}

#####################################################################
# If extra entries are necessary to accommodate devices with
# extended minor device number then this functions creates them.
# This functions is doing this job by being called separately for 
# the three different devices required.
# Called as:	sconfigxtr <No of devices asked to be created> <1,2,3, or 4>
#
sconfigxtr_mdev () {
	case "$2" in
		1) dev_name=$TCPDRVR
		   dev_name2=$TCPDRVR
		   dev_flg2="ocis"
		   dev_flg3="iScM"
		   dev_maj=`/etc/conf/cf.d/configure -j $TCPDRVR`
		   sdev_line="tcp\tY\t256\t0\t0\t0\t0\t0\t0\t0"
	  	   sdev_file=/etc/conf/sdevice.d/$TCPDRVR	
				;;
		2) dev_name=$UDPDRVR
		   dev_name2=$UDPDRVR
		   dev_flg2="ocis"
		   dev_flg3="iScM"
		   dev_maj=`/etc/conf/cf.d/configure -j $UDPDRVR`
		   sdev_line="udp\tY\t256\t0\t0\t0\t0\t0\t0\t0"
	  	   sdev_file=/etc/conf/sdevice.d/$UDPDRVR	
				;;
		3) dev_name=$SOCKDRVR
		   dev_name2="sock"
		   dev_flg2="ocrwis"
		   dev_flg3="icM"
		   dev_maj=`/etc/conf/cf.d/configure -j $SOCKDRVR`
		   sdev_line="socket\tY\t256\t0\t0\t0\t0\t0\t0\t0"
	  	   sdev_file=/etc/conf/sdevice.d/$SOCKDRVR	
				;;
	esac

	last_mdev $dev_name

	entr_exs=$cntd_entr
		# mdevice entries existing so far
	dev_num=`expr 256 \* $entr_exs`
		# number of devices configured
	entr_req=`expr \( $dev_num + $1 \) / 256`
		# mdevice entries to exist after the creation of the extra nodes
	entr_xtr=`expr $entr_req - $entr_exs`
		# mdevice entries to be added in mdevice after 
		# the creation of the extra nodes

	if [ "$entr_xtr" -gt 0 ]
	 then
		i=0
		while [ "$i" -lt "$entr_xtr" ]
		do
			next_maj=`/etc/conf/cf.d/configure -j NEXTMAJOR`
			off_set=`expr \( $entr_exs + $i \) \* 256`
			entry="$dev_name\t$dev_flg2\t$dev_flg3\t$dev_name2\t0\t$next_maj\t$dev_maj\t$off_set\t-1"
			echo $entry >> /etc/conf/cf.d/mdevice
			echo $sdev_line >> $sdev_file
			i=`expr $i + 1`
		done
	 fi
}

#####################################################################
# It returns the number of entries in the mdevice file with first field
# equal to the string passed as first argument.
#
last_mdev () {
	cntd_entr=0

	cntd_entr=`awk -e 'BEGIN { entries = 0 }
			   \$1 == "'"$1"'" { entries ++ }
			   END { print entries }' /etc/conf/cf.d/mdevice`
	 return 
}

#####################################################################
# It removes the mdevice entries corresponding to extended minor device
# numbered devices. It does not touch the entry with the base major number.
# Called as:	rmvdrvr_mul	<driver name> <base major no>
#
rmvdrvr_mul () {
	tmp_majno=`grep "^$1" mdevice | tail -1 | awk '{ print $6 }'`
	./configure -d -c -Z -m $tmp_majno >> conflog
}


#####################################################################
# If extra entries are to be removed from devices with
# extended minor device number then this functions removes them
# by calling sconfigxtr_mdev() for the three different devices required.
# Called as:	sunconfigxtr <No of devices asked to be removed>
#
sunconfigxtr () {
	sunconfigxtr_mdev $1 1
	sunconfigxtr_mdev $1 2
	sunconfigxtr_mdev $1 3
	change="Y"		# We will have to relink the kernel
}

#####################################################################
# If extra entries are to be removed from devices with
# extended minor device number then this functions removes them
# This functions is doing this job by being called separately for 
# the tcp upd and socket drivers.
# Called as: sunconfigxtr_mdev <No of devices asked to be created> <1,2 or 3>
#
sunconfigxtr_mdev () {
	case "$2" in
		1) dev_name=$TCPDRVR
		   dev_maj=`/etc/conf/cf.d/configure -j $TCPDRVR`
	  	   sdev_file=/etc/conf/sdevice.d/$TCPDRVR	
				;;
		2) dev_name=$UDPDRVR
		   dev_maj=`/etc/conf/cf.d/configure -j $UDPDRVR`
	  	   sdev_file=/etc/conf/sdevice.d/$UDPDRVR	
				;;
		3) dev_name=$SOCKDRVR
		   dev_maj=`/etc/conf/cf.d/configure -j $SOCKDRVR`
	  	   sdev_file=/etc/conf/sdevice.d/$SOCKDRVR	
				;;
	esac

	last_mdev $dev_name

	entr_exs=$cntd_entr
		# mdevice entries existing so far
	dev_num=`expr 256 \* $entr_exs`
		# number of devices configured
	entr_xtr=`expr $1 / 256`
		# mdevice entries to remove 

	if [ "$entr_xtr" -gt 0 ] ; then
		i=0
		while [ "$i" -lt "$entr_xtr" ]
		do
			cat $sdev_file | sed -e "$ d" > /tmp/temp.$dev_name
			safemv /tmp/temp.$dev_name $sdev_file
			rmvdrvr_mul $dev_name $dev_maj
			i=`expr $i + 1`
		done
		addno=`expr \( $entr_exs - $entr_xtr - 1 \) \* 256`
	fi
}

##############################################################################
# Update the number of sockets configured in the kernel
#
chktcpconns() {
	last_mdev $SOCKDRVR
	entr_exs=$cntd_entr
		# mdevice entries existing so far
	dev_num=`expr 256 \* $entr_exs`
		# number of devices configured
	addstr="How many additional TCP connections do you want to be created\n"
	rmvstr="How many TCP connections do you want to be removed\n"

	while mesg="\n$dev_num TCP connections currently configured, do you want to:\n
		1. Add TCP Connections 
		2. Remove TCP Connections

Select an option"
		quit="quit"
	do prompt "$mesg" q || break
	select=$cmd
	case $select in
		1) accpt_nl=yes
		   addno=`getnumber "$addstr" "256"`
		   accpt_nl=no
		   __PWD=`pwd`
		   cd /etc/conf/cf.d
		   [ "$addno" -gt 0 ] && {
				temp=`expr $addno / 256`
				if [ `expr $temp \* 256` -ne $addno ]; then
					echo "\n\tSockets must be configured in multiples of 256"
					sleep 1
					cd $__PWD
					continue
				else
					echo "Adding $addno TCP connections ... \c"
					sconfigxtr $addno
					echo "done."
				fi
		    }
  		    cd $__PWD
		    # break out of while loop so we are not prompted again
		    # for addition or removal of TCP connections
		    break
		    ;;
		2) accpt_nl=yes
		   rmvno=`getnumber "$rmvstr" "256"`
		   accpt_nl=no
		   if [ $rmvno -ge $dev_num ] ; then
					echo "\n\tIf you want 0 sockets then remove SCO TCP/IP via custom"
					sleep 1
					cd $__PWD
					continue
		   fi
		   __PWD=`pwd`
		   cd /etc/conf/cf.d
		   [ "$rmvno" -gt 0 ] && {
				temp=`expr $rmvno / 256`
				if [ `expr $temp \* 256` -ne $rmvno ]; then
					echo "\n\tSockets must be configured in multiples of 256"
					sleep 1
					cd $__PWD
					continue
				else
					echo "Removing $rmvno TCP connections ... \c"
					sunconfigxtr $rmvno
					echo "done."
				fi
		    }
  		    cd $__PWD
		    # break out of while loop so we are not prompted again
		    # for addition or removal of TCP connections
		    break
		    ;;
		Q|q)
		    # break out of while loop so we are not prompted again
		    # for addition or removal of TCP connections
		    break
		    ;;

		*) echo "\n	Please select either 1 or 2"
		   continue	;;
	esac
	done
}

##############################################################################
# Set the number of sockets configured in the kernel to N_SOCKET
#
settcpconns() {
	last_mdev $SOCKDRVR
	entr_exs=$cntd_entr
		# mdevice entries existing so far
	dev_num=`expr 256 \* $entr_exs`
		# number of devices configured
	extra_socket=`expr $N_SOCKET - $dev_num`
		# number of scokets to be added/removed

	case $extra_socket in
		0) return $OK
		   break
		   ;;

		[1-9]*) __PWD=`pwd`
		   cd /etc/conf/cf.d
			temp=`expr $extra_socket / 256`
			if [ `expr $temp \* 256` -ne $extra_socket ]; then
				cd $__PWD
				return $FAIL
			else
				message "Adding $extra_socket TCP connections ... \c"
				sconfigxtr $extra_socket
				message "done."
			fi
  		    cd $__PWD
		    break
		    ;;

		-[1-9]*) __PWD=`pwd`
		   cd /etc/conf/cf.d
		   extra_socket=`expr 0 - $extra_socket`
		   [ $extra_socket -eq $dev_num ] && {
			cd $__PWD
			return $FAIL
		   }
			temp=`expr $extra_socket / 256`
			if [ `expr $temp \* 256` -ne $extra_socket ]; then
				cd $__PWD
				return $FAIL
			else
				message "Removing $extra_socket TCP connections ... \c"
				sunconfigxtr $extra_socket
				message "done."
			fi
  		    cd $__PWD
		    break
		    ;;
	esac
}

##############################################################################
# main() 
#
trap "cleanup $FAIL" 1 2 3 15

# First and foremost, lets source in the relevant function definitions.
# For a user interface version use /usr/lib/netconfig/bin/sco_tcp.ui
# For reconfiguration use supplied /usr/lib/netconfig/tmp/sco_tcp.reconf
# Non interactive functions for in place upgrade or ODT mass installation.
#
# Functions required and variables to be set:
#	get_ppp_param()
#		- PPP_HOST_IP = Host IP address for ppp line
#		- PPP_DEST_IP = Destination IP address
#		- PPP_NM = Netmask for ppp line
#		- PPP_DEST_NAME = Destination name in /usr/lib/uucp/Systems
#		- PPP_PAP = Is PAP required? ="pap" (yes), ="" (no)
#               - PPP_LOCAL_NAME = Local host name, also used in /etc/pppauth
#               - PPP_LOCAL_PASSWD = Local password in /etc/pppauth
#	get_slip_param()
#		- SLIP_HOST_IP = Host IP address for slip line
#		- SLIP_DEST_IP = Destination IP address
#		- SLIP_NM = Netmask for slip line (RESERVED)
#		- SLIP_DEST_NAME = Destination name of system
#		- SLIP_BAUD = Slip line baud rate
#		- SLIP_TTY = Tty device for slip line
#		- SLIP_LOCAL_NAME = Local host name for slip connection
#	get_ether_param()
#		- IP = IP address
#		- NM = Netmask
#		- BD = Broadcast address
#		- LOCAL_NAME = Local interface name (Optional)
#	getnodename()
#		- NODENAME = Nodename of system
#	getdomname()
#		- DOMAIN = Domain name (eg sco.COM)
#	getgateway()
#		- GATEWAY = Is the machine a gateway ie set IPFORWARDING
#
_upgrade=
_no_prompt=
_reconfig=

[ -f /tmp/tcprt_upgrade ] && _upgrade=TRUE
[ -f $IQM_NP ] && _no_prompt=TRUE
[ -f /tmp/sco_tcp_reconf ] && _reconfig=TRUE

# Check that no more that one mode flag is set
n_of_args=`echo "$_upgrade $_no_prompt $_reconfig" | wc -w`
if [ $n_of_args -gt 1 ] ; then
	message "Netconfig sco_tcp init script is confused. Multiple modes."
	cleanup $FAIL
fi

if [ "$_upgrade" ] ; then
	# Source in function declarations for upgrade
	. /usr/lib/netconfig/bin/sco_tcp.up
else if [ "$_no_prompt" ] ; then
	# Source in variable declarations + functions for ODT installation
	. $IQM_NP
else if [ "$_reconfig" ] ; then
	# Source in variable declarations for reconfiguration
	. /usr/lib/netconfig/tmp/sco_tcp.reconf
else
	# I guess it must be interactive, so source in interactive functions
	. /usr/lib/netconfig/bin/sco_tcp.ui
fi ; fi ; fi	# fo, fo, fo, and off to work we go ...

# Check to see if chain is already configured, if so do nothing unless reconfig
netconfig -s | grep "sco_tcp#$LOWER_PRD" > /dev/null 2>&1
[ ! "$_reconfig" -a $? -eq 0 ] && cleanup $OK

[ "$_reconfig" ] || message "\nInstalling SCO TCP/IP over $LOWER_PRD\n"

# The pseudo loopback driver must be installed first
if [ "$LOWER_PRD" != "lo0" ] ; then
		# We should really just call netconfig (recursively) to install
		# the loopback driver and issue an information message, but for
		# now issue a warning message and exit.
		netconfig -s | grep "sco_tcp#lo0" > /dev/null 2>&1
		[ $? -eq 0 ] || {
		    Error "The loopback driver is not installed. Please use netconfig to add the\n       lo0 driver then add $LOWER_PRD."
		    cleanup $FAIL		# Exit with error
		}
fi

serdrv=$LOWER_PRD

# Netconfig sets CONFIGURE to "Y" if this is the first sco_tcp to be installed
if [ "${CONFIGURE}" = "Y" ] ; then

	# Check to see that the link kit is installed, 
	# since drivers cannot be installed without it
	cd /
	chklinkkit

	# Turn off IPFORWARDING conformant with RFC 1122
	set_ipforwarding 0

	# Check for the presence of STREAMS in the kernel,
	# if they aren't there, install them
	addstreams

	# Configure kernel parameters
	set_kernel_params

	# Add drivers via idinstall
	add_drivers
	change="Y"

	# Set up the system node name
	getnodename
	# Save name in stune and /etc/systemid
	/etc/conf/bin/idtune -f NODE "\"$NODENAME\""
 	echo $NODENAME > /etc/systemid
	chmod 644 /etc/systemid

	# Get domainname (on the first time configuring TCP/IP)
	getdomname

	# Create /usr/adm/syslog file if it doesn't exist
	if [ ! -f /usr/adm/syslog ] ; then
		> /usr/adm/syslog
	fi

	# Addxuser if not already there
	add_xuser

fi	# End of if $CONFIGURE = "Y"

# 
# now check if a new driver is being installed 
# not in reconfig mode
#
if [ ! "$_reconfig" ] ; then
	netconfig -s 2>/dev/null | \
			/usr/bin/fgrep $LOWER_PRD 2>/dev/null | \
			/usr/bin/fgrep sco_tcp > /dev/null 2>&1
	if [ $? -eq 0 ] ; then
		cleanup $OK
	fi
fi

drv=`/bin/echo ${LOWER_PRD} | sed -e 's/[0-9]$//'`
#
# If the driver below supports multiple units via MACIOC_UNITSEL ioctl, the
# environment variable "UNITS" contains the # of units currently supported.
# If reconfiguring, suppress all messages about multiple unit support.  It
# is done by the reconf script.
#
[  ! "$_upgrade"  -a ! "$_no_prompt" -a ! "$_reconfig" -a "$LOWER_PRD" != "lo0" ] && [ ! "$UNITS" ] && message "Please enter the following information in order to configure $LOWER_PRD"

# we always reconfig 1 unit at a time, so, if reconfig, num_units is 1.
if [ "$UNITS" ] ; then
	if [ ! "$_reconfig" ] ; then
		num_units=$UNITS
		message "NOTE:\tThe $LOWER_PRD driver can support multiple units.  There are $UNITS unit(s)\n\tcurrently configured in $LOWER_PRD."
	else
		# We want to execute the while loop below, only once.
		num_units=`expr $UNIT_NUM + 1`
	fi
else
	num_units=1
fi

# If reconfiguring, and the driver supports multiple units, the unit #
# being re-configured is passed to us as an env variable.
if [ "$UNITS" -a "$_reconfig" ] ; then
	unit_num=$UNIT_NUM
else
	unit_num=0
fi

while [ $unit_num -lt $num_units ]
do
	case $LOWER_PRD in
	lo0)	check_hosts 127.0.0.1 "loopback"
		;;
	ppp*)	get_ppp_param
		if [ ! "$_upgrade" ] ; then
			if [ "$PPP_LOCAL_NAME" ] ; then
				check_hosts $PPP_HOST_IP $PPP_LOCAL_NAME
			else
				check_hosts $PPP_HOST_IP $NODENAME
			fi
		fi
		NETMASK="netmask $PPP_NM"
		update_ppphosts
		# line_state ppp enable PPP_TTY
		line_state_warning ppp enable
		;;
	sl*)	get_slip_param
		if [ ! "$_upgrade" ] ; then
			if [ "$SLIP_LOCAL_NAME" ] ; then
				check_hosts $SLIP_HOST_IP $SLIP_LOCAL_NAME
			else
				check_hosts $SLIP_HOST_IP $NODENAME
			fi
		fi
		update_sliphosts
		if [ "$_upgrade"  -o "$_no_prompt" ] ; then
			line_state_warning slip disable
		else
			line_state slip disable $SLIP_TTY
		fi
		;;
	*)	if [ ! "$_reconfig" -a "$UNITS" ] ; then
			message "\nInformation for unit # $unit_num:"
		fi
		get_ether_param
		if [ "$LOCAL_NAME" ] ; then
			check_hosts $IP $LOCAL_NAME
		else
			check_hosts $IP $NODENAME
		fi
		NETMASK="netmask $NM"
		BROADCAST="broadcast $BD"
		;;
	esac
	
	#
	# Determine node file
	#
	case $LOWER_PRD in
	lo0)
	      ;;
	
	ppp*) IFCONFIG="	ifconfig $LOWER_PRD $PPP_HOST_IP $PPP_DEST_IP -trailers $NETMASK"
	      OUTPUT="	ppplinkint \$1 $LOWER_PRD"
	      ;;
	
	sl*) IFCONFIG="	/etc/slattach $SLIP_TTY $SLIP_HOST_IP $SLIP_DEST_IP $SLIP_BAUD& #$LOWER_PRD"
	     ;;
	
	*)  NODE=$ND/$LOWER_PRD
	    case $LOWER_PRD in
	    e3A*)	IFCONFIG="	ifconfig $LOWER_PRD $IP onepacket 512 3 $FLAGS $NETMASK $BROADCAST"
			;;
	    *)	if [ "$UNITS" ] ; then
				IFCONFIG="	ifconfig $LOWER_PRD$unit_num $IP $FLAGS $NETMASK $BROADCAST"
			else
				IFCONFIG="	ifconfig $LOWER_PRD $IP $FLAGS $NETMASK $BROADCAST"
			fi
			;;
	     esac
	     #
	     # Determine whether nodes are cloning or not
	     #
	     if [ ! -f $NODE ] ; then
		Error "$LOWER_PRD is not installed on this system."
		Error "Please install the driver and re-run $0"
		cleanup $FAIL
	     fi
	
	     set -- `cat $NODE`
	     if [ $1 = "clone" ] ; then
		if [ "$UNITS" ] ; then
			OUTPUT="	uenet ip /dev/$2 $LOWER_PRD $unit_num"
		else
			OUTPUT="	cenetb ip /dev/$2 $LOWER_PRD"
		fi
	     else
		OUTPUT="	senet ip /dev/$2 /dev/$6 $LOWER_PRD"
	     fi
	     ;;
	esac
	
	#
	# Modify etc/strcf and etc/tcp
	#
	case $LOWER_PRD in
	lo0)	default_tcp
		;;
	
	ppp*)	ppp_modify
		;;
	
	sl*)	slip_modify
		;;
	
	*)	ether_modify
		;;
	esac
	
	# If this script is called a second time then the machine may be a
	# gateway. If so set IPFORWARDING & IPSENDREDIRECTS
	no_of_devs=`netconfig -s | grep "^sco_tcp" | grep -v "#lo0" | wc -l`
	
	#
	# If the lower driver supports multiple units (via MACUNITSEL), the
	# count for no_of_devs must be modified.
	#
	if [ "$UNITS" ] ; then
		# if we are re-configuring, netconfig -s will see our driver
		# in the chain and will have a value of greater than 0.
		if [ "$_reconfig" ] ; then
			if [ $no_of_devs -eq 1 ] ; then
			    #
			    # we're the only lower driver configured, and we
			    # support MACUNITSEL.  There may be more than one
			    # device configured and "UNITS" has that count.
			    # Since the check below is for num_of_devs gt 0,
			    # we need to count down UNITS by 1 (start from 0).
			    #
			    no_of_devs=$UNITS
			fi
		else
			if [ $no_of_devs -eq 0 ] ; then
			    # we're the first driver being configured, and
			    # since we support MACUNITSEL, multiple boards
			    # may be configured through us.  The unit_num
			    # gives the current count (it starts from 0).
			    #
			    no_of_devs=`expr $unit_num + 1`
			fi
		fi
	fi
	
	if [ $no_of_devs -ge 1 -o "$_reconfig" ] ; then
		getgateway
		if [ "$GATEWAY" = "Y" -o "$GATEWAY" = "y" ] ; then
			set_ipforwarding 1
		else
			set_ipforwarding 0
		fi
	fi
	
	#
	# if reconfiguring, we do this only for the unit_num specified in the
	# environment.  So, break from the while loop.
	#
	if [ "$_reconfig" ] ; then
		break
	else
		unit_num=`expr $unit_num + 1`
	fi

done	# while

# check to see how many psuedo terminals are configured.
# prompt user to see if they want to reconfigure them
# Interactive version chkptty, else set to default of N_PTTY with setptty
# Don't ask if we are only installing the loopback driver
if [ "$LOWER_PRD" != "lo0" ] ; then
	if [ "$_upgrade"  -o "$_no_prompt" ] ; then
		setptty
	else
		chkptty
	fi
fi

# check to see how many TCP connections are configured.
# prompt user to see if they want to reconfigure them
# Interactive version chktcpconns, else set to default of N_SOCKET with
# settcpconns.
if [ "$_upgrade"  -o "$_no_prompt" ] ; then
	settcpconns
else
	chktcpconns
fi

# Ensure that boot time start and stop scripts are linked to /etc/tcp
if [ ! -f /etc/rc0.d/K85tcp ] ; then
	ln /etc/tcp /etc/rc0.d/K85tcp
fi
if [ ! -f /etc/rc2.d/S85tcp ] ; then
	ln /etc/tcp /etc/rc2.d/S85tcp
fi

[ "$_reconfig" -o "$_upgrade" ] || message "\nTCP/IP Configuration Complete."

rm -f /usr/lib/netconfig/tmp/sco_tcp.conf	# A bit of cleanup work
rm -f /usr/lib/netconfig/tmp/sco_tcp.misc

# Ensure kernel is relinked by netconfig only if necessary
if [ "$change" = "Y" ] ; then
	cleanup $RELINK
else
	cleanup $OK
fi
