:
#      @(#)sco_nb	5.4.1.5 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.
#
#
: ${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/netbios$$
NBCONF=/etc/default/nbconf
NBSTART=/etc/rc2.d/S86netbios
NBSTOP=/etc/rc0.d/K72netbios
SDEVICE=/etc/conf/sdevice.d/nb

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

}

##############################################################################
#
# Remove driver from link kit then cleanup
#
cleanup2() {
	trap '' 1 2 3 15
	/etc/conf/bin/idinstall -d -e nb
	/etc/conf/bin/idinstall -d -e nbcots
	/etc/conf/bin/idinstall -d -e nbclts
	cleanup $1
}

##############################################################################
#
# Print an error message
#
Error() {
	echo "Error: $*" >&2
}

##############################################################################
#
# Print an message
#
message() {
	echo "$*"
}

##############################################################################
#
# Prompt with mesg, return non-zero on q. Also allows setting -x and +x,
# shell escapes and the passing of default values
#
prompt() {
	mesg=$1
	while	echo "\n${mesg} or enter q to quit [$2]: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		-v)	# Print the values of requested variables
			read x
			for var in $x
			do
				eval echo \$$var
			done
			;;
		Q|q)	return $FAIL					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# Take the second argument as default
			cmd=$2
			return $OK
			: continue
			;;
		*)	return $OK					;;
		esac
	done
}

##############################################################################
#
# create the /etc/default/nbconf file
#
donbconf() {
	set -- `echo "$HOSTNAME $SCOPE"`
	grep -v "^[ 	]*export" $NBCONF | grep -v "^NB_HOST" | grep -v "^NB_SCOPE" >$tmp
	for i in NB_HOST NB_SCOPE
	do
		[ -z "$1" ] && echo "$i=" >>$tmp && continue
		echo "$i=$1" >>$tmp && shift
	done
	# Append export line
	EX_LIST=`sed '/^#/d;/^$/d;s/^\(.*\)=.*$/\1/' $tmp | tr "\012" " "`
	echo "export $EX_LIST" >>$tmp && mv $tmp $NBCONF
	rm -f $tmp
}

##############################################################################
#
# Check for driver already there, only add if not there
#
adddriver() {
	cd /usr/lib/tcprt/ID/nb
	/etc/conf/bin/idcheck -p nb
	if [ $? -lt 16 ]
	then
		/etc/conf/bin/idinstall -a -e -k nb
	else
		/etc/conf/bin/idinstall -u -e -k nb
	fi
	# Ensure driver is switched on
	sed "/	N/s/N/Y/" $SDEVICE > $tmp || {
		Error "Unable to edit $DEVICE"
		cleanup $FAIL
	}
	mv $tmp $SDEVICE

	cd /usr/lib/tcprt/ID/nbcots
	/etc/conf/bin/idcheck -p nbcots
	if [ $? -lt 16 ]
	then
		/etc/conf/bin/idinstall -a -e -k nbcots
	else
		/etc/conf/bin/idinstall -u -e -k nbcots
	fi
	# Ensure driver is switched on
	sed "/	N/s/N/Y/" /etc/conf/sdevice.d/nbcots > $tmp || {
		Error "Unable to edit /etc/conf/sdevice.d/nbcots"
		cleanup $FAIL
	}
	mv $tmp /etc/conf/sdevice.d/nbcots 

	cd /usr/lib/tcprt/ID/nbclts
	/etc/conf/bin/idcheck -p nbclts
	if [ $? -lt 16 ]
	then
		/etc/conf/bin/idinstall -a -e -k nbclts
	else
		/etc/conf/bin/idinstall -u -e -k nbclts
	fi
	# Ensure driver is switched on
	sed "/	N/s/N/Y/" /etc/conf/sdevice.d/nbclts > $tmp || {
		Error "Unable to edit /etc/conf/sdevice.d/nbclts"
		cleanup $FAIL
	}
	mv $tmp /etc/conf/sdevice.d/nbclts 
}

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

# First lets source in the relevant function definitions
#
# Functions required and variables to be set:
#	get_netbios_param()
#		- HOSTNAME = Hostname of system running NetBIOS
#		- SCOPE = NetBIOS Scope Identifier (eg sco.COM)
#
_upgrade=
_no_prompt=
_reconfig=
[ -f /tmp/system_upgrade ] && _upgrade=TRUE
[ -f /usr/lib/netconfig/tmp/sco_nb.np ] && _no_prompt=TRUE
[ -f /tmp/netbios_reconfig ] && _reconfig=TRUE
# Check that one or less mode flags are set
n_of_args=`echo "$_upgrade $_no_prompt $_reconfig" | wc -w`
if [ $n_of_args -gt 1 ] ; then
	message "Netconfig netbios init script is confused. Multiple modes."
	cleanup $FAIL
fi
if [ "$_upgrade" ] ; then
	# Source in function declarations for upgrade
get_netbios_params() {
	config_dir=/usr/lib/netconfig/tmp
	# Set HOSTNAME and SCOPE saved by custom init.netbios
	eval `cat $config_dir/sco_nb.params`
}
else if [ "$_no_prompt" ] ; then
	# Source in variable declarations + functions for ODT installation
	:
else if [ "$_reconfig" ] ; then
	# Source in variable declarations for reconfiguration
	:
else
	# I guess it must be interactive, so source in interactive functions

get_netbios_params() {
	HOSTNAME=`uname -n`
	set -- `cat /etc/systemid`
	HOSTNAME=${1:-$HOSTNAME}
	prompt "Please enter the network host name" "$HOSTNAME" || cleanup $FAIL
	HOSTNAME=$cmd
	SCOPE=
	prompt "Please enter the NetBIOS Scope Identifier" "$SCOPE" || cleanup $FAIL
	SCOPE=$cmd
}
fi ; fi ; fi	# fo, fo, fo, and off to work we go ...

[ "$_reconfig" ] || message "Configuring TPI NETBIOS over $LOWER_PRD\n"     

if [ "$CONFIGURE" = "Y" ] ; then
	# Create the /etc/default/nbconf file
	get_netbios_params
	donbconf

	# Add netbios driver to link kit
	adddriver
fi

# Ensure that boot time start and stop scripts are linked to /etc/netbios
if [ ! -f $NBSTOP ] ; then
	ln /etc/netbios $NBSTOP
fi
if [ ! -f $NBSTART ] ; then
	ln /etc/netbios $NBSTART
fi

[ "$_reconfig" ] || message "\nTPI NETBIOS Configuration Complete"

rm -f /usr/lib/netconfig/tmp/sco_nb.params # A bit of cleanup work
cleanup $RELINK	# Ensure that the kernel is relinked by netconfig
