:
#	@(#) lmxc 60.3 3/18/92
#
#	Copyright (C) The Santa Cruz Operation, 1986, 1989.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation and is Confidential.
#
# lmx - Xenix Net for UNIX 3.2 Initialization
#
# 	Adapted for ODT Phoenix
#

PATH=/etc:/bin:/usr/bin
LANG=english_us.ascii
export PATH LANG

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

# Define driver dependents
xnetflags=
xstrflags=
xncbflags=
corflags=
tpncflags="-Y"
tpndflags="-Y"
lbcflags="-Y"
lbuflags="-Y"

rmvlog=/tmp/rmvlog
errlog=/tmp/rmvlog
change=
wholemenu=
codelist=
netmax=2
tmp=/tmp/$$

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

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

# 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*
	exit $1
}

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

# Prompt for yes or no answer - returns non-zero for no
getyn() {
	while	echo "$* (y/n) \c">&2
	do	read yn rest
		case $yn in
		[yY])	return $OK 			;;
		[nN])	return $FAIL			;;
		*)	echo "Please answer y or n" >&2	;;
		esac
	done
}

# Prompt with mesg, return non-zero on q
prompt() {
	while	echo "\n${mesg} or enter q to quit: \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		Q|q)	return $FAIL					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is an argument use it as the default
			# else loop until 'cmd' is set
			[ "$1" ] && { 
				cmd=$1
				return $OK
			}
			: continue
			;;
		*)	return $OK					;;
		esac
	done
}

# Print an error message
Error() {
	echo "\nNetconfig rmv error: $*" >&2
	echo "\nNetconfig rmv error: $*" >> $errlog
	return $FAIL
}

# Configure error message
conferr()  {
	Error "\nConfigure failed to update system configuration.
Check /etc/conf/cf.d/conflog for details."
}

# ---------- DEVICE ROUTINES ---------  These routines create and remove
#					devices.

