#	
#   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 remove device from osi configuration file. 
#------------------------------------------------------------------------

tmp=/tmp/sco_isxp$$
LKPERM=/etc/perms/inst			# OS (link kit) permlist
ND=/etc/conf/node.d
change="N"
ipxdrivers="ipx ipxe nvt spx xecho"
ipxfiles="etc usr/bin"

# Function Definitions

##############################################################################
#
# Remove temp files and exit with the status passed as argument
#
cleanup() {
	trap '' 1 2 3 15
	[ "$tmp" ] && rm -f $tmp*
	exit $1
}

##############################################################################
#
# Test to see if link kit is installed
#
chklinkkit() {
	cd /
	fixperm -i -d LINK $LKPERM 2> /dev/null
	case $? in
	0)	return 0
		;;
	3|4)	echo "The Link Kit is not installed." >&2
		cleanup $FAIL
		;;
	5)	echo "The Link Kit is only partially installed." >&2
		echo "Attempting to remove IPX/SPX package anyway." >&2
		return 0
		;;
	*)	echo "Error testing for Link Kit. Exiting."
		cleanup $FAIL
		;;
	esac
}

##############################################################################
#
# Restore the streams parameters to their previous values
#
restore_str() {
	PARAM_LIST="
		NBLK4096	-0
		NBLK2048	-0
		NBLK1024	-32
		NBLK512		-64
		NBLK256		-128
		NBLK128		-256
		NBLK64		-512
		NBLK16		-256
		NBLK4		-256
	"

	set -- $PARAM_LIST
	while [ "$2" ]; do
		PNAME=$1
		RVALUE=$2
		changeparm $PNAME $RVALUE
		shift
		shift
	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
}

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

cd /

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

if [ "$CONFIGURE" = "N" ]; then

	# Determine node file
	NODE=$ND/$LOWER_PRD
	if [ ! -f $NODE ] ; then
		Error "$LOWER_PRD is not installed on this system."
		cleanup $FAIL
	fi
	ADAPTER=`get_adapter $NODE`
	[ $? -eq 0 ] || cleanup $FAIL

	# Modify /etc/ipx.d/NPSConfig
	remove_lan_info $ADAPTER
	enter_nvt_max_logins `get_nvt_max_logins`

	# configure pseudo terminals
	if [ -t 0 ]
	then
		chkptty -d `get_nvt_max_logins` || change="Y"
	fi

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

chklinkkit

#
# Stop ipx
#
echo "Stopping IPX/SPX ... \c"
/etc/stopnps 2>/dev/null 1>&2
echo done.

#
# remove drivers
#
echo "Removing the IPX/SPX drivers ... \c"
for drv in $ipxdrivers; do
	echo "$drv \c"
	rm -f /etc/conf/pack.d/$drv/*.h
	idinstall -d -e $drv 2>/dev/null 1>&2
done
echo "done."

#
# remove files
#
cd $DIST
echo "Removing files ... \c"
find $ipxfiles -type f -print | while read f; do
	/bin/rm -f ${ROOT}$f
done
/bin/rm -rf ${ROOT}etc/ipx.d ${ROOT}usr/bin/ipx
echo "done."

#
# Alter streams parameters
#
echo "Restoring streams resources ... \c"
restore_str
echo "done."

# configure pseudo terminals unless running in noprompt mode
if [ -t 0 ]
then
	chkptty -d 0
fi

#
# Remove special files so that immediate re-install will work.
#
/bin/rm -f ${ROOT}dev/nvt0
/bin/rm -f ${ROOT}dev/nvt
/bin/rm -f ${ROOT}dev/ipx0
/bin/rm -f ${ROOT}dev/ipx
/bin/rm -f ${ROOT}dev/nspx
/bin/rm -f ${ROOT}dev/nspxd

#
# remove other random things
#
/bin/rm -f /etc/startnps /etc/rc2.d/S60nps
/bin/rm -f /etc/stopnps /etc/rc0.d/K60nps

message "\nAll IPX/SPX chains removed."

if [ -t 0 ]
then
	stop_screen
fi

cleanup $RELINK
