:
#	@(#) lmxc 60.2 2/17/92
#
#	Copyright (C) The Santa Cruz Operation, 1986 - 1991.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation and is Confidential.
#
#	Netconfig init script for ODT fresh install _and_ upgrade of lmxc
#	Hopefully works as standalone as well.
#	Needs to:
#		1. Configure lmx drivers into UNIX
#		2. Call NetBIOS init script
#		3. Copy out /etc/rc files

CONFIG_SAVE=/tmp

# Log files
instlog=$CONFIG_SAVE/install.log
errlog=$CONFIG_SAVE/install.log

lmxdrivers="nfc_ xnet xncb xstr lbu lbc tpnc tpnd cor"
MAXCHAINS=2

# Define return values
: ${OK=0} ${FAIL=1} ${RELINK=2} ${STOP=10} ${HALT=11}
: ${TRUE=0} ${FALSE=1}
tmp=/tmp/$$

# Function definitions
########################

# ---------- STANDARD ROUTINES -------- These routines are common to scripts
#					requiring kernel relinking.

Error() {
	echo "lmxc netconfig error: $*" >&2
	echo "lmxc netconfig error: $*" >> $errlog
	sleep 3
}

# Define traps for critical and non critical code.
set_trap()  {	
	trap 'Error "\nInterrupted! Exiting ..."; cleanup 1' 1 2 3 15
}

unset_trap()  {
	trap '' 1 2 3 15
}
 
# Remove temp files and exit with the status passed as argument
cleanup() {
	trap '' 1 2 3 15
	rm -f $tmp*
	[ $1 -eq $OK -o $1 -eq $RELINK ] || {
		cd /usr/lib/lm
		[ -f ./stubs.c- ] && mv ./stubs.c- \
			/etc/conf/pack.d/nfc_/stubs.c-
		cd /etc/conf/pack.d/nfc_
		[ -f ./stubs.c- ] && mv ./stubs.c- ./stubs.c
	}
	exit $1
}

# clear the screen if it is supported
clearscr() {
	# check if the terminal environment is set up
	[ "$TERM" ] && clear 2> /dev/null
}

log() {
	echo "lmxc netconfig init: $*" >&2
	echo "lmxc netconfig init: $*" >> $instlog
}

# Prompt for yes or no answer - returns non-zero for no
#
# Usage: getyn "question"
# Argument is a quoted question
# Notes: getyn returns 0 if the answer is y or Y and 1 if
#	 the answer is n or N.
#
getyn() {
	while	echo "\n$* (y/n) \c" >&2
	do	read yn rest
		case $yn in
		[yY])	return 0 				;;
		[nN])	return 1				;;
		*)	echo "\nPlease answer y or n" >&2	;;
		esac
	done
}

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

	# What's this "-z" business?  When does configure return an empty string?
	# Why do we want to return if it does?
	#
	# 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
}


# Turn 'N' to 'Y'
configin () {
	drvr=$1
	currdir=`pwd`
	cd /etc/conf/sdevice.d

	# Check the status of the driver
	# If there is a 'Y' in the second field, the driver is configured
	drvr_in=`awk '{print $2}' ./$drvr > /dev/null` || {
		Error "Couldn't check status of $drvr sdevice."
		return $FAIL
	}
	[ "$drvr_in" = "Y" ] && {
		return $OK
	}
	# edit the sdevice file to use reflect the addition of a driver
	sed 's/	N/	Y/' $drvr > $tmp.$drvr || {
		Error "Edit of /etc/conf/sdevice.d/$drvr failed."
		return $FAIL
	}

	# copy the new sdevice entry we just created into place
	unset_trap
	mv $tmp.$drvr /etc/conf/sdevice.d/$drvr
	set_trap
	return $OK
}