##########
#
# listcodes ()
#
# Calls: basename, expr
#
# Creates a list of vendor codes of all available drivers (codelist)
listcodes() { 
	[ -d /usr/lib/netbios -a -f /usr/lib/netbios/* ] && { 
		codelist=
		for _i in /usr/lib/netbios/*
		do
			base=`basename $_i`
			vcode=`expr "$base" : "\(.*\)init"`
			codelist="$codelist $vcode"
		done
	}
	return $OK
}


##########
#
# setvendorcode()
#
# Calls: grep, basename, expr
#
# Get the vendor supplied name of a netbios driver
setvendorcode() {
	vlib=$1
	for _file in /usr/lib/netbios/*
	do
		if grep $vlib $_file > /dev/null
		then
			base=`basename $_file`
			vendorcode=`expr "$base" : "\(.*\)...."`
			return $OK
		fi
	done
	return $FAIL
}

##########
#
# driverlists()
#
# Calls: cat
#
# Create a list of vendor codes that are currently installed (dlist)
driverlists() { 
	dlist=
	dcount=0
	[ -f /usr/lib/netbios/nb.list ] || > /usr/lib/netbios/nb.list
	[ "$codelist" ] || listcodes
	for biosname in `cat /usr/lib/netbios/nb.list`
	do
	    setvendorcode $biosname || { 
		Error "Missing data file for $biosname"
		return $FAIL
	    }
	    for _i in $dlist
	    do
		# If the vendorcode is already in dlist, continue with the
		# next entry from /usr/lib/netbios/nb.list.
		[ "$_i" = "$vendorcode" ] && continue 2
	    done
	    dcount=`expr $dcount + 1`
	    dlist="$dlist $vendorcode"
	done
	return $OK
}


##########
#
# changeparm ()
#
# 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`
    [ -z "$curval" ] && {
	cd $_currdir
	return
    }
    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
}

##########
#
# confchk ()
#
# Calls: configure, pwd, eval
#
# Arguments: $1 = name of driver
#
# Checks the status of the specified driver in the system, sets
# cf_state to YES for fully configured driver, PART for driver
# with assigned major number, NO non used driver
confchk () {
    _name=$1
    cf_state=
    eval ${_name}major=
    _currdir=`pwd`
    cd /etc/conf/cf.d
    eval ${_name}major='`./configure -j $_name`' || {
	cf_state=NO
	cd $_currdir
	return $FALSE
    }
    # we already have major number; check for driver enabled
    dconfed $_name && {
	cf_state=YES
	cd $_currdir
	return $OK
    }
    cf_state=PART
    cd $_currdir
    return $FALSE
}

##########
#
# configout ()
#
# Calls: pwd, configure, eval
#
# Arguments: $1=name of driver, $2=state of driver
#
# Deconfigure a driver in the system
configout () {
    _name=$1
    state=$2
   # echo "Removing $_name driver."
    [ "$state" = "NO" ] && return $OK
    _currdir=`pwd`
    cd /etc/conf/cf.d
    eval majnum=\$${_name}major
    eval flags=\$${_name}flags
    /etc/conf/bin/idinstall -d $_name >> $rmvlog 2>&1 || {
#    ./configure -d -c -m $majnum $flags > $rmvlog || {
        Error "Removing driver failed.  See $rmvlog for details."
        cd $_currdir
        return $FAIL
    }
    cd $_currdir
    change=yes
    return $OK
}


##########
#
# undoxnet ()
#
# check for and remove xnet drivers from the system
undoxnet () {

    confchk cor
    [ "$cf_state" = "NO" ] || {
	configout cor $cf_state || return $FAIL
    }

    confchk xnet
    [ "$cf_state" = "NO" ] && {
	echo "LAN Manager $prdname components are not configured in system."
	return $OK
    }
    configout xnet $cf_state || return $FAIL
    confchk xncb
    [ "$cf_state" = "NO" ] || {
	configout xncb $cf_state || return $FAIL
    }

    confchk tpnc
    [ "$cf_state" = "NO" ] || {
	configout tpnc $cf_state || return $FAIL
    }

    confchk tpnd
    [ "$cf_state" = "NO" ] || {
	configout tpnd $cf_state || return $FAIL
    }

    confchk lbc
    [ "$cf_state" = "NO" ] || {
	configout lbc $cf_state || return $FAIL
    }

    confchk lbu
    [ "$cf_state" = "NO" ] || {
	configout lbu $cf_state || return $FAIL
    }

    rm -f /dev/xnet
    rm -f /dev/tpnc
    rm -f /dev/tpnd
    rm -f /dev/lbu
    rm -f /dev/lbc
    return $OK
}


##########
#
# undonfc_ ()
#
# Calls: cp, sed, mv
#
# Deconfigure nfc_ drivers from the system
undonfc_ () {

#    echo "Removing nfc_ driver."
    # turn off flag in sfsys.d/nfc_ file
    [ -f /etc/conf/sfsys.d/nfc_ ] || {
	Error "No sfsys.d/nfc_ file.  Cannot proceed."
	return $FAIL
    }
    sed 's/	.$/	N/' /etc/conf/sfsys.d/nfc_ > $tmp.nfc_
    unset_trap
    mv $tmp.nfc_ /etc/conf/sfsys.d/nfc_
    set_trap
    [ -d /etc/conf/pack.d/nfc_ ] && {
	    cd /etc/conf/pack.d/nfc_
	    [ -f stubs.c- ] && mv stubs.c- stubs.c
	    rm -f Driver.[oa] space.c
    }
    return $OK
}


alldrivers="cor xnet xstr xncb lbu lbc tpnc tpnd nfc_"
##########
#
# undotunable()
#
# Calls: awk, fgrep, mv, rm
#
# Remove xnet tunable parameters from mtune file
undotunable () {

    [ -f /etc/conf/cf.d/mtune ] || {
	Error "No mtune file!!! Ensure this file is present before continuing."
	return $FAIL
    }
    currdir=`pwd`
    cd /usr/lib/lm/ID
    [ -f /usr/lib/lm/mtune.lmx ] && rm /usr/lib/lm/mtune.lmx
    for drv in $alldrivers
    do
	cd $drv
	[ -f Mtune ] && cat Mtune >> /usr/lib/lm/mtune.lmx
	cd ..
    done
    cd $currdir
    [ -f /usr/lib/lm/mtune.lmx ] || {
	Error "LAN Manager $prdname tunable parameters file not found."
	return $OK
    }
    awk '{printf("^%s\n", $1)}' /usr/lib/lm/mtune.lmx > $tmp.tun
    cp /etc/conf/cf.d/mtune $tmp.mtun1
    for _i in `cat $tmp.tun`
    do
        grep -v $_i $tmp.mtun1 > $tmp.mtun
        mv $tmp.mtun $tmp.mtun1
    done
    unset_trap
    mv $tmp.mtun1 /etc/conf/cf.d/mtune
    set_trap

    awk '{printf("^%s\n", $1)}' /usr/lib/lm/mtune.lmx > $tmp.tun
    cp /etc/conf/cf.d/stune $tmp.stun1
    for _i in `cat $tmp.tun`
    do
        grep -v $_i $tmp.stun1 > $tmp.mtun
        mv $tmp.mtun $tmp.stun1
    done
    unset_trap
    mv $tmp.stun1 /etc/conf/cf.d/stune
    set_trap
    rm -f $tmp.tun /usr/lib/lm/mtune.lmx
    return $OK
}

##########
#
# rmlmxdrvs ()
#
# run through each step to remove lmx drivers
rmlmxdrvs () {
    echo "Removing LAN Manager Drivers"
    undoxnet || return $FAIL
    undonfc_ || return $FAIL
    undotunable || return $FAIL
    echo "Decreasing System 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:
    [ "$prd" = "lmxs" ] && {
	    changeparm NUMTIM -128
	    changeparm NUMTRW -128
    }
    echo "LAN Manager $prdname components successfully removed from system configuration\n"
    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 $FAIL
    ./${initscript}init $2
    retval=$?
    case $retval in
	$OK) : ;;
	$STOP) cleanup $STOP ;;
	$HALT) Error "$initscript rmv: haltsys not supported" ;;
	$FAIL) Error "$initscript rmv: failed" ;;
    esac
    cd $_currdir
    return $retval
}

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

	# Check the status of the driver
	# If there is a 'N' in the second field, the driver is deconfigured
	drvr_in=`awk '{print $2}' ./$drvr > /dev/null` || {
		Error "Couldn't check status of $drvr sdevice."
		return $FAIL
	}
	[ "$drvr_in" = "N" ] && {
		return $OK
	}
	# edit the sdevice file to use reflect the deletion of a driver
	sed 's/	Y/	N/' $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
	cd $currdir
	return $OK
}

##########
#
# rmbiosdrvs ()
#
# Calls: netconfigure, grep, mv
#
# Check for and remove netbios driver from system.  Adjust streams stack
rmbiosdrvs () {

    aci=
    name=$1
    _currdir=`pwd`
    cd /etc/conf/cf.d
    echo "Removing NetBIOS driver $name"
    grep "^${name}\$" /usr/lib/netbios/nb.list > /dev/null || {
	echo "$name LAN Manager $prdname NetBIOS driver not installed."
	cd $_currdir
	return $OK
    }
    execinit $name remove
    grep -v "^$name" /usr/lib/netbios/nb.list > $tmp.nbl
    unset_trap
    mv $tmp.nbl /usr/lib/netbios/nb.list
    set_trap
    dcount=`wc -l /usr/lib/netbios/nb.list | awk '{ print $1 }'`
    dlist=`cat /usr/lib/netbios/nb.list`
    strdrv=
    if [ "$dcount" -ge "1" ]
    then
	if [ "$dcount" -ge "2" ] 
	then strdrv="cor"
	else
	    turnyton cor || {
		Error "Deconfiguring co-resident driver failed."
		return $FAIL
	    }
	    changeparm CNCBS -32
	    changeparm CORMAPNCB -32
	fi
	for i in $dlist
	do 
	    [ "$i" != "$name" ] && strdrv="$strdrv $i"
	done
	./netconfigure -S xncb -n $strdrv > /etc/conf/pack.d/xstr/space.c || {
	    Error "Netconfigure failed."
	    cd $_currdir
	    return $FAIL
	}
    else
	confchk xstr
	configout xstr $cf_state || return $FAIL
    fi

    # remove driver from netbios list
    wholemenu=
    change=yes
    cd $_currdir
    return $OK
}

##########
#
# dconfed ()
#
# Arguments: $1=name of driver
#
# Calls: awk
#
# Check is a driver is currently configured into the system
dconfed ()  {
dname=$1

if [ -f /etc/conf/sdevice.d/$dname ] 
then
	confflag=`awk '{print $2}' /etc/conf/sdevice.d/$dname` 2> /dev/null ||
		Error "awk failed reading sdevice file"
	[ "$confflag" = "Y" ] && return $OK
fi
return $FAIL
}


##########
#
# undomultiact ()
#
# Calls: find, rm
#
# Remove rc files needed by lmx
undomultiact () {
    [ "$prd" = "lmxs" ] && {
	rm -f /etc/rc0.d/K65lmxs
	rm -f /etc/rc2.d/S90lmxs
    }
    [ "$prd" = "lmxc" ] && {
	rm -f /etc/rc0.d/K66lmxc
	rm -f /etc/rc2.d/S91lmxc
    }
    return $OK
}

rmbsd() {
	echo "Removing BSD enhancements"
	curdir=`pwd`
	cd /etc/conf/cf.d
	major=`./configure -j bsd` && {
		./configure -d -c -m $major
	}
	rm -fr ../*/bsd
	rm -f /dev/bsd
	cd $curdir
}


rmserver() {
	rmbsd
	echo "Removing vtps."
	/etc/vtps remove
	echo "Decreasing System Parameters for LAN Manager Server."
	changeparm NBUF -512
	changeparm NPROC -200
	changeparm NREGION -600
	changeparm NFILE -100
	changeparm SHMSEG -9
	changeparm SRVINUSE -1
}

# main() 
#########################

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

# [ "$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
}
nbname=`grep NCBPREFIX $down_name | sed -e 's/NCBPREFIX=\"//
					    s/\"//'`

