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

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

# Some useful functions

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

##############################################################################
#
# Reconfigure 
#
reconfig_ipx() {

	_adapter=$1

	while :; do
		echo "\nParameters common to all SCO IPX/SPX chains:"
		display_conf_info
		getyn "Do you wish to change any of these parameters"
		case $? in
		0)	echo "\nPlease enter the revised parameters."
			enter_conf_info `get_conf_info`;;
		1)	break;;
		*)	cleanup $FAIL;;
		esac
	done

	while :; do
		echo "\nParameters specific to the sco_ipx->$LOWER_PRD chain:"
		display_lan_info $_adapter
		if [ $? -ne 0 ]; then
			echo "\nError: Cannot find $_adapter in configuration files.\n"
			cleanup $FAIL
		fi
		getyn "Do you wish to change any of these parameters"
		case $? in
		0)	echo "\nPlease enter the revised parameters."
			enter_lan_info `get_lan_info $_adapter`;;
		1)	break;;
		*)	cleanup $FAIL;;
		esac
	done

	unset _adapter
	return 0
}

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

. /usr/lib/netconfig/bin/sco_ipx.fn
. /usr/lib/netconfig/bin/sco_ipx.ui

if [ -z "$LOWER_PRD" ] ; then
	cleanup $OK
fi
echo "\nIPX/SPX Reconfiguration of sco_ipx->$LOWER_PRD"

# 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

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

reconfig_ipx $ADAPTER || cleanup $FAIL

[ "$tmp" ] && rm -r -f $tmp*

# configure pseudo terminals
chkptty -x `get_nvt_max_logins` || change="Y"

message "\nIPX/SPX Reconfiguration Complete."

# 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
