:
#	@(#) lmx 60.1 2/13/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.2v2 Initialization
#
OK=0
FAIL=1

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

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

# perms list needed if link kit must be installed
permschk () {
	if [ -f /etc/perms/inst ]; then
		PERM=/etc/perms/inst
	else
		error "Cannot locate /etc/perms/inst. Needed to verify
 linkkit installation"
		exit $FAIL
	fi
}

# test to see if link kit is installed
linkchk()  {
	cd /
	until	fixperm -i -d LINK $PERM
	do	case $? in
		4)  echo "\nThe Link Kit is not installed." >&2	;;
		5)  echo "\nThe Link Kit is only partially installed." >&2  ;;
		*)  echo "\nError testing for Link Kit.  Exiting." >&2; cleanup $FAIL  ;;
		esac

		# Not fully installed. Do so here
		[ "$auto" = "yes" ] && {
		     echo "
The link kit must be installed to run this program.  Exiting ..."
		     exit $OK
		}

		getyn "\nDo you wish to install it now?" || {
			# answered no
			echo "
The link kit must be installed to run this program.  Exiting ..."
			exit $OK
		}

		# answered yes, so install link kit
		echo "\nInvoking /etc/custom\n"
		/etc/custom -o -i LINK || {
			# custom exited unsuccessfully
			echo "custom failed to install Link Kit successfully.  Please try again."
			exit $FAIL
		}
	done
}

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

############
#
# streamschk ()
#
# Calls: mkdev
#
# Ensure that streams are configured on the system.  If they
# aren't, give option of installing them.
#
streamschk () {

    dconfed timod && dconfed tirdwr && return $OK

    [ "$auto" = "yes" ] && {
	echo "Streams must be configured on your system before LAN Manager 
can be made operational."
	cleanup $OK
    }
    getyn "
Streams must be configured on the system before LAN Manager can be made
operational.  Do you wish to configure streams now?" || {
	echo "Run mkdev streams to include streams on your system."
	cleanup $OK
    }
    mkdev streams || {
	echo "Configuring streams failed.  Streams must be configured
before LAN Manager can be configured."
	cleanup $FAIL
    }
}

getnbname() {
	cd /usr/lib/netbios
	if [ -f ${1}data ]
	then
		. ${1}data
	else
		menu=`sed -n 's/^#![ 	]*\(.*\)/\1/p' ${1}init`
	fi
	cd $curdir
}

mkmenu() {
	curdir=`pwd`
	count=0
	cd /usr/lib/netbios
	ls *init | sed -e 's/init//' > $ourdir/nblist
	cd $curdir
	rm -f chainlist
	rm -f menu
	star=no
	[ -x /usr/lib/netconfig/init/lmxs ] && {
		for i in `cat nblist`
		do
			chain=lmxs:$i
			getnbname $i
			echo $chain >> chainlist
			if grep $chain /usr/lib/lm/chains > /dev/null 2>& 1
			then
				install=YES
			else
				install=NO
			fi
			count=`expr $count + 1`
			netconf=" "
			[ -f /usr/lib/netconfig/info/$i ] && {
				netconf="*"
				star=yes
			}
			echo "$netconf$count.\t$install\t\tMS LAN Manager Server\t$menu" >> menu
		done
	}
	[ -x /usr/lib/netconfig/init/lmxc ] && {
		for i in `cat nblist`
		do
			chain=lmxc:$i
			echo $chain >> chainlist
			getnbname $i
			if grep $chain /usr/lib/lm/chains > /dev/null 2>& 1
			then
				install=YES
			else
				install=NO
			fi
			count=`expr $count + 1`
			netconf=" "
			[ -f /usr/lib/netconfig/info/$i ] && {
				netconf="*"
				star=yes
			}
			echo "$netconf$count.\t$install\t\tMS LAN Manager Client\t$menu" >> menu
		done
	}
	[ "$star" = "yes" ] && echo "\n* - Netconfig must be used to configure or deconfigure this component" >> menu
}

# prompt user to re-link new kernel
asklink()  {
	getyn "
You must create a new kernel to effect the driver change you specified.
Do you wish to create a new kernel now?"  ||  {
		echo "
To create a new kernel execute /etc/conf/cf.d/link_unix. Then you
must reboot your system by executing  /etc/shutdown -i0  before the 
changes you have specified will be implemented.\n"
			return $FAIL 
		}
	return $OK
}

# re-link new kernel
klink() {
	cd /etc/conf/cf.d
	./link_unix || { 
		echo "\nError: Kernel link failed."
		cleanup $FAIL
	}
	return $OK
}

# main()
	cd /
	permschk
	linkchk
	streamschk
	ourdir=/tmp/.lmx
	rm -fr $ourdir
	mkdir $ourdir
	cd $ourdir
	change=no

	while true
	do
		mkmenu
		echo "\n\tLAN Manager Configuration Utility.\n"
		echo "Chain\tConfigured\tServer or Client\tNetBIOS Name"
		cat menu
		mesg="Enter chain number to configure or deconfigure"
		while prompt || break 2
		do
			[ "$cmd" -ge "1" -a "$cmd" -le "$count" ] && break
			echo "Please select a number between 1 and $count."
		done
		# pass the NetBIOS name to the server or client init routine
		chain=`tail +$cmd chainlist | head -1`
		prg=`echo $chain | sed -e 's/:.*//'`
		nbname=`echo $chain | sed -e 's/.*://'`
		curdir=$ourdir
		getnbname $nbname
		tmpf=$ourdir/bog$$
		[ "$prg" = "lmxc" ] && type="with LAN Manager Client"
		[ "$prg" = "lmxs" ] && type="with LAN Manager Server"
		[ -f /usr/lib/netconfig/info/$nbname ] && {
			echo "$menu supports netconfig"
			echo "You cannot use mkdev to configure it"
			echo "Please run netconfig to configure this one"
			continue
		}
		echo "NCBPREFIX=$nbname" > $tmpf
		if grep $chain /usr/lib/lm/chains > /dev/null 2>& 1
		then
			getyn "\nDe-Configure $menu $type" && {
				echo "\nDe-Configuring $menu $type...\n"
				( . $tmpf ; . /usr/lib/netconfig/remove/$prg )
				retval=$?
				[ $retval = 2 ] && change=yes
			}
		else
			getyn "\nConfigure $menu $type" && {
				echo "\nConfiguring $menu $type...\n"
				( . $tmpf ; . /usr/lib/netconfig/init/$prg )
				retval=$?
				[ $retval = 2 ] && change=yes
			}
		fi
		rm -f $tmpf
	done

	cd /
	rm -fr $ourdir

	[ "$change" = "yes" ] && {
		asklink && klink
	}

	exit 0

# end main