cd /

chains=/usr/lib/lm/chains
prd="lmxc"
chain=$prd:$nbname

# remember if we are a server
server=1
prdname="Server"
[ "$prd" = "lmxc" ] && {
	server=0
	prdname="Client"
}

# first check if this chain exists
grep $chain $chains > /dev/null 2>&1 || {
	Error "$prdname chain does not exist"
	cleanup $OK
}

echo "Removing SCO LAN Manager $prdname over $nbname."

# remove our chain
sed -e "/$chain/d" < $chains > /tmp/bog$$
cp /tmp/bog$$ $chains
rm -f /tmp/bog$$

# next check if this is the last product
grep "lmx[cs]:" $chains > /dev/null 2>&1
lastprd=$?

# check if this is the last product of its type (client or server)
grep "$prd:" $chains > /dev/null 2>&1
lastprdtp=$?

# next check if the netbios name is gone
grep ":$nbname" $chains > /dev/null 2>&1
lastnb=$?

# check if server and last server
lastsrv=0
[ "$server" != "0" ] && {
	grep "lmxs:" $chains > /dev/null 2>&1
	lastsrv=$?
}

# remove server if needed
[ "$lastsrv" != "0" ] && {
	echo "Removing LAN Manager Server."
	rmserver
	# mail configuration
	[ -x /usr/lib/lm/nbmail.rmv ] && /usr/lib/lm/nbmail.rmv
}

# undomultiact if last of product type
[ "$lastprdtp" != "0" ] && undomultiact

# remove bios driver if needed
[ "$lastnb" != "0" ] && rmbiosdrvs $nbname

# remove lmx basic drivers if last product
[ "$lastprd" != "0" ] && rmlmxdrvs

[ "$change" = "yes" ] && cleanup $RELINK
cleanup $OK