##########
#
# addlmxdrvs ()
#
# Add drivers via idinstall
addlmxdrvs () {

	echo "Updating system configuration with LAN Manager drivers." >&2

	currdir=`pwd`
	# backup the stubs file that was in the nfc_ driver directory.
	# a zero length file iis created instead.  The zero length file
	# must exist so fixperm thinks the link kit is fully installed

	[ -d /etc/conf/pack.d/nfc_ ] || {
		Error "Filesystem directory '/etc/conf/pack.d/nfc_' missing.
Install from LINK KIT."
		return $FAIL
	}
	cd /etc/conf/pack.d/nfc_
	unset_trap
	mv ./stubs.c /usr/lib/lm/stubs.c- 2> /dev/null || {
		set_trap
		Error "Can't back up '/etc/conf/pack.d/nfc_/stubs.c'.
Install from LINK KIT."
		cd $currdir
		return $FAIL
	}
	set_trap
	cd $currdir

	for drv in $lmxdrivers
	do
		[ -d /usr/lib/lm/ID/$drv ] || {
			Error "$drv configuration directory not present.
Reinstall LAN Manager and then run 'netconfig.'"
			return $FAIL
		}
		cd /usr/lib/lm/ID/$drv
		/etc/conf/bin/idinstall -d $drv
		if [ -f Driver.[oa] -a -f Master ]
		then
			# My experimentation with idinstall showed the
			# existence of the directory to be the key to
			# whether to use -u or -a.
			if [ -d /etc/conf/pack.d/$drv ]
			then
				err=`/etc/conf/bin/idinstall -u -e -k $drv 2>&1`
			else
				err=`/etc/conf/bin/idinstall -a -e -k $drv 2>&1`
			fi
			[ $? -eq 0 ] || {
				echo $err >> $errlog
			Error "Reinstall LAN Manager and then run 'netconfig.'"
				cd $currdir
				return $FAIL
			}
		else
			[ -d /etc/conf/pack.d/$drv ] || mkdir /etc/conf/pack.d/$drv
			[ -f Master ] && cat Master >> /etc/conf/cf.d/mdevice
			[ -f Node ] && cp Node /etc/conf/node.d/$drv
			[ -f System ] && cp System /etc/conf/sdevice.d/$drv
			[ -f Mfsys ] && cp Mfsys /etc/conf/mfsys.d/$drv
			[ -f Sfsys ] && cp Sfsys /etc/conf/sfsys.d/$drv
			[ -f Driver.[oa] ] && cp Driver.[oa] /etc/conf/pack.d/$drv/Driver.o
			[ -f Space.c ] && cp Space.c /etc/conf/pack.d/$drv/space.c
		fi
		# kludge - I happen to know the node names are the
		# same as the directory names - I'm not going to look
		# in ./Node for the name(s) of the nodes in /dev.
		[ -f ./Node ] && rm -f /dev/$drv
		[ -f ./Mtune ] && {
			# edit the mtune
			awk '{print $1}' ./Mtune > $tmp.tune || {
			    Error "Cannot edit $drv Mtune."
			Error "Reinstall LAN Manager and then run 'netconfig.'"
			    cd $currdir
			    return $FAIL
			}
			fgrep -v -f $tmp.tune /etc/conf/cf.d/mtune > \
				                           $tmp.mtune || {
			    Error "Cannot backup /etc/conf/cf.d/mtune."
			    cd $currdir
			    return $FAIL
			}
			trap "mv $tmp.mtune /etc/conf/cf.d/mtune" 1 2 3 15
			cat $tmp.mtune ./Mtune > /etc/conf/cf.d/mtune
		}
	done
	mv /usr/lib/lm/stubs.c- /etc/conf/pack.d/nfc_/stubs.c-
	> /etc/conf/pack.d/nfc_/stubs.c

	# Up params
	echo "Increasing Parameters for LAN Manager drivers."
	changeparm NQUEUE 240
	changeparm NSTREAM 60
	changeparm NBLK16 40
	changeparm NBLK64 40
	changeparm NBLK128 40
	changeparm NBLK256 40
	changeparm NBLK512 40
	changeparm NBLK1024 16
	changeparm NBLK2048 16
	changeparm NBLK4096 6
	# server only:
	# changeparm NUMTIM 128
	# changeparm NUMTRW 128
	echo "y" | /etc/conf/bin/idtune -m NLBC 128 >/dev/null
	echo "y" | /etc/conf/bin/idtune -m NLBU 64 >/dev/null

	cd $currdir
	return $OK
}

##########
#
# execinit ()
#
# Arguments: $1=name of driver
#
# Execute the netbios driver init script
execinit () {
    initscript=$1
    _currdir=`pwd`
    cd /usr/lib/netbios
    [ -x ./${initscript}init ] || return $OK
    ./${initscript}init $2
    retval=$?
    case $retval in
	$OK) : ;;
	$STOP) cleanup $STOP ;;
	$HALT) Error "$initscript NetBIOS init script returned HALT." ;;
	$FAIL) Error "$initscript NetBIOS init script failed" ;;
    esac
    cd $_currdir
    return $retval
}

