#! /bin/sh5
#WARNING: Do not remove the following line.  Update version number when needed.
#ds_version=2.4
# static char DsvconfigSid[] = "@(#)dsvconfig	2.4		01/16/92";
#                         Copyright (C) 1988-1992 by
#                Digital Equipment Corporation, Maynard, Mass.
#
#            Copyright (c) Digital Equipment Corporation, 1990  All  Rights
#            Reserved.   Unpublished  rights  reserved  under the copyright
#            laws of the United States.  The  software  contained  on  this
#            media   is   proprietary  to  and  embodies  the  confidential
#            technology of Digital Equipment Corporation.  Possession, use,
#            duplication  or  dissemination  of  the  software and media is
#            authorized only pursuant  to  a  valid  written  license  from
#            Digital  Equipment Corporation.  RESTRICTED RIGHTS LEGEND Use,
#            duplication, or disclosure by the U.S.  Government is  subject
#            to  restrictions  as  set  forth in Subparagraph (c)(1)(ii) of
#            DFARS 252.227-7013, or in FAR 52.227-19, as applicable.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or  any  other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of  the  software  is  hereby
# transferred.
#
# The information in this software is subject to change without  notice
# and  should  not be  construed  as  a commitment by Digital Equipment
# Corporation.
#
# Digital assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Digital.
#
# Networks & Communications Terminal Server Software
#
# MODIFICATION  HISTORY:
# 2.4	16-DEC-1991 David Bonner
#	Added support for MUXserver 320.
#
# 2.3	13-Dec-1991 David G. Dugal
#	Add DS700 and DS90 support.
#
# 2.2	23-Oct-1991 David G. Dugal
#	Make intelligent guess at service circuit ID so that user will not
#	be required to run NCP.
#
# 2.1   22-Aug-1991 Ian Richards
#       Added support for MUXserver 380.
#
# 2.0	08-Feb-1991 Dan Muller
#	Ported from C shell (csh) to Bourne shell (sh5).
#	This required major restructuring. Very few changes were made
#	to the program's behaviour, however.
#
#	I did fix a problem
#	with the code that detects "old format" dsvconfig data files.
#	There was no reason to translate lines to uppercase in that
#	code. In fact, the way in which awk was piping output to tr
#	caused the test to intermittently fail; apparently, the shell
#	script execution sometimes continued before tr was done, so
#	sometimes it tested for the existence of tr's output file
#	before the file had been created, causing the script to miss
#	the fact that the database needed to be converted!
#
#	After a slightly longer startup time, the Bourne shell script
#	appears to run considerably faster than the C shell script.
#
# 1.6   10-DEC-1990 John R. Mattioli, Jr.
#       Included changes to support the DECserver 250 as supplied by Bill Dion.
#
# 1.5   24-Oct-1990 -- L. Fairbanks
#
#       Took out DECnet address prompting; not used by DECnet-ULTRIX.
#	
#	13-OCT-1989
#	DS500 V2.n support -- L. Fairbanks
#
#    	21-May-1990 
#
#  	Fixed some nits, like adding sleep's for screens that flashed by
# 	before they could be read, and added "Type RET to accept the
#	default" in a couple of appropriate places.
#
#	28-Nov-1989 
#
# 	Added retention of old DECnet address of a server being swapped;
#	previously the DECnet address of swapped servers was being replaced
#	by 0.  Fixed creation of default image files when adding servers so
#	that it first checks to see if a file with EITHER the default name
#	or the specified name exists before creating a new one.  Added
#	option to SWAP for user to retain the old image file of a server being
#	swapped out.
#
#	9-NOV-1989
#
#	Modified so that default image files names are created in lower
#	case (MOP requirement) instead of upper case, and changed 
#	dump file names for all server types to lower case.
#
#	3-NOV-1989 
#	Added a column for DECnet address to the dsvconfig display,
#	and added code to prompt for it to the DS500 case.  Added check 
#	for invalid DECnet address.  
#
#       30-OCT-1989 
#	Modified to prompt user for name of DS500 image file instead of
#	just defaulting to "DS5svrname.SYS".  If image file specified
#	does not exist in the working dir, then the distribution image
#	is copied into a file with that name.
#
# 1.4   10-APR-1989
#	Ultrix V3.0
#
# 1.3   10-APR-1989	Craig Perez
#	Ultrix V3.0
#       - Modified version of Version 1.4 that removes the DS300
#         references from the procedure.
#       - Fixed circuit field misalignment
#       - Added comment that BNT-n is also for a DEBNA device
#       - Added comment that XNA-n is for a DEBNI device
#
# 1.2	08-FEB-1989	Carlos Sierra
#	ULTRIX V3.0
#	- Merge 1.1 changes made by JRM with MUXserver Changes
#	- Change server types to accept literal values rather that
#	  numerical values
#
# 1.1	07-NOV-1988
#	Ultrix V2.4-6, MUXserver 100 V2.2, MUXserver 300 V1.0
#
# 1.0  25-APR-1988
#      ULTRIX V2.4-6, DECserver 100 V2.0
#
# 0.4  18-NOV-1986
#      Ultrix v1.2/2.0, DECserver 100 v1.3
#
# 0.2  08-May-1986
#      Ultrix V2.0
#
# 0.0  27-Dec-1985                              
#      Ultrix V1.2
#
#
#
# NOTE:  the datafile has the following format
#
#        field 1 -- node name
#        field 2 -- server type
#        field 3 -- service circuit
#        field 4 -- ethernet addr
#        field 5 -- load file
#        field 6 -- dump file
#

