:
#      @(#)sco_tcp	5.4.1.2 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.
#
# 
# This script is for for reconfiguring sco_tcp using netconfig
#
: ${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

tmp=/tmp/sco_tcp$$

#
# Parameters used by the functions in tcp_nfs.comm
#
PRODUCT="TCP/IP"
PERMSLIST=/etc/perms/tcprt
LKPERM=/etc/perms/inst

#
# 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
# Some useful functions

##############################################################################
# Remove temp files and exit with the status passed as argument
#
cleanup3() {
	trap '' 1 2 3 15
	for i in /etc/tcp /etc/strcf /etc/default/tcp /etc/hosts /etc/ppphosts /etc/pppauth
	do
		[ -f $tmp$i ] && cp $tmp$i $i
	done
	[ "$tmp" ] && rm -r -f $tmp*
	rm -f /usr/lib/netconfig/tmp/sco_tcp.reconf
	rm -f /tmp/sco_tcp_reconf
	echo "\nRestored old configuration ... Exiting."
	exit $1
}

##############################################################################
# Reconfigure the loopback (pseudo) driver
#
reconfig_lo0() {
	NODENAME=`cat /etc/systemid`
	DOMAIN=`grep DOMAIN /etc/default/tcp | sed -e 's/DOMAIN=//'`
	hosts=`grep "[ 	]${NODENAME}[ 	]" /etc/hosts`
	if [ -n "$hosts" ] ; then
		IP=`echo $hosts | awk '{ print $1 }'`
	fi

	change=0
	while [ $change -eq 0 ]
	do
		echo "\nThe current configuration is shown below:\n"
		echo "Nodename:			$NODENAME"
		echo "Domain:				$DOMAIN\n"
		echo "Please enter the revised parameters:"
		# Get a complete set of new parameters
		prompt "Enter the system nodename" $NODENAME || cleanup3 $FAIL
		NODENAME=$cmd
		prompt "Enter the domain" $DOMAIN || cleanup3 $FAIL
		DOMAIN=$cmd
		getyn "Do you wish to change any of these parameters"
		change=$?
		[ $change -eq 2 ] && cleanup3 $FAIL
	done
	if [ -n "$hosts" ] ; then
		ed -s /etc/hosts <<-EOF >/dev/null
			/^$IP/
			d
			-1
			a
			$IP	$NODENAME ${NODENAME}.$DOMAIN
			.
			w
			q
			EOF
	fi

	GATEWAY=`grep IPFORWARDING /etc/conf/cf.d/stune | awk '{ print $2 }'`
	if [ "$GATEWAY" = "1" ] ; then
		GATEWAY="Y"
	else
		GATEWAY="N"
	fi
}

##############################################################################
# Reconfigure for ethernet style interface
#
reconfig_ether() {
	unit_num=$1
	if [ "$UNITS" ] ; then
	    old_config=`grep "$LOWER_PRD$unit_num" /etc/tcp | fgrep "ifconfig"`
	    old_strcf=`grep "$LOWER_PRD $unit_num" /etc/strcf | fgrep -v "^#"`
	else
	    old_config=`grep "$LOWER_PRD" /etc/tcp | fgrep "ifconfig"`
	    old_strcf=`grep "$LOWER_PRD" /etc/strcf | fgrep -v "^#"`
	fi
	if [ -z "$old_config" -o -z "$old_strcf" ] ; then
		echo "\nError: Cannot find $LOWER_PRD in configuration files.\n"
		cleanup3 $FAIL
	fi
	IP=`echo $old_config | awk '{ print $3 }'`
	if [ -z "$IP" ] ; then
		echo "\nError: Could not find host IP address.\n"
		cleanup3 $FAIL
	fi
	hosts=`grep "^${IP}[ 	]" /etc/hosts | head -1`
	if [ -z "$hosts" ] ; then
		hosts="$IP `cat /etc/systemid`"
	fi
	NM=`expr "$old_config" : '.*netmask[ 	]*\([^ 	]*\)'`
	BD=`expr "$old_config" : '.*broadcast[ 	]*\([^ 	]*\)'`
	LOCAL_NAME=`echo $hosts | awk '{ print $2 }'`
	[ -z "$LOCAL_NAME" ] && LOCAL_NAME=`cat /etc/systemid`
	DOMAIN=`grep DOMAIN /etc/default/tcp | sed -e 's/DOMAIN=//'`

	# Remove all occurances of driver in configuration files
	grep -v "^$old_config" "$tmp/etc/tcp" > /etc/tcp
	grep -v "^$old_strcf" $tmp/etc/strcf > /etc/strcf
	grep -v "^$hosts" $tmp/etc/hosts > /etc/hosts

	change=0
	while [ $change -eq 0 ]
	do
		if [ "$UNITS" ] ; then
			echo "\nThe current configuration for unit #$unit_num is shown below:\n"
		else
			echo "\nThe current configuration is shown below:\n"
		fi
		echo "Interface Address:		$IP"
		echo "Netmask:  			$NM"
		echo "Broadcast Address:		$BD"
		echo "Local host name:		$LOCAL_NAME\n"

		echo "Please enter the revised parameters:"
		# Get a complete set of new parameters
		/usr/lib/netconfig/bin/tcpcfg -t -I $IP -N $NM # -B $BD
			IP=
			NM=
			BD=

		set -- `cat /usr/lib/netconfig/tmp/sco_tcp.conf`
			IP=$1
			NM=$2
			BD=$3

		prompt "Enter local name for this interface" "$LOCAL_NAME"
		if [ $? -eq 0 ] ; then
			LOCAL_NAME=$cmd
		else
			LOCAL_NAME=
		fi

		is_gateway $unit_num
		getyn "Do you wish to change any of these parameters"
		change=$?
		[ $change -eq 2 ] && cleanup3 $FAIL
	done
}

##############################################################################
# Reconfigure for ppp interface
#
reconfig_ppp() {
	old_config=`grep "$LOWER_PRD" /etc/tcp | fgrep "ifconfig"`
	old_strcf=`grep "$LOWER_PRD" /etc/strcf | grep -v "^#"`
	if [ -z "$old_config" -o -z "$old_strcf" ] ; then
		echo "\nError: Cannot find $LOWER_PRD in configuration files.\n"
		cleanup3 $FAIL
	fi
	PPP_HOST_IP=`echo $old_config | awk '{ print $3 }'`
	if [ -z "$PPP_HOST_IP" ] ; then
		echo "\nError: Could not find host IP address.\n"
		cleanup3 $FAIL
	fi
	PPP_DEST_IP=`echo $old_config | awk '{ print $4 }'`
	if [ -z "$PPP_DEST_IP" ] ; then
		echo "\nError: Could not find destination IP address.\n"
		cleanup3 $FAIL
	fi

	ppp_hosts=`grep "^$PPP_DEST_IP[ 	]" /etc/ppphosts | grep -v "^#"`
	[ -n "$ppp_hosts" ] && {
		PPP_DEST_NAME=`echo $ppp_hosts | awk '{ print $3 }'`
		[ -z "$PPP_DEST_NAME" ] && PPP_DEST_NAME=`cat /etc/systemid`
	}

	hosts=`grep "^${PPP_HOST_IP}[ 	]" /etc/hosts | grep -v "^#" | head -1`
	[ -n "$hosts" ] && {
		PPP_LOCAL_NAME=`echo $hosts | awk '{ print $2 }'`
		[ -z "$PPP_LOCAL_NAME" ] && PPP_LOCAL_NAME=`cat /etc/systemid`
	}

	PPP_PAP=`echo $ppp_hosts | awk '{ print $4 }'`
	[ "$PPP_PAP" != "pap" ] && PPP_PAP=""
	PPP_NM=`expr "$old_config" : '.*netmask[ 	]*\([^ 	]*\)'`

	[ -z "$hosts" ] && hosts="${PPP_HOST_IP}[ 	][ 	]*${PPP_LOCAL_NAME}"
	[ -z "$ppp_hosts" ] && ppp_hosts="${PPP_DEST_IP}[ 	][ 	]*${PPP_DEST_NAME}"

	DOMAIN=`grep DOMAIN /etc/default/tcp | sed -e 's/DOMAIN=//'`

	# Remove all occurances of driver in configuration files
	grep -v "^$old_config" "$tmp/etc/tcp" > /etc/tcp
	grep -v "^$old_strcf" $tmp/etc/strcf > /etc/strcf
	grep -v "^$ppp_hosts" $tmp/etc/ppphosts > /etc/ppphosts
	grep -v "^$hosts" $tmp/etc/hosts > /etc/hosts

	change=0
	while [ $change -eq 0 ]
	do
		echo "\nThe current configuration is shown below:\n"
		echo "PPP Source Address:		$PPP_HOST_IP"
		echo "PPP Netmask:			$PPP_NM"
		echo "PPP Destination Address: 	$PPP_DEST_IP"
		echo "PPP Destination Name:		$PPP_DEST_NAME"
		echo "PPP Local Name:			$PPP_LOCAL_NAME"
		echo "PAP Enabled:			\c"
		if [ "$PPP_PAP" = "pap" ] ; then
			echo "Yes\n"
			_default="y"
		else
			echo "No\n"
			_default="n"
		fi

		echo "Please enter the revised parameters:"
		# Get a complete set of new parameters
		/usr/lib/netconfig/bin/tcpcfg -p -I $PPP_HOST_IP -D $PPP_DEST_IP -H "$PPP_DEST_NAME" -N "$PPP_NM"
			PPP_HOST_IP=
			PPP_DEST_IP=
			PPP_NM=
			PPP_DEST_NAME=

		set -- `cat /usr/lib/netconfig/tmp/sco_tcp.conf`
			PPP_HOST_IP=$1
			PPP_DEST_IP=$2
			PPP_NM=$3
			PPP_DEST_NAME=$4

		prompt "Enter local name for ppp connection" "$PPP_LOCAL_NAME"
		if [ $? -eq 0 ] ; then
			PPP_LOCAL_NAME=$cmd
		else
			PPP_LOCAL_NAME=
		fi

		PPP_PAP=""
		getyn "Do you want PAP (Password Authentication) configured?" $_default
		[ $? -eq 0 ] && {
			PPP_PAP="pap"
			echo "Note: You must edit /etc/pppauth manually to change PAP password."
		}

		is_gateway
		getyn "Do you wish to change any of these parameters"
		change=$?
		[ $change -eq 2 ] && cleanup3 $FAIL
	done
}

##############################################################################
# Reconfigure for slip interface
#
reconfig_slip() {
	old_slattach=`grep '^[ 	]*/etc/slattach' /etc/tcp | fgrep $LOWER_PRD`
	if [ -z "$old_slattach" ] ; then
		echo "\nError: Cannot find $LOWER_PRD in configuration files.\n"
		cleanup3 $FAIL
	fi
	SLIP_DEST_IP=`echo $old_slattach | awk '{ print $4 }'`
	[ -z "$SLIP_DEST_IP" ] || {
		rem_hosts=`grep "^${SLIP_DEST_IP}[ 	]" /etc/hosts | grep -v "^#" | head -1`
	}
	SLIP_HOST_IP=`echo $old_slattach | awk '{ print $3 }'`
	SLIP_BAUD=`echo $old_slattach | awk '{ print $5 }' | sed -e 's/&//'`
	SLIP_TTY=`echo $old_slattach | awk '{ print $2 }'`
	SLIP_DEST_NAME=`echo $rem_hosts | awk '{ print $2 }'`

	hosts=`grep "^${SLIP_HOST_IP}[ 	]" /etc/hosts | grep -v "^#" | head -1`
	[ -n "$hosts" ] && {
		SLIP_LOCAL_NAME=`echo $hosts | awk '{ print $2 }'`
		[ -z "$SLIP_LOCAL_NAME" ] && SLIP_LOCAL_NAME=`cat /etc/systemid`
	}

	[ -z "$hosts" ] && hosts="${SLIP_HOST_IP}[ 	][ 	]*${SLIP_LOCAL_NAME}"
	[ -z "$rem_hosts" ] && ppp_hosts="${SLIP_DEST_IP}[ 	][ 	]*${SLIP_DEST_NAME}"

	DOMAIN=`grep DOMAIN /etc/default/tcp | sed -e 's/DOMAIN=//'`

	# Remove all occurances of driver in configuration files
	grep -v "$old_slattach" "$tmp/etc/tcp" > /etc/tcp
	grep -v "^$hosts|^$rem_hosts" $tmp/etc/hosts > /etc/hosts

	change=0
	while [ $change -eq 0 ]
	do
		echo "\nThe current configuration is shown below:\n"
		echo "TTY line:			$SLIP_TTY"
		echo "Baud rate:			$SLIP_BAUD"
		echo "Slip Source Address:		$SLIP_HOST_IP"
		echo "Slip Destination Address: 	$SLIP_DEST_IP"
		echo "Slip Destination Name:		$SLIP_DEST_NAME"
		echo "Slip Local Name:		$SLIP_LOCAL_NAME\n"

		echo "Please enter the revised parameters:"
		# Get a complete set of new parameters
		prompt "Enter tty line to be used for slip" $SLIP_TTY
		SLIP_TTY=$cmd
		prompt "Enter baud rate for $SLIP_TTY" $SLIP_BAUD
		SLIP_BAUD=$cmd
		/usr/lib/netconfig/bin/tcpcfg -s -I $SLIP_HOST_IP -D $SLIP_DEST_IP -H "$SLIP_DEST_NAME"
			SLIP_HOST_IP=
			SLIP_DEST_IP=
			SLIP_DEST_NAME=

		set -- `cat /usr/lib/netconfig/tmp/sco_tcp.conf`
			SLIP_HOST_IP=$1
			SLIP_DEST_IP=$2
			SLIP_DEST_NAME=$3

		prompt "Enter local name for slip connection" "$SLIP_LOCAL_NAME"
		if [ $? -eq 0 ] ; then
			SLIP_LOCAL_NAME=$cmd
		else
			SLIP_LOCAL_NAME=
		fi

		is_gateway
		getyn "Do you wish to change any of these parameters"
		change=$?
		[ $change -eq 2 ] && cleanup3 $FAIL
	done
}

##############################################################################
# Question user on whether machine is going to be a gateway if more than one
# driver is installed
#
is_gateway() {
	unit_num=$1
	GATEWAY=`grep IPFORWARDING /etc/conf/cf.d/stune | awk '{ print $2 }'`
	if [ "$GATEWAY" = "1" ] ; then
		GATEWAY="y"
	else
		GATEWAY="n"
	fi

	no_of_devs=`netconfig -s | grep "^sco_tcp" | grep -v "#lo0" | wc -l`
	#
	# if the lower interface supports unitselect, multiple instances
	# of the driver will not be reflected in netconfig -s
	#
	if [ $no_of_devs -lt 2 -a "$UNITS" ] ; then
		no_of_devs=`expr $unit_num + 1`		# unit_num starts at 0
	fi
	if [ $no_of_devs -lt 2 ] ; then
		return 0
	fi

	prompt "Is the machine to be used as a gateway (y or n)?" $GATEWAY || cleanup3 $FAIL
	if [ "$cmd" = "Y" -o "$cmd" = "y" ] ; then
		GATEWAY="Y"
	else
		GATEWAY="N"
	fi
	return 0
}

##############################################################################
# Save these new parameters in /usr/lib/netconfig/tmp/sco_tcp.reconf
#
save_params() {
	rm -f /usr/lib/netconfig/tmp/sco_tcp.reconf
	VAR_LIST="IP NM BD LOCAL_NAME NODENAME GATEWAY DOMAIN SLIP_HOST_IP SLIP_DEST_IP SLIP_BAUD SLIP_TTY SLIP_DEST_NAME SLIP_LOCAL_NAME PPP_HOST_IP PPP_DEST_IP PPP_NM PPP_DEST_NAME PPP_LOCAL_NAME PPP_PAP"
	if [ "$UNITS" ] ; then
		UNIT_NUM=$1
		VAR_LIST="$VAR_LIST UNITS UNIT_NUM"
	fi
	for i in $VAR_LIST
	do
		echo "$i=`eval echo "$"$i`" >> /usr/lib/netconfig/tmp/sco_tcp.reconf
	done
}

##############################################################################
# Tidy up
#
tidyup() {
	[ "$tmp" ] && rm -r -f $tmp*
	rm -f /usr/lib/netconfig/tmp/sco_tcp.reconf
	rm -f /tmp/sco_tcp_reconf
	echo "\nTCP/IP Reconfiguration Complete."
}

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

if [ -z "$LOWER_PRD" -o "$LOWER_PRD" = "-" ] ; then
	exit $OK
fi
echo "\nTCP/IP Reconfiguration of sco_tcp->$LOWER_PRD"

# Save old configuration files - to be restored in an emergency!
trap "" 1 2 3 15
for i in /etc/tcp /etc/strcf /etc/default/tcp /etc/hosts /etc/ppphosts /etc/pppauth
do
	dir=`dirname $i`
	[ -d "$tmp$dir" ] || mkdir -p $tmp$dir
	[ -f $i ] && safecopy $i $tmp$i
done
trap "cleanup3 $FAIL" 1 2 3 15

# Get old parameters and remove entries from current configuration files
case $LOWER_PRD in
lo0)	reconfig_lo0
	echo $NODENAME > /etc/systemid	# Reset system nodename. This may
	chmod 644 /etc/systemid		# not match name in link kit
	;;
