:
#      @(#)sco_nb	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_nb 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_nb$$
NBCONF=/etc/default/nbconf

# Some useful functions

##############################################################################
#
# Remove temp files and exit with the status passed as argument
#
cleanup() {
	trap '' 1 2 3 15
	[ -f "$tmp" ] && cp $tmp $NBCONF
	[ "$tmp" ] && rm -f $tmp*
	echo "\nRestored old configuration ... Exiting."
	exit $1
}

##############################################################################
#
# Prompt with mesg, return non-zero on q. Also allows setting -x and +x,
# shell escapes and the passing of default values
#
# Usage: prompt "message" [default]
# Argument is a quoted message
# Dependencies: expr
# Notes: returns 1 if the user answers q or Q, returns 0 otherwise
#	 if $quit is not set, it is set to "quit"
#	 $cmd is set to whatever is input by the user, and then can
#		subsequently be used in the shell script.
#
prompt() {
	_mesg=$1
	while	echo "${_mesg} or q to ${quit:=quit} [$2]: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		Q|q)	return 1					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is an second argument use it
			# as the default else loop until 'cmd' is set
			[ "$2" ] && { 
				cmd=$2
				return 0
			}
			: continue
			;;
		*)	unset _mesg
			return 0					;;
		esac
	done
}

##############################################################################
#
# Prompt for yes or no answer with message passed as argument - 
# returns 1 for no; 2 for quit
#
getyn() {
	while	echo "\n$* (y/n/q) \c">&2
	do	read yn rest
		case $yn in
			[yY]) return 0				;;
			[nN]) return 1				;;
			[qQ]) return 2				;;
		*)	echo "Please answer y,n or q" >&2	;;
		esac
	done
}

##############################################################################
#
# Edit the /etc/default/nbconf file
#
addnbconf() {
if [ -f "$NBCONF" ] ; then
	ed -s $NBCONF <<-EOF >/dev/null
	/NB_HOST=/
	d
	a
	$NB_HOST
	.
	/NB_SCOPE=/
	d
	a
	$NB_SCOPE
	.
	w
	q
	EOF
fi
}

##############################################################################
#
# main()
#
trap "" 1 2 3 15

if [ -z "$LOWER_PRD" ] ; then
	exit $OK
fi
echo "\nTPI NetBIOS Reconfiguration of sco_nb->$LOWER_PRD"

# Save old configuration files - to be restored in an emergency!
cp /etc/default/nbconf $tmp 2>/dev/null
trap "cleanup $FAIL" 1 2 3 15

HOSTNAME=`grep NB_HOST= /etc/default/nbconf | sed -e 's/NB_HOST=//'`
SCOPE=`grep NB_SCOPE= /etc/default/nbconf | sed -e 's/NB_SCOPE=//'`

change=0
while [ $change -eq 0 ]
do
	echo "\nThe current configuration is shown below:\n"
	echo "NetBIOS hostname: 		$HOSTNAME"
	echo "NetBIOS scope identifier: 	$SCOPE\n"

	echo "Please enter the revised parameters:"
	prompt "Enter NetBIOS hostname" $HOSTNAME
	HOSTNAME=$cmd
	prompt "Enter NetBIOS scope identifier" $SCOPE
	SCOPE=$cmd
	getyn "Do you wish to change any of these parameters"
	change=$?
	[ $change -eq 2 ] && cleanup $FAIL
done
# Edit nbconf configuration file
NB_HOST="NB_HOST=$HOSTNAME"
NB_SCOPE="NB_SCOPE=$SCOPE"
addnbconf
# Cleanup
[ "$tmp" ] && rm -f $tmp*
exit $OK