set -f		# disable file name generation
ARGV="$*"
PROG=`basename $0`
TRUE=0		# This looks strange, but makes sense in Bourne shell
FALSE=1		# control constructs.
DEST="/usr/lib/dnet"
DATAFILE="$DEST/dsvconfig.dat"
TEMP="$DEST/dsvconfig.$$"
NULL="/dev/null"
TRAPS="1 2 3 9 15 17"	# My best guess at signals we should trap
readonly PROG TRUE FALSE DEST TEMP NULL TRAPS

set -- $ARGV
case "$1" in
"")	RESTORE=$FALSE ;;
[Rr])	if [ -s $DATAFILE ]
	then
		RESTORE=$TRUE
	else
		echo "\nThere are no DECservers defined in the data file"
		exit
	fi
	;;
*)	echo "Usage: $PROG [Rr]"
	exit
	;;
esac

test -f $DEST/nodes_p	# node_p determines if DECnet running
DECNET=$?		# remember DECnet state
fromswap=$FALSE

trap "ERR_EXIT 1" $TRAPS

#

# Miscellaneus Function definitions

# UPCASE
# Takes one argument, the name of an environment variable.
# The value of that environment variable is converted to uppercase.
UPCASE() {
	eval v=\"$"$1"\"			# v is value of the variable
	eval $1=`echo \"$v\" | tr a-z A-Z`	# change value to uppercase
	unset v
}

# DOWNCASE
# The converse of UPCASE.
DOWNCASE() {
	eval v=\"$"$1"\"			# v is value of the variable
	eval $1=`echo \"$v\" | tr A-Z a-z`	# change value to lowercase
	unset v
}

# NO_SERVERS checks to see if the DECserver data file is empty. If so,
# it prints a message to that effect and return $TRUE.
NO_SERVERS() {
	if [ -s $TEMP.dat ]
	then
		return $FALSE
	else
		clear
		echo "\nThere are no DECservers defined in the data file."
		sleep 2
		return $TRUE

	fi
}

PROMPTS() {
	clear
	err=$FALSE
	echo "\nAt any time you may type a ? for help."
	echo "Type < 6 > to return to menu."
}

#

# Exit functions. These functions are used to cleanup and exit
# under various conditions.

# This function is called when a fatal error is encountered.
# It tries to clean up temporary files and then exits the shell.
ERR_EXIT() {
	rc=$1
	echo ""
	[ -f $TEMP.dsv ] && rm $TEMP.dsv
	[ -f $TEMP.dat ] && rm $TEMP.dat
	exit $rc
}