ppp*)	reconfig_ppp
	;;
sl*)	reconfig_slip
	;;
*)	#
	# if the driver supports UNITSEL, set the UNITS global variable
	#
	num_units=`grep "$LOWER_PRD" /etc/strcf 2>/dev/null | grep uenet 2>/dev/null | wc -l`
	num_units=`expr $num_units`	# remove leading tabs
	if [ $num_units -gt 0 ] ; then
		UNITS=$num_units
		echo "\nNOTE: The $LOWER_PRD driver supports multiple units."
		echo "      $num_units unit(s) are currently configured in the system."
	else
		# The lower interface doesn't support UNITSEL.  So, it's
		# probably configured using cenet.  Let reconfig_ether()
		# decide what to do.
		UNITS=
		num_units=1
	fi

	# Configure each unit seperately
	unit_num=0
	while [ $unit_num -lt $num_units ]
	do
		reconfig_ether $unit_num
		# Save new params in /usr/lib/netconfig/tmp/sco_tcp.reconfig
		save_params $unit_num

		# Invoke netconfig/init/sco_tcp script to configure in changes
		> /tmp/sco_tcp_reconf	# So that init script knows its a reconf
		echo "\nUpdating configuration files ..."
		sh /usr/lib/netconfig/init/sco_tcp $LOWER_PRD $LOWER_IF - - N
		case $? in
			$FAIL) cleanup3 $FAIL
				;;
			$RELINK) relink="TRUE"
				;;
		esac
		unit_num=`expr $unit_num + 1`
	done
	tidyup
	if [ "$relink" ] ; then
		exit $RELINK
	else
		exit $OK	# Ensure kernel is relinked if necessary
	fi
	# not reached
	;;
esac

# Save these new parameters in /usr/lib/netconfig/tmp/sco_tcp.reconf
save_params

# Invoke /usr/lib/netconfig/init/sco_tcp script to configure in changes
> /tmp/sco_tcp_reconf	# So that init script knows its a reconfiguration
echo "\nUpdating configuration files ..."
sh /usr/lib/netconfig/init/sco_tcp $LOWER_PRD - - - N
result=$?
if [ $result -eq 1 ] ; then
	cleanup3 $FAIL
fi
tidyup
exit $result		# Ensure kernel is relinked if necessary
