#	
#   Copyright (C) 1993, The Santa Cruz Operation, Inc.
#   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
#   The Santa Cruz Operation, Inc..  This source code may not be
#   copied, disclosed, distributed, demonstrated or licensed except
#   as expressly authorized by The Santa Cruz Operation, Inc..
#
#   SCCSID: @(#) sco_ipx 4.2 92/09/17  (SCO)
#
: ${OK=0} ${FAIL=1} ${RELINK=2}
PATH=/bin:/usr/bin:/etc:/etc/conf/bin
ROOT=/
DIST=/usr/lib/ipxrt
NPSCONFIG=/etc/ipx.d/NPSConfig

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.
#------------------------------------------------------------------------

tmp=/tmp/sco_ipx$$
ND=/etc/conf/node.d
change="N"

# Initialise configuration variables
NODENAME=`cat /etc/systemid 2>/dev/null`


##############################################################################
#
# Remove temp files and exit with the status passed as argument
#
cleanup() {
	trap '' 1 2 3 15
	if [ $1 -eq $FAIL ]; then
		for i in $NPSCONFIG; do
			[ -f $tmp$i ] && cp $tmp$i $i
		done
		echo "\nRestored old configuration ... Exiting."
	fi
	[ "$tmp" ] && rm -r -f $tmp*
	exit $1
}

##############################################################################
#
# Remove temp files and exit with the status passed as argument
#
cleanup2() {
	trap '' 1 2 3 15
	message "\nConfiguration exiting ..."
	message "Removing drivers from link kit ... \c"
	for drv in $ipxdrivers
	do
		/etc/conf/bin/idinstall -d -e $drv
	done
	message "done."
	cleanup $1
}

##############################################################################
#
# Test to see if link kit is installed
#
chklinkkit() {
	until	fixperm -i -d LINK $LKPERM 2> /dev/null
	do
		case $? in
		3|4) Error "The Link Kit is not installed."
		     ;;
		5)   warning "The Link Kit is only partially installed.\nAttempting to install IPX/SPX anyway."
		     return 0
		     ;;
		*)   Error "Error testing for Link Kit. Exiting."
		     cleanup $FAIL
		     ;;
		esac
		# If in a no prompt mode then exit with error
		if [ "$_upgrade" -o "$_no_prompt" ] ; then
			cleanup $FAIL
		fi
		# Not fully installed. Do so here
		while	echo "Do you wish to install it now? (y/n) \c"
		do	read ANSWER
			case $ANSWER in
			Y|y)	custom -o -i LINK
				break
				;;
			N|n)	echo "Drivers cannot be installed without the Link Kit."
				cleanup $FAIL
				;;
			*)	echo "Please answer 'y' or 'n'. \c"
				;;
			esac
		done
	done
}

##############################################################################
#
# This routine checks to see if streams are currently configured in system
# files. It returns 0 if driver is installed.
#
strchk() {
	PWD=`pwd`
	cd /etc/conf/cf.d
	if [ -f ../sdevice.d/$name ] 
	then
		fieldval=`awk '{print $2}' ../sdevice.d/$name` || {
			Error "awk failed to read sdevice file."
			cleanup $FAIL
		}
		[ "$fieldval" = "Y" ] && return 0
	else
		Error "The $name device has no sdevice file.\nCannot install IPX/SPX without this file."
		cleanup2 $FAIL
	fi
	cd $PWD
	return 1
}

##############################################################################
#
# Add streams drivers 
#
stradd() {
	PWD=`pwd`
	cd /etc/conf/sdevice.d
	sed "/	N/s/N/Y/" $name > /tmp/sdevice.$name || { 
		Error "sed unable to edit /etc/conf/sdevice.d/$name."
		cd $PWD
		return 1
	}
	mv /tmp/sdevice.$name $name
	cd $PWD
	return 0
}

##############################################################################
#
# Add streams to kernel
#
addstreams() {
	# name MUST be 6 characters or shorter (long file names)
	for name in "str" "clone" "log" "ptm" "pts" "ldterm" "ptem" "timod" \
		    "tirdwr" "sp" 
	do
		strchk || stradd || cleanup2 $FAIL
	done
#	message "\nStreams modules have been successfully added to the kernel."
}

##############################################################################
#
# Streams resources adjustment
# NOTE: the use of both the -m and -f option does not work under 3.2.0
#
streamconfig() {
	# If RVALUE is a delta change to a parameter, call changeparm()
	# to add or subtract this value, else call idtune to simple
	# up the value if necessary. This allows stream parameters to
	# reduced if IPX/SPX is deconfigured
	PARAM_LIST="
		NINODE		150
		NS5INODE	150
		NFILE		150

		NQUEUE		512
		NSTREAM		128

		NBLK4096	4
		NBLK2048	8	
		NBLK1024	+32
		NBLK512		+64
		NBLK256		+128
		NBLK128		+256
		NBLK64		+512
		NBLK16		+256
		NBLK4		+256
				
		NMUXLINK	64
		MAXSEPGCNT	1
		NUMTIM		32
		NUMTRW		32
	"

	message "Changing Streams resources needed for IPX/SPX ... \c"
	set -- $PARAM_LIST
	while [ "$2" ]; do
		PNAME=$1
		RVALUE=$2
		delta=`echo $RVALUE | awk '{ print substr($1,1,1) }'`
		if [ "$delta" = "+" -o "$delta" = "-" ] ; then
			RVALUE=`echo $RVALUE | sed -e 's/+//'`
			changeparm $PNAME $RVALUE
		else
			/etc/conf/bin/idtune -m -f $PNAME $RVALUE
		fi
		shift
		shift
	done
	unset PARAM_LIST PNAME RVALUE
	message "done."
}