DONE() {
	trap $TRAPS
	mv $TEMP.dat $DATAFILE
	[ -f $TEMP.dsv ] && rm $TEMP.dsv
	if [ -s $DATAFILE ]
	then
		echo ""
		echo "NOTE:	The /etc/mop_mom daemon must be running before loading"
		echo "	any DECservers."
		echo ""
	fi
	exit
}

EXIT() {
	echo "$PROG:\c"
	if [ $err = 0 ]
	then
		echo " Cancelled via user interrupt"
	else
		echo " Fatal error\n\n"
	fi
	echo "                  The DECserver node was NOT \c"
	echo "$errstr"
	ERR_EXIT 1
}

#
#
# function VALIDATE_CIRC_NAME
# Checks $1 to make sure it's a valid circuit name. Returns $TRUE if
# it is, $FALSE if it's not.
#
VALIDATE_CIRC_NAME() {(	# run as child process so we don't clobber env
	cir="$1"
	case "$cir" in
		UNA-[0-9] | QNA-[0-9] | BN[AT]-[0-9] | XNA-[0-9] | \
		SVA-[0-9] | NE-[0-9] | FZA-[0-9])
			return $TRUE
			;;
		'?')
			echo ""
			echo "Specify the service circuit-ID associated with your network controller type"		
			echo " "

			echo "      UNA-n   if your network controller type is a DEUNA, DELUA"
			echo "      QNA-n   if your network controller type is a DEQNA"
			echo "      BNT-n   if your network controller type is a DEBNT, DEBNA"
			echo "      XNA-n   if your network controller type is a DEBNI"
			echo "      SVA-n   if your network controller type is a DESVA" 
			echo "      FZA-n   if your network controller type is a DEFZA"
			echo "      NE-n"
			echo " "		
			echo "      where n is an integer (typically 0 or 1)"
			echo " "		
			sleep 1
			return $FALSE
			;;
		* )
#			echo ""
#			echo "          *** Invalid service circuit ID ***"
#			sleep 1
#			VALIDATE_CIRC_NAME '?'
#			return $FALSE
			return $TRUE
			;;
	esac
)}

#
# function DSNAME_HELP
# Prints standard help message for name prompts.
DSNAME_HELP() {
	echo "     The DECserver node name for your DECserver unit  must  be  from  1  to  6"
	echo "     alphanumeric characters, the first being alphabetic. This name identifies"
	echo "     the unit on the network."
}