##########
#
# domultiact ()
#
# copy rc scripts to their homes
domultiact () {

    multi=
    if [ -f /tmp/system_upgrade ]
    then
	[ -d /etc/rc.old ] || return $OK
	cd /etc/rc.old
	[ -f rc0.d/K*lm* ] && multi=yes
    else
        getyn "\nDo you want to start the LMX $prdname every time you go multiuser" && multi=yes
    fi

    [ -z "$multi" ] && return $OK
    cd /usr/lib/lm
    for rcf in rc0.d/K66lmxc rc2.d/S91lmxc
    do
	cp ./$rcf /etc/$rcf
    done
    return $OK
}

# main
currdir=`pwd`

# Parse args
down_name=$1
down_int=$2
up=$3
up_int=$4
config_in=$5

prdname="Client"

[ "$down_int" = "do_ncb" ] || {
	Error "Down interface not supported."
	cleanup $FAIL
}

# Find netbios name of $down_name.  Currently only netbios transports
# are supported and we have to build this here space.c file.
# We need the NCBPREFIX from the info file.  I guess we have to
# look through the info files until we find the one with NAME=$down_name,
# and then grab its NCBPREFIX.
#
# What's more, in the generic case, this might not be the
# first transport configured under us and we need to know them
# all to tell netconfigure.  We can find the earlier transports
# in netconfig/chains, lm/chains, or netbios/nb.list.

currdir=`pwd`
cd /usr/lib/netconfig/info
[ -r $down_name ] || {
	Error "Can't find netconfig info file for $down_name."
	cleanup $FAIL
}
nb_name=`grep NCBPREFIX $down_name | sed -e 's/NCBPREFIX=\"//
					     s/\"//'`
[ -z "$nb_name" ] && {
	Error "Can't find NCBPREFIX of $down_name."
	cleanup $FAIL
}
transports=$nb_name
dcount=0

# Find already installed lmxc transports
[ -f /usr/lib/lm/chains ] && grep "lmxc" /usr/lib/lm/chains > $tmp.chn
[ -f $tmp.chn ] && dcount=`wc -l $tmp.chn | awk '{print $1}'`
[ $dcount -ge $MAXCHAINS ] && {
	Error "Maximum number of transports already configured."
	cleanup $FAIL
}
[ -f $tmp.chn ] && {
	sed s/lmxc:// $tmp.chn > $tmp.tprt || {
		Error "Failed to edit /usr/lib/lm/chains."
		cleanup $FAIL
	}
}

if [ -f $tmp.tprt ]
then
	for i in `cat $tmp.tprt`
	do
		transports="$transports $i"
	done
fi

if [ "$config_in" = "Y" ]
then
	addlmxdrvs || cleanup $FAIL
fi

# Call netbios init script
execinit $nb_name add 
ret=$?
case $ret in
$OK|$RELINK) : 				;;
*)	 cleanup $ret			;;
esac

# Add new chain to file
# In upgrade mode it might already be there
lmchain="lmxc:$nb_name"
grep "$lmchain" /usr/lib/lm/chains > /dev/null 2>&1 || \
	echo "$lmchain" >> /usr/lib/lm/chains
grep "$nb_name" /usr/lib/netbios/nb.list > /dev/null 2>&1 || \
	echo "$nb_name" >> /usr/lib/netbios/nb.list

# Add cor if needed
# if there is more than one netbios driver, a coresident
# library "cor" must be added to the stack, also

[ "$dcount" -ge "1" ] && {
    configin cor || {
	Error "Failed to configure co-resident driver."
	return $FAIL
    }
    transports="cor $transports"
    changeparm CNCBS 32
    changeparm CORMAPNCB 32
}

# this builds a space.c file defining entry points for all the
# netbios drivers that will be used
chmod 700 /etc/conf/cf.d/netconfigure
cd /etc/conf/cf.d
./netconfigure -S xncb -n $transports > /etc/conf/pack.d/xstr/space.c || {
	Error "Netconfigure failed."
	cd $_currdir
	cleanup $FAIL
}


[ "$config_in" = "Y" ] && {
	# Start rc scripts
	domultiact
	# Run netbios mail init
	[ -x /usr/lib/lm/nbmail.init ] && /usr/lib/lm/nbmail.init
}

cleanup $RELINK