##############################################################################
#
# Calls: configure expr
#
# Purpose: Change the value of kernel parameters
#
changeparm () {
	_currdir=`pwd`
	_name=$1
	_value=$2
	cd /etc/conf/cf.d

	curval=`./configure -y $_name | tail -1`
	[ -z "$curval" ] && {
		cd $_currdir
		return
	   }

	curval=`./configure -y $_name | tail -1` || {
		cd $_currdir
		return $FAIL
	}
	curval=`expr $curval + $_value`
	[ "$curval" -ge "0" ] && {
	    set -- `grep $_name mtune`
	    [ "$curval" -gt "$4" ] && curval=$4
	    /etc/conf/bin/idtune -f $_name $curval
	}
	cd $_currdir
	return $OK
}


##############################################################################
#
# Add drivers via idinstall
#
add_drivers() {
	message "Installing drivers into link kit ... \c"
	for drv in $ipxdrivers
	do
		message "$drv \c"
		cd $DIST/ID/$drv
		/etc/conf/bin/idcheck -p $drv
		if [ $? -lt 16 ]
		then
			/etc/conf/bin/idinstall -a -e -k $drv
		else
			/etc/conf/bin/idinstall -u -e -k $drv
		fi
		# copy any include files
		[ "`/bin/ls *.h 2> /dev/null`" ] && cp *.h /etc/conf/pack.d/$drv
		# Ensure driver is switched on
		sed "/	N/s/N/Y/" /etc/conf/sdevice.d/$drv >$tmp || {
			Error "Unable to edit /etc/conf/sdevice.d/${drv}."
			cleanup2 $FAIL
		}
		mv $tmp /etc/conf/sdevice.d/$drv
	done
	message "done."
}

##############################################################################
#
#	install ipx files
#
install_files() {

	cd $DIST
	echo "Copying files ... \c"
	find $ipxfiles -type f -print | cpio -plumd $ROOT 2>&1 | sed '/blocks$/d'
	find $ipxfiles -type d -exec chmod 755 '{}' ';'
	echo "done."

	return 0
}

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

# First and foremost, lets source in the relevant function definitions.
. /usr/lib/netconfig/bin/sco_ipx.fn

_upgrade=
if [ -f /tmp/system_upgrade ] && [ -f /usr/lib/netconfig/tmp/sco_ipx.config ]
then
	 _upgrade=TRUE
	. /usr/lib/netconfig/bin/sco_ipx.up
else
	. /usr/lib/netconfig/bin/sco_ipx.ui
fi

# Check to see if chain is already configured, if so do nothing unless reconfig
netconfig -s | grep "sco_ipx#$LOWER_PRD" > /dev/null 2>&1 && cleanup $OK

message "Installing SCO IPX/SPX over $LOWER_PRD\n"

serdrv=$LOWER_PRD

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

	# Install the ipxrt stuff
	LKPERM=/etc/perms/inst			# OS (link kit) permlist
	pkg="ISPXRT"					# permlist package name
	set="SCO IPX/SPX Runtime"		# appears in prompts
	ipxdrivers="ipx ipxe nvt spx xecho"
	ipxfiles="etc usr/bin"

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

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

	# Configure streams resources
	streamconfig

	# Add drivers via idinstall
	add_drivers
	change="Y"

	# Copy files
	install_files

	# modify etc/ipx.d/NPSConfig
	remove_all_lan_info
	enter_conf_info

else	# end of if $CONFIGURE = "Y"

	# Save old configuration files - to be restored in an emergency!
	trap "" 1 2 3 15
	for i in $NPSCONFIG; do
		dir=`dirname $i`
		[ -d "$tmp$dir" ] || mkdir -p $tmp$dir
		[ -f $i ] && safecopy $i $tmp$i
	done
	trap "cleanup $FAIL" 1 2 3 15

	# modify etc/ipx.d/NPSConfig
	enter_nvt_max_logins `get_nvt_max_logins`
fi

# check if maximum number of IPX/SPX chains exceeded
set -- `netconfig -s | fgrep sco_ipx | wc`
if [ "$2" -gt 7 ]; then
	Error "More IPX/SPX chains not supported".
	stop_screen
	cleanup $FAIL
fi

# Determine node file
NODE=$ND/$LOWER_PRD
if [ ! -f $NODE ] ; then
	Error "$LOWER_PRD is not installed on this system."
	Error "Please install the driver and re-run $0"
	stop_screen
	cleanup $FAIL
fi
ADAPTER=`get_adapter $NODE`

# Modify etc/ipx.d/NPSConfig
enter_lan_info . $ADAPTER . . . . . .

# configure pseudo terminals
chkptty -a 0 || change="Y"

# Ensure that boot time start and stop scripts are linked to /etc
[ ! -f /etc/rc2.d/S60nps ] && ln /etc/startnps /etc/rc2.d/S60nps
[ ! -f /etc/rc0.d/K60nps ] && ln /etc/stopnps /etc/rc0.d/K60nps

if [ "$_upgrade" ] ; then
	# We quit here in the upgrade case.  A kernel relink
	# will happen automagically.
	cleanup $OK
fi

# Ensure kernel is relinked by netconfig only if necessary
if [ "$change" != "N" ] ; then
	message "\nIPX/SPX Configuration Complete."
	stop_screen
	cleanup $RELINK
else
	message "\nYou must restart IPX/SPX to activate the changes."
	stop_screen
	cleanup $OK
fi