#
#
# function VALIDATE_NAME
# Checks $1 to make sure it's a valid DECserver name. If $2 is the
# string "new", it also makes sure that it does not already exist in
# any of the relevant databases. If $2 is "existing", it makes sure
# that it DOES exist. If any of these checks fails, VALIDATE_NAME
# returns $FALSE, otherwise it returns $TRUE. If $2 is anything other
# than "new" or "existing", the database checks are not done.
#
VALIDATE_NAME() {(	# run in child process so we don't clobber env
	name="$1"
	required_state="$2"
	case "$name" in
		[a-z] |\
		[a-z][a-z0-9] |\
		[a-z][a-z0-9][a-z0-9] |\
		[a-z][a-z0-9][a-z0-9][a-z0-9] |\
		[a-z][a-z0-9][a-z0-9][a-z0-9][a-z0-9] |\
		[a-z][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9] )
                ;;
	*)
		echo ""
		echo "     *** Invalid DECserver node name ***"
		return $FALSE
		;;
	esac
	if [ $required_state = new ]
	then
		if grep -s -i $name $TEMP.dat
		then
			echo ""
			echo "     A node with the name $name already exists in the DECserver"
			echo "     data file; please try again."
			sleep 1
			return $FALSE
		elif /etc/getnode $name >$NULL 2>$NULL
		then
			echo ""
			echo "     A node with the name $name already exists in the volatile"
			echo "     node database; please try again."
			sleep 1
			return $FALSE
		elif [ $DECNET = $TRUE ] &&\
		     /etc/getnode -P $name >$NULL 2>$NULL
		then
			echo ""
			echo "     A node with the name $name already exists in the permanent"
			echo "     node database; please try again."
			sleep 1
			return $FALSE
		fi
	elif [ $required_state = existing ]
	then
		if grep -s -i $name $TEMP.dat
		then :
		else
			echo ""
			echo "     No unit is defined as DECserver node name $name"
			sleep 1
			return $FALSE
		fi
	fi
	return $TRUE
)}
#
#
# VALIDATE_ENET_ADDR
# Checks $1 to be sure it's a syntactically correct ethernet address.
# If $2 is the string "new", it also checks to make sure the address
# does not already exist in the DECserver data file.
# If either check fails, returns $FALSE, otherwise returns $TRUE
#
VALIDATE_ENET_ADDR() {(	# run in child process to avoid changing env
	addr="$1"
	required_state="$2"
	case "$addr" in
	'?')	echo ""
		echo "     The Ethernet address is the address located on the back of your  DECserver"
		echo "     unit.  It must be in the form of 6 Hex digits, separated with a minus (-)."
		echo ""
		echo " 	      (  Example -    08-00-01-00-AB-CD   )"
		echo ""
		echo "     This address is used to establish communication between your host and the"
		echo "     DECserver unit, and must be entered in the exact format shown,  and be"
		echo "     correct for the unit you are adding."
		enet=""
		return $FALSE
		;;
	[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F])
		;;
	*)	echo "\n     *** Invalid Ethernet address ***"
		enet=""
		return $FALSE
		;;
	esac
	if [ $required_state = new ] && grep -s -w $addr $TEMP.dat
	then
		echo ""
		echo "     A node with the address $addr already exists;"
		echo "     please try again."
		sleep 1
		return $FALSE
	fi
	return $TRUE
)}
#
#
# function VALIDATE_TYPE
# Checks value of dstype variable to make sure it's a valid DECserver
# type name.
# If dstype is a question mark, give help and return $FALSE.
# If it's a valid name, then return $TRUE.
# Otherwise, give help, set dstype to null, and return $FALSE.
VALIDATE_TYPE() {(	# run in child proc to avoid clobbering env
	dstype="$1"
	case "$dstype" in
	DS100 | DS200 | DS250 | DS300 | DS700 | DS90 | DS500 | MS100 | MS300 | MS320 | MS380)
		return $TRUE
		;;
	'?')	echo ""
		echo ""
		echo "Specify the type of DECserver you are adding:"
		echo ""
		echo "DS100   for a new DECserver 100."
		echo "DS200   for a new DECserver 200."
		echo "DS250   for a new DECserver 250."
		echo "DS300   for a new DECserver 300."
		echo "DS500   for a new DECserver 500."
		echo "DS700   for a new DECserver 700."
		echo "DS90    for a new DECserver 90TL."
		echo "MS100   for a new MUXserver 100."
		echo "MS300   for a new MUXserver 300."
		echo "MS320   for a new MUXserver 320."
		echo "MS380   for a new MUXserver 380."
		return $FALSE
		;;     
	*)	echo ""
		echo "		*** Invalid server type ***"
		VALIDATE_TYPE '?'
		return $FALSE
		;;
        esac
)}
#        
#
ADD_NODE() {	# get dump file name, then add to vol & perm databases
	case "$dstype" in
	DS100)
		dump=psdmp`echo $enet|awk '{s="-";n=split($1,a,s);print a[5]a[6]}'`
		dsload="ps0801eng.sys"
		;;
	DS200)
		dump=ds2$name.dmp
		dsload="pr0801eng.sys"
		;;
	DS250)
		dump=DP2$name.DMP
		dsload="dp0601eng.sys"
		;;
	DS300)
		dump=ds3$name.dmp
		dsload="sh1601eng.sys"
		;;
	DS700)
		dump=ds7$name.dmp
		dsload="wweng1.sys"
		;;
	DS90)
		dump=ds9$name.dmp
		dsload="mneng1.sys"
		;;
	DS500)
		dump=ds5$name.dmp
		#
		# Now, if we're coming to this part of the code from "SWAP",
		# and the user has indicated they wanted to keep the old image
		# file, then no image file prompting is necessary--can skip to
		# the actual add.
		#
		if [ "$fromswap" != $TRUE ]
		then
			echo ""
			echo "Image file name [<RET> for default] ? \c"
			read ifilename
			#
			# If user just hit <RET>, fill in the default file name
			[ ! "$ifilename" ] && ifilename=ds5$name.sys
			#
			# Now see if image file specified already exists in working dir.
			# If it does, skip to setting load file name.
			#
			if [ ! -f $DEST/"$ifilename" ]
			then
				# Copy the distribution image into the specified file.
				cp $DEST/ds5tsv.sys $DEST/"$ifilename"
			fi
			dsload=$ifilename
			# Set the protections on image file to user=rwx,group=rx,others=r.
			chmod 754 $DEST/"$ifilename"
			dstype="DS500"
		fi
		;;
	MS100)
		dump=ms1$name.dmp
		dsload="ms1601eng.sys"
		;;
	MS300)
		dump=ms3$name.dmp
		dsload="ms4801eng.sys"
		;;
	MS320)
		dump=ms2$name.dmp
		dsload="ms3201eng.sys"
		;;
	MS380)
		dump=ms8$name.dmp
		dsload="ms3801eng.sys"
		;;
	esac
	#
	# Don't forget to turn off the "fromswap" flag so that the next option
	# to run through here won't skip out incorrectly!
	#
	fromswap=FALSE

	echo ""
	echo "Please wait ..."
	if /etc/addnode $name -c $cir -h $enet -D $dump -l $dsload
	then :
	else
		err=1
		return $FALSE
	fi
	if [ $DECNET = $TRUE ]
	then
		if /etc/addnode $name -c $cir -h $enet -D $dump -l $dsload -P
		then :
		else
			/etc/remnode $name
			err=1
			return $FALSE
		fi
	fi
	return $TRUE
}

#
#
# LIST_SERVERS lists the contents of the DECserver data file.
#
LIST_SERVERS() {
	NO_SERVERS && return $FALSE
		  
	echo " "
	echo " Node     Svr       Svc    Ethernet           Load              Dump"
	echo " Id       Type      Ckt    Addr               Image             File"
	echo " =============================================================================="
	awk '{printf " %-8s %-9s %-6s %-18s %-17s %s\n",$1,$2,$3,$4,$5,$6}\
END {if(NR > 1) s="s "; else s=" "; \
	print "\n\n Total of "NR" DECserver"s"defined";}' $TEMP.dat
	echo " "
	echo "  Press <RET> to continue \c"
	read dummy
	return $TRUE
}

#
ADD_SERVER() {
	trap EXIT $TRAPS
	errstr="added to the file."
	PROMPTS
	echo "Type <RET> to accept the default"

	dstype=""
	until [ "$dstype" ]
	do
		echo "\n\nDECserver type ? \c"
		read dstype
		UPCASE dstype
		case "$dstype" in
		6)	return $FALSE ;;
		"")	;;
		*)	VALIDATE_TYPE "$dstype" || dstype="" ;;
		esac
	done

	name=""
	until [ "$name" ]
	do
		echo "\nDECserver node name for unit ? \c"
		read name
		DOWNCASE name
		case "$name" in
		6 )	return $FALSE ;;
		'?')	DSNAME_HELP ; name="" ;;
		"")	;;
		*)	VALIDATE_NAME "$name" new || name="" ;;
		esac
	done

	enet=""
	until [ "$enet" ]
	do
		echo "\nDECserver Ethernet address for this unit ? \c"
		read enet
		UPCASE enet
		case "$enet" in
		6)	return $FALSE ;;
		"")	;;
		*)	VALIDATE_ENET_ADDR "$enet" new || enet="" ;;
		esac
	done

	enetcir=`netstat -i | awk '{print $1}'`
	enetcir=`echo $enetcir | awk '{print $2}'`
	case "$enetcir" in
		ln0)	defcir="SVA-0" ;;
		de0)	defcir="UNA-0" ;;
		qe0)	defcir="QNA-0" ;;
		ni0)	defcir="BNA-0" ;;
		xna0)	defcir="XNA-0" ;;
		ne0)	defcir="FCA-0" ;;
		*)	defcir="" ;;
	esac

	cir=""
	until [ "$cir" ]
	do
		echo "\nService circuit for this unit [""$defcir""]? \c"
		read cir
		UPCASE cir
		case "$cir" in
		6)	return $FALSE ;;
		"")	cir="$defcir" ;;
		*)	VALIDATE_CIRC_NAME $cir || cir="" ;;
		esac
	done

	ADD_NODE || EXIT

	trap "" $TRAPS	# disable traps while we do the actual updates
	echo $name $dstype $cir $enet $dsload $dump >> $TEMP.dat && \
	sort $TEMP.dat > $TEMP.dsv && \
	mv $TEMP.dsv $TEMP.dat

	if [ $? != $TRUE ]
	then
		/etc/remnode $name
		[ $DECNET = $TRUE ] && /etc/remnode -P $name
		err=1
		EXIT
	fi

	cp $TEMP.dat $DATAFILE
	return $TRUE
}
#
SWAP_SERVER() {
	NO_SERVERS && return $FALSE
	errstr="swapped."
	oenet=""
	PROMPTS
	echo "Type <RET> to accept the default"

	name=""
	until [ "$name" ]
	do
		echo "\nNode name of DECserver you want to swap ? \c"
		read name
		DOWNCASE name
		case "$name" in
		6)	return $FALSE ;;
		"")	;; 
		'?')	echo "     For the SWAP option, a DECserver unit is being replaced with a  new  one."
			echo "     Please enter the DECserver node name you have entered  for  the  unit  to"
			echo "     replace.  You will then be asked the Ethernet address for the  new  unit."
			;;
		*)	VALIDATE_NAME "$name" existing ;;
		esac
	done

	node=`grep -i -w $name $TEMP.dat`
	set -- $node  # Set positional args
	oname=$name
	odstype=$2
	ocir=$3
	oenet=$4
	oifile=$5
	echo ""
	echo "DECserver at Ethernet address $oenet is being swapped."

	dstype=""
	until [ "$dstype" ]
	do
		echo "\n\nDECserver type [ $odstype ] ? \c"
		read dstype
		UPCASE dstype
		case "$dstype" in
		6)	return $FALSE ;;
		"")	echo Using default $odstype
			dstype=$odstype ;;
		*)	VALIDATE_TYPE || dstype="" ;;
		esac
	done

	name=""
	until [ "$name" ]
	do
		echo "\nDECserver node name for unit [ $oname ] ? \c"
		read name
		DOWNCASE name
		case "$name" in
		6 )	return $FALSE ;;
		'?')	DSNAME_HELP ;;
		"")	name=$oname ;;	# default is old name
		*)	VALIDATE_NAME "$name" || name="" ;;
		esac
	done

	if [ $dstype = DS500 -a "$name" = "$oname" ]
	then
		#
		# If user chooses same name for new server, then he must
		# be given the option of keeping the same image file, too,
		# IF it's a DS500 being swapped.
		#
		# Give user the option of retaining the old image
		# file of the server being swapped out ...
		#
		if [ -f "$DEST/$oifile" ]
		then
			echo "\nLoad file $oifile already exists in /usr/lib/dnet."
			echo "\nDo you wish to keep this file [YES] ?\c"
			read keep
			UPCASE keep
			case "$keep" in
			Y | YE | YES | "" )
				#
				# If they want to keep it, set a flag so
				# "ADD_NODE" will know no image file 
				# prompting or creation is necessary.
			        #
				fromswap=$TRUE
				#
				# Since they wanted to use the image that 
				# was being used for the server which was 
				# swapped out, just load the old image file 
				# name and skip asking for a new load file 
				# name ...
				#
				ifilename="$oifile"
				dsload=$ifilename
				#
				# Set the protections on image file to 
				# user=rwx,group=rx,others=r.
				#
				chmod 754 $DEST/"$ifilename"
				dstype="DS500"
				;;
			#
			# If user said anything but YES in response
			# to the question, just continue.
			*)	;;
			esac
		fi
	fi

	enet=""
	until [ "$enet" ]
	do
		echo "\nDECserver Ethernet address for this unit ? \c"
		read enet
		UPCASE enet
		case "$enet" in
		6)	return $FALSE ;;
		"")	;;
		*)	VALIDATE_ENET_ADDR "$enet" new || enet="" ;;
		esac
	done

	cir=""
	until [ "$cir" ]
	do
		echo "\nService circuit for this unit [ $ocir ] ? \c"
		read cir
		UPCASE cir
		case "$cir" in
		6)	return $FALSE ;;
		"")	cir=$ocir ;;
		*)	VALIDATE_CIRC_NAME "$cir" || cir="" ;;
		esac
	done

	trap "" $TRAPS	# Ignore interrupts while we do the following
	if [ "$oname" != "$name" ]
	then
		/etc/remnode $oname
		/etc/getnode $oname >$NULL 2>$NULL && EXIT
		if [ $DECNET = $TRUE ]
		then
			/etc/remnode $oname -P
			/etc/getnode -P $oname >$NULL 2>$NULL && EXIT
		fi
		grep -v $oname $TEMP.dat > $TEMP.dsv
		mv $TEMP.dsv $TEMP.dat
	fi

	ADD_NODE || EXIT

	grep -v -w \^$name $TEMP.dat > $TEMP.dsv
	echo $name $dstype $cir $enet $dsload $dump >> $TEMP.dsv && \
	sort $TEMP.dsv > $TEMP.dat && \
	rm $TEMP.dsv
	if [ $? != 0 ]
	then
		/etc/remnode $name
		[ $DECNET = $TRUE ] && /etc/remnode -P $name
		err=1
		EXIT
	fi

	[ -f $TEMP.dsv ] && rm $TEMP.dsv
	cp $TEMP.dat $DATAFILE
	return $TRUE
}
#
DELETE_SERVER() {
	NO_SERVERS && return $FALSE
	PROMPTS

	errstr="deleted."

	name=""
	until [ "$name" ]
	do
		echo "\nNode name of DECserver to be deleted ? \c"
		read name
		case "$name" in
		6)	ABORT=$TRUE ;;
		'?')	echo "     For the DELETE  option,  you  will  remove  a  DECserver  unit  from  the"
			echo "     database. Please enter the DECserver node name of the unit to be deleted,"
			echo "     or <6> to exit."
			DSNAME_HELP ;;
		"")	;;
		*)	VALIDATE_NAME "$name" existing || name="";;
		esac
	done

	trap "" $TRAPS
	grep -v -w "$name" $TEMP.dat > $TEMP.dsv && \
	mv $TEMP.dsv $TEMP.dat
	if [ $? != $TRUE ]
	then
		err=1
		EXIT
	fi

	/etc/remnode $name
	[ $DECNET = $TRUE ] && /etc/remnode $name -P
	echo ""
	echo "     Node $name deleted "

	[ -f $TEMP.dsv ] && rm $TEMP.dsv
	cp $TEMP.dat $DATAFILE
	sleep 1
	return $TRUE
}
#
RESTORE_SERVERS() {
	NO_SERVERS && return $FALSE
	trap "" $TRAPS
	echo ""
	echo "Restoring existing DECservers from local database..."
	awk 'length>0 {print "/etc/addnode "$1" -c "$3" -h "$4" -l "$5" -D "$6 }' $TEMP.dat >> $TEMP.dsv
	if [ $? -ne $TRUE ]
	then
		echo " "
		echo "$PROG":" awk: fatal error"
		ERR_EXIT
	fi

	if [ $DECNET = $TRUE ]
	then
		awk 'length>0 {print "/etc/addnode "$1" -c "$3" -h "$4" -l "$5" -D "$6" -P" }' $TEMP.dat>>$TEMP.dsv
		if [ $? != $TRUE ]
		then
			echo " "
			echo "$PROG":" awk: fatal error"
			ERR_EXIT
		fi
	fi
	/bin/sh5 -ef $TEMP.dsv >> $TEMP.err 2>&1
	err=$?
	[ -f $TEMP.dsv ] && rm $TEMP.dsv
	if [ $err != $TRUE ]
	then
		echo ""
		echo "$PROG":" Fatal error"
		echo "The file $TEMP.err contains the errors."
		ERR_EXIT
	fi
	[ -f $TEMP.err ] && rm $TEMP.err
	echo ""
	echo "     Local database successfully restored."
	echo " "
	sleep 1
	return $TRUE
}
#
#
# Make sure DECserver data file and our working copy of it, $TEMP.dat,
# exist. Create empty files if necessary.
#
if [ -f $DATAFILE ]
then
	cp $DATAFILE $TEMP.dat

else
	cp $NULL $TEMP.dat &&\
	cp $NULL $DATAFILE &&\
	chmod 600 $DATAFILE
fi
if [ $? != $TRUE ]	# if previous command failed...
then
	echo "$PROG: can't create $TEMP.dat"
	exit
fi

chmod 600 $TEMP.dat

#
#
# Unless the user specified the "r" argument to restore the servers
# from the datafile, print the introductory screen now.
#
if [ $RESTORE = $FALSE ]
then
	clear
	echo ""
	echo "DECserver Configuration Procedure - Version 2.4"
	echo ""
	echo "------------------------------------------------------------------"
	echo "Procedure started \c"
	date
	echo "------------------------------------------------------------------"
	echo ""
	echo "You must assign a unique node name for each "
	echo "DECserver you are going to configure."
	echo ""
	echo ""
fi
#
#
# Check the existing database. If it has an OLD format (only three
# fields per entry), then convert it to the new format before
# continuing.
#
awk '{if (NF>3) exit; else print;}' $TEMP.dat > $TEMP.dsv
if [ -s $TEMP.dsv ]
then
	echo ""
	echo "The database file dsvconfig.dat must be reformatted for common server use.  "
	cir=""
	until [ "$cir" ]
	do
		echo "Enter the service circuit-ID for all DECservers, ? for help or"
		echo  "<RET> to exit this procedure: \c"
		read cir
		UPCASE cir
		case "$cir" in
		"")	DONE ;;
		*)	VALIDATE_CIRC_NAME "$cir" || cir="" ;;
		esac
	done
	if [ ! "$cir" ]
	then
		rm $TEMP.dsv
		exit
	fi

	echo ""
	echo "Your DECserver database is being converted, please wait..."
	awk '{if(NF>3) {print} else {print $1" DS100 "$2" ps0801eng.sys "$3" "}}' $TEMP.dsv \
	    | sed -e s/\$/$cir/ | awk '{print $1" "$2" "$6" "$3" "$4" "$5}' >$TEMP.dat
	echo " "
	echo "DECserver database conversion is now complete."
fi
rm $TEMP.dsv

#
#
# If the user specified the "r" option on the command line, then just
# restore the servers and exit.
#
echo ""
echo "Please be sure to read/print your release notes."
echo "The release notes can be found in /usr/lib/dnet."
echo " "
echo "  Press <RET> to continue \c"
read dummy
sleep 2
if [ $RESTORE = $TRUE ]
then
	RESTORE_SERVERS
	DONE	# Clean up and exit
fi
#
#
# This is the main menu loop.
#
while true	# until break
do
	name=""
	enet=""
	dump=""
	node=""
	ifilename=""
	area=0
	nodenum=0

	trap continue $TRAPS
	clear
	echo " "
	echo " "
	echo "         DECserver Configuration Procedure - Version 2.4"
	echo " "
	echo "                     Menu of Options"
	echo " "
	echo "         1 - List known DECservers"
	echo "         2 - Add a DECserver"
	echo "         3 - Swap an existing DECserver"
	echo "         4 - Delete an existing DECserver"
	echo "         5 - Restore existing DECservers"
	echo "         6 - Exit from this procedure"
	echo " "
	echo "         Your selection ? \c"
	read sel
	case "$sel" in
	1)	LIST_SERVERS ;;
	2)	ADD_SERVER ;;
	3)	SWAP_SERVER ;;
	4)	DELETE_SERVER ;;
	5)	RESTORE_SERVERS ;;
	6)	DONE ;;
	"")	;;
	*)	echo "\n         Valid choices are 1 - 6 only "
		sleep 1
		;;
	esac
done

