#
#ident  "@(#)snconfig.SRC 1.7     95/12/01 SMI"
#
# Copyright (c) 1993-1994 by Sun Microsystems, Inc.
#
# Description:
# 	This script performs the SolarNet server side startup
#	configuration. It is designed to set up initial SolarNet
#	nameservice appropriate data storage, the initial
#	DHCP (Dynamic Host Configuration Protocol) configuration
#	information, and starts up the SolarNet server daemons.
#	The script will prompt the administrator for information
#	as it executes.  
#
#	snconfig command line syntax:
#
#	snconfig [-Q]		Quiet mode to suppress milestone messages
#				(defaults to print messages)
#		 [-L]		Log mode to write errors to syslog
#				(defaults is to write errors to terminal)
#		 [-F]		Force updating datastores with default
#				configurations (defaults to adding only
#				missing configuration information)
#
#	snconfig -h		displays the snconfig Usage help line.
#
#	snconfig -v		displays the snconfig version number.
#
##########################################################################
# Get error handling set up first
SCRIPT=`basename ${0}`
INTERRUPTS="1 2 3 15"
trap errorhand ${INTERRUPTS}
###########################################
#	Error Handler - trap interrupts
###########################################
errorhand() {
	trap " " ${INTERRUPTS}

	: ${NOTRAP:="1"}
	if [ ${NOTRAP} -eq 0 ] ; then
		echo "${SCRIPT}: Ignoring Interrupt."
		return 0
	else
# add any cleanup here
		if [ -z "${1}" ] ; then
			EXIT=1
		else
			EXIT="${1}"
		fi
		: ${ERROR:="${SCRIPT}: Interrupt!"}
		echo ${ERROR} | sed 's/XX/\
		/g' >&2
			exit ${EXIT}
	fi
}
###########################################
#
# Global variables
#
###########################################
#
VERSION=1.5.0.2
SOLARNETDIR=/opt/SUNWpcnet
NETINTLIST=""
#
ERR=0
NULL="\"\""
TRUE=0
FALSE=1
WARNING=1
SUCCESS=0
ERRRET=2
MAXRETRIES="XXX"
SNWAITTIME=3
NL="\015"
MIN_DHCP_BUNDLED_OS=550

#GFH
SNROOT=/opt/SUNWpcnet
SNVDIR=1.5
 
CFG_PCPRO_CLNT_CLASS=SUNW.PCNFSPRO.1.1
CFG_SUNW_PCWLAN_CLASS=SUNW.PCW.LAN
TBL_DHCP_MACRO=m
DHCP_SNCPATH_NAME="SNCpath"
DHCP_SNCBOOT_NAME="SNCboot"
DHCP_SNCLOGIN_NAME="SNClogin"
DHCP_SNCLOGOUT_NAME="SNClgout"
SNADMFW=SNadmfw
PCNFSD=Pcnfsd
SNCSERV=SNCserv
LICSERV=Licserv
 
CFG_PCPRO_SPEC=pcnfspro
 
CFG_BOOTFILE=boot.snc
CFG_LOGINFILE=login.snc
CFG_LOGOUTFILE=logout.snc
 
CFG_DHCP_BOOTDIR=$SNROOT"/"$SNVDIR"/site"
 
#We will construct the following strings
#SUNW.PCNFSPRO.1.1       m       :SNCpath="/opt/SUNWpcnet/1.5/site":SNCboot="pcnfspro\\boot.snc":SNClogin="pcnfspro\\login.snc":SNClgout="pcnfspro\\logout.snc":
 
BEGINNING="$CFG_PCPRO_CLNT_CLASS"
BEGINNING2="$CFG_SUNW_PCWLAN_CLASS"
SNCPATH="$DHCP_SNCPATH_NAME=\"$SNROOT"/"$SNVDIR"/"site\""
SNCPATH2="$DHCP_SNCPATH_NAME=\"$SNROOT"/"%CLNT_VERS%"/"site\""
BOOT="$DHCP_SNCBOOT_NAME=\"$CFG_PCPRO_SPEC\\\\$CFG_BOOTFILE\""
LOGIN="$DHCP_SNCLOGIN_NAME=\"$CFG_PCPRO_SPEC\\\\$CFG_LOGINFILE\""
LOGOUT="$DHCP_SNCLOGOUT_NAME=\"$CFG_PCPRO_SPEC\\\\$CFG_LOGOUTFILE\""
 
MACRONAME="$BEGINNING"
MACRONAME2="$BEGINNING2"
MACROVAL=":$SNCPATH:$BOOT:$LOGIN:$LOGOUT:"
MACROVAL2=":$SNCPATH2:$BOOT:$LOGIN:$LOGOUT:"
 
NOERR="0"
RESOURCE="RESOURCE"
DEFPATH="PATH"

#
# Following variables control error messages and log messages
# Default is to see errors and not log
# To suppress errors, set QUIET to "-Q"; to log, set LOG to "-L"
#
QUIET=
LOG=
#
# Following variables control updating existing information
# Default is to not overwrite existing data, but only add missing info
#
UPDATE=
#
# Following variables control debugging information...
#
MYQUIET=$FALSE
MYLOG=$FALSE
DEBUG=$FALSE
#
# Following variables control processing.  Each variable controls
# a functional section of the script.  If variable is "$TRUE",
# it is executed.  If "$FALSE", the section is skipped.
# Sections include:
#
#  VARIABLE		ACTION
#  ----------------	-------------------------------------------------
#  DO_SETUP		Set up SolarNet name service
#  DO_ADMIN		Set up SolarNet primary administrator
#  DO-CREATE		Creaete and verify initial data tables
#  DO_EXPORT		Export SolarNet client executables and config files
#  DO_DHCP_SETUP	Set up DHCP default dhcptab symbols and macros
#  DO_DHCP_NETWORK	Set up DHCP client per network IP tables
#  DO_SOLARNET_START	Install script and start SolarNet servers
#			(solarnetd, in.dhcpd)
#  DO_PCNFSD_START	Start Pcnfsd servers (script installed by package)
#			(pcnfsd, msgserv, plumd) 
#  DO_ADD_LICENSES	Add client licenses to plum
#  DO_SERVER_ADV	Advertise SolarNet services
#
DO_SETUP=$TRUE
DO_ADMIN=$TRUE
DO_CREATE=$TRUE
DO_EXPORT=$TRUE
DO_DHCP_SETUP=$TRUE
DO_DHCP_NETWORK=$TRUE
DO_SOLARNET_START=$TRUE
DO_PCNFSD_START=$TRUE
DO_ADD_LICENSES=$TRUE
DO_SERVER_ADV=$TRUE
#
# Following variables are the default values for SolarNet prompting.
# Variables include:
#
#  VARIABLE		MEANING
#  ----------------	-------------------------------------------------
#  DFLT_NAME_SERVICE	Name service for SolarNet table data stores
#  DFLT_ADMIN		User identity for SolarNet primary administrator
#  DFLT_CLIENT_OPTION	Allocate dynamic PC client IP addresses for subnets
#  DFLT_CLIENT_COUNT	Number of dynamic PC client IP addresses to allocate
#  DFLT_CLIENT_RANGE	Range of IP addresses in each subnet to search.
#			Value must be "-f nnn" or "-l nnn".  See snsubnet.
#  DFLT_ADDLICENSES	User wishes to add client licenses to plum.
#  DFLT_NUMLICENSES 	User has a license agreement for this number of licenses.
#  DFLT_LICENSECODE	There is no default license CODE1/CODE2. All are unique.
#
# If a default value is $NULL, there is no default and a value must be
# entered for the associated prompt.
#
DFLT_NAME_SERVICE="nisplus"
DFLT_ADMIN=$NULL
DFLT_NIS_ADMIN="root"
DFLT_NIS_SOURCE="/etc/yp"
DFLT_CLIENT_OPTION="yes"
DFLT_CLIENT_COUNT="16"
DFLT_CLIENT_RANGE="-l 64"
DFLT_ADDLICENSES="yes"
DFLT_NUMLICENSES="1"
DFLT_LICENSECODE=$NULL
SUNW_PCW_LAN=":SNCpath=\"/opt/SUNWpcnet/%CLNT_VERS%/site\":SNCboot=\"pcnfspro\\\\boot.snc\":SNClogin=\"pcnfspro\\\\login.snc\":SNClgout=\"pcnfspro\\\\logout.snc\":"
#
# Following variables are default SolatNet directories and scripts
#
SNMAILPATH=${SOLARNETDIR}/pcnfs/bin
SNMAILFILE=email.ini
PCNFSDIR=/opt/SUNWpcnfs
SNSCRIPTPATH=${SOLARNETDIR}/etc/init.d
PCSCRIPTPATH=${PCNFSDIR}/scripts
SNSCRIPT=solarnet
SNSCRIPTTMP="solarnet.tmp"
PCSCRIPT=pcnfs
PCNFSSTART=32${PCSCRIPT}
SOLARNETSTART=33${SNSCRIPT}
PLUMTMP="plum.tmp"
#
# Following variables are commands used in this script
#
LOGGER=/usr/ucb/logger
SNSETUP=${SOLARNETDIR}/bin/snsetup
SNADMIN=${SOLARNETDIR}/bin/snadmin
SNTABLE=${SOLARNETDIR}/bin/sntable 
SNDHCPINIT=${SOLARNETDIR}/bin/sndhcpinit
SNDHCPADD=${SOLARNETDIR}/bin/sndhcpadd
SNSUBNET=${SOLARNETDIR}/bin/snsubnet
SNCLNTINIT=${SOLARNETDIR}/bin/snclntinit
SNSERVADD=${SOLARNETDIR}/bin/snservadd
PLUM=${PCNFSDIR}/bin/plum
NISUPDATE=${SOLARNETDIR}/bin/nisupdate
DHCPTABSETUP=${SOLARNETDIR}/bin/dhcptabsetup
DHCPTABTPL=${SOLARNETDIR}/etc/dhcptab.tpl
SNTMPFILE=/tmp/snconfig.$$
TMPFILE1=/tmp/snconfig1.$$
TMPFILE2=/tmp/snconfig2.$$
TMPFILE3=/tmp/snconfig3.$$
TMPFILE4=/tmp/snconfig4.$$


FHAVE_SNadmfw=/tmp/HAVE_SNadmfw.$$
FHAVE_Pcnfsd=/tmp/HAVE_Pcnfsd.$$
FHAVE_SNCserv=/tmp/HAVE_SNCserv.$$
FHAVE_Licserv=/tmp/HAVE_Licserv.$$
#
# Following variables to export SolarNet directories and install scripts
#
SHARENFS_PART1="share -F nfs -o rw -d SolarNet"
DFSTAB=/etc/dfs/dfstab
INITD=/etc/init.d
RC3D=/etc/rc3.d
RC2D=/etc/rc2.d
RC1D=/etc/rc1.d
RC0D=/etc/rc0.d
#
# Following variables are section headers printed during execution
#
SEC_HDR_STOP="Section 1  - Stop any running PC-NFS and SolarNet Daemons"
SEC_HDR_NS="Section 2  - Set the Name Service for SolarNet Tables"
SEC_HDR_TBL="Section 3  - Create Initial SolarNet Tables"
SEC_HDR_ADM="Section 4  - Set the SolarNet Primary Administrator"
SEC_HDR_DHCP="Section 5  - Create Default DHCP Network Configurations"
SEC_HDR_CLNT="Section 6  - Configure Subnets Connected to this Server for DHCP"
SEC_HDR_EXP="Section 7  - Export SolarNet File Systems"
SEC_HDR_SND="Section 8  - Start the SolarNet Daemons"
SEC_HDR_PCD="Section 9  - Start the PC-NFS Daemons and Add Client Licenses"
SEC_HDR_CAP="Section 10 - Advertise SolarNet Services"
#
# Following variables are messages used in this script
#
MSGVERSION="The current version is "
MSGNOSUBNETS="There are no usable subnets for this system's network interfaces"
MSGSUBNETLIST="In configuring the following contiguous subnets, you can specify:\n  - whether the DHCP daemon should monitor DHCP requests on that subnet\n  - how many PC clients are to be assigned dynamically allocated IP addresses\nNOTE: Additional steps (beyond running snconfig) are required to configure other subnets.  See \"Adding a Relay Agent to a Network\" in the Administration Guide\n"
MSGDOSUBNET1="Do you want the DHCP daemon on this server to monitor subnet "
MSGDOSUBNET2="\nfor PC's requesting DHCP network configurations? (yes, no, ?, quit)"
HELPDOSUBNET="You can specify that the DHCP daemon should monitor a given subnet for requests from PC client machines for their network configurations.  The DHCP server will check its databases and return the network configurations for its clients.  Both dynamic and permanent PC clients can be configured.\n  Enter \"yes\" to monitor DHCP requests on the specified subnet.\n  Enter \"no\" to skip this subnet."
MSGNUMCLIENTS="Do you want to add dynamic PC clients for this subnet?\nIf so, how many?. (no, number, ?, quit)"
HELPNUMCLIENTS="This specifies the number of IP addresses you want reserved on this subnet for dynamic SolarNet clients -- those clients that are issued the next available IP address each time they are booted.  In calculating that number, do not include existing PC-NFS systems that will upgrade to SolarNet clients; their IP addresses are already reserved.\n\nYou can increase the number of reserved addresses later by running the snconfig command again. If you make a mistake and reserve an incorrect range of addresses, use the Client Manager to delete the addresses."
MSGRANGE="What range of IP addresses on this subnet do you want to be searched for unused addresses to allocate? (IP_address_1 IP_address_2, ?, quit)"
HELPRANGE="Enter two IP addresses (e.g., 129.148.5.100 129.148.5.200) to define the range to be searched for unused addresses.  You can choose forward or reverse search order; the SolarNet search begins with the first address you enter and searches toward the second."
MSGNSCONTFILES="Do you wish to continue with the files name service, or exit? (continue,?,quit)"
CONTINUE="continue"
HELPNS="The SolarNet server can use either nisplus, nis or files as its name service. Note that this only pertains to the data that SolarNet uses to configure clients. Clients may still have access to other name services (like NIS or NIS+ or DNS) if they are running on the server."
HELPNS1="The nisplus name service must already be running for the SolarNet server to use it."
HELPNS2="The nis name service must already be running for the SolarNet server to use it."
MSGNAMESERVICE="Enter the name service you want SolarNet to use for its data:(nisplus, nis, files, ?, quit)?"
MSGNAMESERVICE2="Enter the name service you want SolarNet to use for its data:(nis, files, ?, quit)?"
MSGADMIN="Enter the username of the primary SolarNet administrator:(username,?,quit)"
HELPADMIN="You must enter the username of an existing account.  If you are using the NIS+ nameservice, do not choose root."
MSGNISSOURCE="Enter the name of the directory containing the NIS map source files  "
HELPNISSOURCE="You must enter the name of the directory containing the NIS master's NIS map source files.  If you select /etc, your local system configuration files will be used.  The files may be in a remotely mounted directory."
MSGNISSOURCECONT="Do you wish to continue enetering another path, or exit to setup NIS source files? (continue,?,quit)"
MSGDOLIC1="Do you want to add client licenses to the plum license server? (yes, no, ?, quit)"
HELPADDLICENSES="The SolarNet PC-Admin product ships with client licenses which can be entered into the PLUM (PC-NFS License Usage Manager) license server."
MSGNUMLICENSES="Please enter the number of licenses from your license agreement. (0, number of licenses, ?, quit)"
HELPNUMLICENSES="This is the number of client licenses in your license agreement. Enter 0 to skip adding licenses and continue with configuration."
MSGLICENSEKEY="Please enter the CODE1 from your license agreement. (CODE1, ?, quit)"
HELPCODE1="The CODE1 number is the license (key) from the bottom of your license agreement."
MSGLICENSEFIRST="Please enter the CODE2 from your license agreement. (CODE2, ?, quit)"
HELPCODE2="The CODE2 number is the starting license number from the bottom of your license agreement."
MSGLICADDOK="License(s) successfully added."
MSGNEXTAGREEMENT="Do you wish to enter licenses from another agreement? (yes, no, ?, quit)"
MSGUPDATINGNISMAPS="Rebuilding NIS maps from updated source files."
MSGUSINGBUNDLEDDHCP="Using the version of DHCP bundled with this operating system."
MSGCONFIGBUNDDHCP="The bundled DHCP/BOOTP services available for this operating system must be installed and configured before running setup for PC-Admin, exiting." 
#
# Following variables are error messages for this script
#
ERRNOCMD="SN6001  The following required component command is not found: "
ERRNISPLUSNOTRUNNING="SN6002  Nisplus is not currently running. You can either continue with the files name service option, or exit from this script, start nisplus, and run snconfig again."
ERRNAMESERVICE="SN6003  The valid choices are nisplus, nis and files."
ERRNAMESERVICE1="SN6003  The valid choices are nis and files."
ERRBADADMIN="SN6004  Error trying to set up primary administrator.  Please try again."
ERRMAXADMIN="SN6005  Exceeded retry limit trying to set up primary administrator, exiting."
ERRBADNUMBER="SN6006  Invalid number of clients."
ERRBADRANGE="SN6007  Error initializing dynamically allocated IP address client entries."
ERRBADDHCPOPT="SN6008  Invalid option.  Answer \"yes\" or \"no\"."
ERRDHCPINIT="SN6009  Error trying to set up DHCP table, exiting."
ERRCLNTINIT="SN6010  Error trying to set up IP addresses, exiting."
ERRNSSETUP="SN6011  Error trying to set up SolarNet name service, exiting."
ERRPERMTABLE="SN6012  Error trying to set up SolarNet administrative access, exiting."
ERRROOT="SN6013  You must be root to run snconfig."
ERRDFSTAB="SN6014  $DFSTAB could not be located."
ERRSOLARNETDIR="SN6015  $SOLARNETDIR could not be located. Did the SolarNet installation complete without errors?"
ERRINITD="SN6016  $INITD does not exist or is not a directory."
ERRRC3D="SN6017  $RC3 does not exist or is not a directory."
ERRRC2D="SN6018  $RC2 does not exist or is not a directory."
ERRRC1D="SN6019  $RC1 does not exist or is not a directory."
ERRRC0D="SN6020  $RC0 does not exist or is not a directory."
ERRNOSOLAR="SN6021  !!! WARNING !!! SolarNet daemon processes not started!"
ERRNOPCNFS="SN6022  !!! WARNING !!! Pcnfs daemon processes not started!"
ERRLICADD="SN6023  Error adding licenses to license database."
ERRBADLICOPT="SN6024   Invalid number of license agreements."
ERRNISNOTRUNNING="SN6025  Nis is not currently running. You can either continue with the files name service option, or exit from this script, start nis, and run snconfig again."
#
###########################################
#	Handle echos: quiet or also to the log
# 	Default is echo errors to the screen
#	If log option is specified then log to SYSLOG
###########################################
myecho() {
	if [ $MYQUIET -eq $FALSE ]; then
		echo "$1"
	fi
	if [ $MYLOG -eq $TRUE ]; then
		if [ -f $LOGGER ] ; then
			$LOGGER -i -t $SCRIPT "$1" 2> /dev/null
		fi
	fi
}
###########################################
#	For debugging - be silent when not
###########################################
mydebug() {
	if [ $DEBUG -eq $TRUE ]; then
		echo "$1"
	fi
}
###########################################
#	Print Usage Message
###########################################
Print_Usage() {
	echo >&2 "Usage: snconfig [-F] [-Q] [-L]"
	echo >&2 "       snconfig  -v"
	echo >&2 "       snconfig  -h"
}
###########################################
#	Print Version Message
###########################################
Print_Version() {
	myecho "$VERSION"
}
###########################################
#	Display help messages
###########################################
Display_help() {
	echo "$1"
}
###########################################
#	Anykey - hit any key to continue
###########################################
anykey(){
echo
echo "*****Press Return to continue with SolarNet configuration*****"
read key
case $key in
	*) ;;
esac
}
###########################################
#	Enter help system
###########################################
Show_help() {
	echo "$1"
}
###########################################
#	Display section headers
###########################################
Display_header() {
	if [ "$#" -ne 0 ] ; then
		myecho "" 
		myecho "$1"
		myecho "----------"
		myecho ""
	fi
}
###########################################
#	Display error messages
###########################################
Display_error() {
	if [ "$#" -ne 0 ] ; then
		myecho "" 
		myecho "$1"
		myecho ""
	fi
}
###########################################
#	Prompt the user for input
#	$1:message $2:default $3:helptext
#	Returns what the user types in
#	also logs to syslog where appropriate
###########################################
Query() {
	
	okchoice=0
	while [ $okchoice -eq 0 ]
	do
		echo " "
		if [ "$2" = $NULL ] ; then
			echo $1 
			if [ $MYLOG -eq $TRUE ] ; then
				if [ -f $LOGGER ] ; then
					$LOGGER -i -t $SCRIPT "$1" 2> /dev/null
				fi
			fi
		else
			echo $1 "[" $2 "]" 
			if [ $MYLOG -eq $TRUE ] ; then
				if [ -f $LOGGER ] ; then
					$LOGGER -i -t $SCRIPT "$1" 2> /dev/null
				fi
			fi
		fi
		read choice
		if [ $MYLOG -eq $TRUE ] ; then
				if [ -f $LOGGER ] ; then
					$LOGGER -i -t $SCRIPT "$choice" 2> /dev/null
				fi
		fi
		case "$choice"
		in
			"q"|"Q"|"quit"|"QUIT") exit 1
				;;
			"?") Display_help "$3"
#				anykey
				continue;
				;;
			"") if [ "$2" != $NULL ] ; then
					ENTERED="$2"
					myecho "$ENTERED"
					return 0
				else
					Display_help "$3"
#					anykey
					continue;
				fi
					;;
			*) ENTERED=$choice
				return 0
		esac
	done
}
###########################################
#	Setup the DHCP data store: 
#	for the per network information
###########################################
DHCP_Network() {
# set up per network databases
# find out how many interfaces
	ERR1=0
	Display_header "${SEC_HDR_CLNT}"
	if [ ! -f $SNSUBNET ] ; then
		myecho "${ERRNOCMD} $SNSUBNET"
		ERR1=1
		return 1
	fi
	if [ ! -f $SNCLNTINIT ] ; then
		myecho "${ERRNOCMD} $SNCLNTINIT"
		ERR1=1
		return 1
	fi
	NETINTLIST=""
	mydebug "$SNSUBNET -q $LOG -i"
	SUBNETLIST=`$SNSUBNET -Q $LOG -i`
	if [ "$?" -ne 0 ] ; then
		$SNSUBNET -i
		ERR1=1
		return 1
	fi
	if [ "$SUBNETLIST" = "" ] ; then
		set ""
		NUMNETS=0
	else
		set $SUBNETLIST
		NUMNETS="$#"
	fi
	if [ $NUMNETS -eq 0 ] ; then
		myecho "${MSGNOSUBNETS}"
	else
		myecho ""
		myecho "${MSGSUBNETLIST}"
		myecho "${SUBNETLIST}"
	fi
	while [ "$1" != "" ]
	do
		SUBNET=$1
		mydebug "$SNSUBNET $QUIET $LOG -m $SUBNET"
		SUBMASK=`$SNSUBNET $QUIET $LOG -m $SUBNET`
		mydebug "$SNSUBNET $QUIET $LOG -n $SUBNET"
		SUBNAME=`$SNSUBNET $QUIET $LOG -n $SUBNET`
		DOSUBNET=""
		while [ "$DOSUBNET" = "" ]
		do
			Query "${MSGDOSUBNET1} $SUBNET $MSGDOSUBNET2" "${DFLT_CLIENT_OPTION}" "$HELPDOSUBNET"
			ANS=$ENTERED
			if [ "${ANS}" = "yes" -o "${ANS}" = "YES" -o "${ANS}" = "y" -o "${ANS}" = "Y" ] ; then
				DOSUBNET=y
			fi
			if [ "${ANS}" = "no" -o "${ANS}" = "NO" -o "${ANS}" = "n" -o "${ANS}" = "N" ] ; then
				DOSUBNET=n
			fi
			if [ "$DOSUBNET" = "" ] ; then
				Display_error "$ERRBADDHCPOPT"
			fi
		done
		if [ $DOSUBNET = "y" ] ; then
# Set the network interface option for the dhcp daemon process
			if [ "${NETINTLIST}" = "" ] ; then
				NETINTLIST="${SUBNAME}"
			else
				NETINTLIST="${NETINTLIST},${SUBNAME}"
			fi
# Set number of dynamic PC clients for this subnet
			NUMCLIENTS=""
			while [ "$NUMCLIENTS" = "" ]
			do
# Get the number of clients
				Query "${MSGNUMCLIENTS}" "$DFLT_CLIENT_COUNT" "$HELPNUMCLIENTS"
				if [ $ENTERED = "no" -o $ENTERED = "n" -o $ENTERED = "NO" -o $ENTERED = "N" ] ; then
					NUMCLIENTS="0"
				else 
					NUMCLIENTS=`expr $ENTERED + 0`
					if [ "$NUMCLIENTS" != "$ENTERED" ] ; then
						Display_error "$ERRBADNUMBER"
						NUMCLIENTS=""
					fi
				fi
# Get the range of IP addresses to search for this subnet
			if [ "${NUMCLIENTS}" != "0" -a "${NUMCLIENTS}" != "" ] ; then
				ADDRRANGE=""
				if [ "${DFLT_CLIENT_RANGE}" != $NULL ] ; then
					mydebug "$SNSUBNET $QUIET $LOG -r $SUBNET -m $SUBMASK $DFLT_CLIENT_RANGE"
					ADDRRANGE=`$SNSUBNET $QUIET $LOG -r $SUBNET -m $SUBMASK $DFLT_CLIENT_RANGE`
				fi
				Query "${MSGRANGE}" "${ADDRRANGE}" "${HELPRANGE}"
				ADDRRANGE=$ENTERED
# Create the specified number of dynamic PC clients
				mydebug "$SNCLNTINIT $QUIET $LOG -s $SUBNET -n $NUMCLIENTS -r $ADDRRANGE "
#				mydebug "$PNTADM $QUIET $LOG -C $SUBNET -f $NUMCLIENTS  
				$SNCLNTINIT $QUIET $LOG -s $SUBNET -n $NUMCLIENTS -r $ADDRRANGE
				if [ "$?" -eq $ERRRET ] ; then
					Display_error "$ERRBADRANGE"
					NUMCLIENTS=""
				fi
			fi
			done
		fi
		shift 
	done 
	return $ERR1
}
###########################################
#	NameService_Setup
#	Assumes that if a command line option
#	was entered, that it has been checked for legality
###########################################
#
NameService_Setup() {
# Determine the name service to be used
# First query what the system's name service is
# SolarNet server choices: nisplus, nis or files
#
	ERR1=0
	Display_header "${SEC_HDR_NS}"
	if [ ! -f $SNSETUP ] ; then
		myecho "${ERRNOCMD} $SNSETUP"
		ERR1=1
		return 1
	fi
# Get the name service the server is currently using: NIS+,  NIS or /etc files
# If current name service is found, use it as the default
	if [ $use_bundled_dhcp -eq $TRUE ] ; then
		mydebug "DHCP_DEFAULTS is: $DHCP_DEFAULTS"
		CURRENTNAMESERVICE=`cat $DHCP_DEFAULTS | grep -v "^#" | grep -v "^$" | tr '=' ' ' | awk '
			{
				res=""
				while( $0 )
				{
					if( $1 == reso )
						res= $2

					getline
				}
				printf("%s",res)
			}' reso=$RESOURCE `

		mydebug "CURRENTNAMESERVICE is: $CURRENTNAMESERVICE"

                CURRENTPATH=`cat $DHCP_DEFAULTS | grep -v "^#" | grep -v "^$" | tr '=' ' ' | awk '
                        {
                                res=""
                                while( $0 )
                                {
					if( $1 == defpath )
						res= $2
 
					getline
                                }
                                printf("%s",res)
                        }' defpath=$DEFPATH `
 
		mydebug "CURRENTPATH is: $CURRENTPATH"

 
	else
		CURRENTNAMESERVICE=`$SNSETUP $QUIET $LOG -q -D`
	fi

	if [ "$?" -eq $ERRRET ]; then
		CURRENTNAMESERVICE=$NULL
		ERR1=1
	fi
	if [ -n "$CURRENTNAMESERVICE" ] ; then
		DFLT_NAME_SERVICE=$CURRENTNAMESERVICE
	fi

# Query for the user's preference of name service
	okchoice=0
	while [ $okchoice -eq 0 ]
	do
		if [ $use_bundled_dhcp -eq $TRUE ] ; then
			if [ $DFLT_NAME_SERVICE = "nisplus" ] ; then
				SNNAMESERVICE="nisplus"
			else
		                Query "$MSGNAMESERVICE2" $DFLT_NAME_SERVICE "$HELPNS"
		                case "$ENTERED"
		                in
		                        "files"|"FILES") SNNAMESERVICE="files"
					LNKDST=/opt/SUNWpcnet/etc/dhcp_ip
					OLDLNKDIR=/opt/SUNWpcnet/etc/dhcp_ip.old
					mydebug "processing $LNKDST"
					if [ -f $LNKDST ] ; then
						if [ ! -d $OLDKINKDIR ] ; then
							mkdir -p $OLDLINKDIR 2>/dev/null
						fi

						mv -f $LNKDST $OLDLNKDIR  2>/dev/null
					else
						mv -f $LNKDST $OLDLNKDIR 2>/dev/null
					fi

					ln -s $CURRENTPATH $LNKDST 2> /dev/null
		                                ;;
		                        "nis"|"NIS") SNNAMESERVICE="nis"
                                        LNKDST=/opt/SUNWpcnet/etc/dhcp_ip
                                        OLDLNKDIR=/opt/SUNWpcnet/etc/dhcp_ip.old
                                        mydebug "processing $LNKDST"
                                        if [ -f $LNKDST ] ; then
                                                if [ ! -d $OLDKINKDIR ] ; then 
                                                        mkdir -p $OLDLINKDIR 2>/dev/null
                                                fi
 
                                                mv -f $LNKDST $OLDLNKDIR  2>/dev/null
                                        else
                                                mv -f $LNKDST $OLDLNKDIR 2>/dev/null
                                        fi
 
                                        ln -s $CURRENTPATH $LNKDST 2> /dev/null
		                                ;;
		                        *) Display_error "$ERRNAMESERVICE1"
		                                continue;
		                                ;;
		                esac
			fi
		else
				Query "$MSGNAMESERVICE" $DFLT_NAME_SERVICE "$HELPNS"
			case "$ENTERED"
			in
				"files"|"FILES") SNNAMESERVICE="files"
					;;
				"nisplus"|"NISPLUS") SNNAMESERVICE="nisplus"
					;;
				"nis"|"NIS") SNNAMESERVICE="nis"
					;;
				*) Display_error "$ERRNAMESERVICE"
					continue;
					;;
			esac
		fi


# For now,  it is a bit of overkill to prompt when the
# only choice is files - but for when we add more name services ...
		if [ "$SNNAMESERVICE" = "nisplus" ] ; then
			if [ "$CURRENTNAMESERVICE" != "nisplus" ] ; then
				Display_error "$ERRNISPLUSNOTRUNNING"
				Query "$MSGNSCONTFILES" $CONTINUE "$HELPNS1"
				case "$ENTERED"
				in
					"continue"|"CONTINUE"|"y"|"Y"|"yes"|"YES"|"files"|"FILES") SNNAMESERVICE="files"
						break;
						;;
					*) exit 1
				esac
			else
				break;
			fi
		else
			break;
		fi
		if [ "$SNNAMESERVICE" = "nis" ] ; then
			if [ "$CURRENTNAMESERVICE" != "nis" ] ; then
				Display_error "$ERRNISNOTRUNNING"
				Query "$MSGNSCONTFILES" $CONTINUE "$HELPNS2"
				case "$ENTERED"
				in
					"continue"|"CONTINUE"|"y"|"Y"|"yes"|"YES"|"files"|"FILES") SNNAMESERVICE="files"
						break;
						;;
					*) exit 1
				esac
			else
				break;
			fi
		else
			break;
		fi
	done

	# Set the SolarNet name service type.  Exit if error setting it up.
	# For NIS, we need to gather a few parameters first to pass to SNSETUP
	if [ "$SNNAMESERVICE" = "nis" ] ; then

		okchoice=0
		while [ $okchoice -eq 0 ]
		do
			# See if we can determine a path for the NIS sources
			YPDIR=`/usr/bin/grep '^DIR[ 	]*=' /var/yp/Makefile 2>/dev/null | /usr/bin/cut -f2 -d'='`
			if [ -d "$YPDIR" ] ; then
				DFLT_NIS_SOURCE=$YPDIR
			fi
			# Query for the NIS map source files path
			Query "$MSGNISSOURCE" $DFLT_NIS_SOURCE "$HELPNISSOURCE"
			NISSOURCE=$ENTERED
			# Store the path into the config file
			$SNSETUP $QUIET $LOG -c "$NISSOURCE"
			if [ "$?" -eq $ERRRET ] ; then
				Display_error "$ERRORNISSOURCE"
				Query "$MSGNISSOURCECONT" $CONTINUE "$HELPNISSOURCE"
				case "$ENTERED"
				in
					"continue"|"CONTINUE"|"y"|"Y"|"yes"|"YES") 
						;;
					*) exit 1
				esac
			else
				okchoice=1
			fi
		done	

	fi

	$SNSETUP $QUIET $LOG -n $SNNAMESERVICE
	if [ "$?" -eq $ERRRET ] ; then
		Display_error "$ERRORNSSETUP"
		exit 1
	fi

	return $ERR1
}

###########################################
#	InitTables
#	Creates initial tables if they do not exist
#	Verifies user can update tables if they do exist
###########################################
#
InitTables() {
# Initialize minimal name service databases
# Warning means file not found for sntable
# Includes:
#	dhcptab
#	perm
#	servers

	ERR1=0
	Display_header "${SEC_HDR_TBL}"
	myuser=`id | cut -f1 -d\) | cut -f2 -d\(`

# create dhcptab table
	mydebug "$SNTABLE -Q $LOG -t dhcptab"
	$SNTABLE -Q $LOG -t dhcptab
	if [ "$?" -eq $WARNING ] ; then
		mydebug "$SNTABLE $QUIET $LOG -c dhcptab"
		$SNTABLE $QUIET $LOG -c dhcptab
		if [ "$?" -eq $ERRRET ] ; then
			Display_error "$ERRDHCPINIT"
			exit 1
		fi
	else
		if [ "$?" -eq $ERRRET ] ; then
			$SNTABLE $QUIET $LOG -t dhcptab
		fi
	fi
# verify user can update table - errors considered warning
	mydebug "$SNTABLE $LOG -t dhcptab -u $myuser"
	$SNTABLE $LOG -t dhcptab -u $myuser

# create perm table
	mydebug "$SNTABLE -Q $LOG -t perm"
	$SNTABLE -Q $LOG -t perm
	if [ "$?" -eq $WARNING ] ; then
		mydebug "$SNTABLE $QUIET $LOG -c perm"
		$SNTABLE $QUIET $LOG -c perm
		if [ "$?" -eq $ERRRET ] ; then
			Display_error "$ERRPERMTABLE"
			exit 1
		fi
	else
		if [ "$?" -eq $ERRRET ] ; then
			$SNTABLE $QUIET $LOG -t perm
		fi
	fi
# verify user can update table - errors considered warning
	mydebug "$SNTABLE $LOG -t perm -u $myuser"
	$SNTABLE $LOG -t perm -u $myuser

# create servers table
	mydebug "$SNTABLE -Q $LOG -t servers "
	if [ ! -f $SNTABLE ] ; then
		myecho "${ERRNOCMD} $SNSETUP"
		ERR1=1
		return 1
	fi
	$SNTABLE -Q $LOG -t servers
	if [ "$?" -eq $WARNING ] ; then
		mydebug "$SNTABLE $QUIET $LOG -c servers"
		$SNTABLE $QUIET $LOG -c servers
		if [ "$?" -eq $FALSE ] ; then
# Don't exit if unable to set up the servers table
# This does not have to exist to run SolarNet
			Display_error "$ERRSERVERTABLE"
			ERR1=1
		fi
	else
# verify user can update table - errors considered warning
		mydebug "$SNTABLE $LOG -t servers -u $myuser"
		$SNTABLE $LOG -t servers -u $myuser
	fi

return $ERR1
}
###########################################
# 	Access_Setup
# 	Give the Primary Administrator access
#	to the SolarNet data stores from the PC
###########################################
Access_Setup(){
	
# Query for the primary administrator
	Display_header "${SEC_HDR_ADM}"
	USEADMIN=$DFLT_ADMIN
	Query "$MSGADMIN" $USEADMIN "$HELPADMIN"
	USEADMIN=$ENTERED

	adminok=$SUCCESS
	mydebug "$SNADMIN $QUIET $LOG -a $USEADMIN"
	if [ -f $SNADMIN ] ; then
		$SNADMIN $QUIET $LOG -a $USEADMIN
		adminok="$?"
	fi

	retries=X
	while [ $adminok -eq $ERRRET ]
	do
		if [ ${retries} = ${MAXRETRIES} ] ; then
			Display_error "$ERRMAXADMIN"
			exit 1
		fi
		retries=${retries}X
	# display an error here even if quiet mode to explain prompt
		if [ $MYQUIET -eq $TRUE ] ; then
			Display_error "$ERRBADADMIN"
		fi
		Query "$MSGADMIN" $DFLT_ADMIN "$HELPADMIN"
		USEADMIN=$ENTERED
		mydebug "$SNADMIN $QUIET $LOG -a $USEADMIN"
		if [ -f $SNADMIN ] ; then
			$SNADMIN $QUIET $LOG -a $USEADMIN
			adminok="$?"
		fi
	done

# Make primary admin owner of email.ini file
	if [ $adminok -eq 0 ] ; then
		/usr/bin/chown -f $USEADMIN ${SNMAILPATH}/${SNMAILFILE}
	fi
}

###########################################
# 	Add client licenses to plum.
#	Note: this requires access to the existing plum database
#	if there is one.
###########################################
ADD_LICENSES(){

	ERR1=0
# Get the number of licenses
	Query "$MSGNUMLICENSES" "$DFLT_NUMLICENSES" "$HELPNUMLICENSES"
	NUMLICENSES=$ENTERED

	if [ "${NUMLICENSES}" != "0" -a "${NUMLICENSES}" != "" ] ; then

# Get the CODE1 (or key) value
		Query "$MSGLICENSEKEY" "$DFLT_LICENSECODE" "$HELPCODE1"
		CODE1=$ENTERED

# Get the CODE2 (or start) value
		Query "$MSGLICENSEFIRST" "$DFLT_LICENSECODE" "$HELPCODE2"
		CODE2=$ENTERED

# Create a licensing file to use as input to plum 
		if [ -f ${PCSCRIPTPATH}/${PLUMTMP} ] ; then
			/usr/bin/rm ${PCSCRIPTPATH}/${PLUMTMP}
		fi
		echo $NUMLICENSES > ${PCSCRIPTPATH}/${PLUMTMP}
		echo $CODE1 >> ${PCSCRIPTPATH}/${PLUMTMP}
		echo $CODE2 >> ${PCSCRIPTPATH}/${PLUMTMP}


# Enter the licenses into plum from the temporary file
		if [ ! -f $PLUM ] ; then
			myecho "${ERRNOCMD} $PLUM"
			ERR1=1
			return $ERR1
		fi

		myecho "$PLUM -i ${PCSCRIPTPATH}/${PLUMTMP}"
		$PLUM -i ${PCSCRIPTPATH}/${PLUMTMP} 
		ERR1=0

	fi

# plum errors are directly sent to the screen, so they 
# are not reflected here
	return $ERR1
}
###########################################
#	snconfig - server configuration
#	Main program for configuring a
#	SolarNet server
###########################################
#
# make sure running as root
uid=`/bin/id | /bin/cut -d"=" -f2 | /bin/cut -d"(" -f1`
if [ "${uid}" != "0" ] ; then
	Display_error "$ERRROOT"
	exit 1
fi

# Parse input options
#
while getopts vhFQL option
do
	case $option in
		v) Print_Version
			exit 0
			;;
		h) Print_Usage
			exit 0
			;;
		Q) QUIET="-Q"
			MYQUIET=$TRUE
			;;
		L) LOG="-L"
			MYLOG=$TRUE
			;;
		F) UPDATE="-u"
			;;
		?) Print_Usage
			exit 0
			;;
	esac
done


#
# Check if this is a version of the OS which
# is bundled with DHCP by taking os version string
# and "normalizing" it to a three digit decimal
# without dots.

osversion=`uname -r`
suffix=0
threedigit=3
osversion=`echo "$osversion" | tr -d '.'`
numchars=`expr "$version" : '.*'`
bundled_dhcp_exists=$FALSE
bundled_dhcp_config=$FALSE

if [ $numchars -lt $threedigit ] ; then
	osversion=$osversion$suffix
fi
 
if [ $osversion -gt $MIN_DHCP_BUNDLED_OS ] ; then
	use_bundled_dhcp=$TRUE
	DHTADM=/usr/sbin/dhtadm
else
	use_bundled_dhcp=$FALSE
fi


# If version of OS is one which has a bundled
# DHCP/BOOTP service, check for existance of
# DHCP/BOOTP packages.

if [ $use_bundled_dhcp -eq $TRUE ] ; then 
	pkginfo SUNWdhcsu 2> /dev/null 1>&2 
	ERR="$?"
	if [ $ERR -ne $NOERR ] ; then
	        mydebug "SUNWdhcsu does not exist"
		bundled_dhcp_exists=$FALSE
	else
	        mydebug "SUNWdhcsu does exist"
		bundled_dhcp_exists=$TRUE
	fi
 
	pkginfo SUNWdhcsr 2> /dev/null 1>&2
	ERR="$?"
	if [ $ERR -ne $NOERR ] ; then
	        mydebug "SUNWdhcsr does not exist"
		bundled_dhcp_exists=$FALSE
	else
	        mydebug "SUNWdhcsr does exist"
		bundled_dhcp_exists=$TRUE
	fi
fi


# If Packages for OS bundled DHCP/BOOTP service
# exist, check to see if the bundled DHCP/BOOTP
# service has been configured.

if [ $bundled_dhcp_exists -eq $TRUE ] ; then
	DHCP_DEFAULTS=/etc/default/dhcp

	if [ -f $DHCP_DEFAULTS ] ; then
		bundled_dhcp_config=$TRUE
	else
		bundled_dhcp_config=$FALSE
	fi
fi


#  If the OS version should have bundled DHCP
#  and it is not set up, force user to install
#  and set it up
#
#  note:  If bundled_dhcp_exists is false, then
#	  use_bundled_dhcp will automatically be
#	  false.

if [ $use_bundled_dhcp -eq $TRUE ] ; then
	if [ $bundled_dhcp_config -eq $FALSE ] ; then
	        Display_help "$MSGCONFIGBUNDDHCP"
	        exit 1
	else
        	Display_help "$MSGUSINGBUNDLEDDHCP"
        	anykey
	fi
fi


#
# Stop running PC-NFS and SolarNet daemon processes
Display_header "${SEC_HDR_STOP}"
if [ -f ${INITD}/${SNSCRIPT} ] ; then
        ${INITD}/${SNSCRIPT} stop
        sleep ${SNWAITTIME}
fi
if [ -f ${INITD}/${PCSCRIPT} ] ; then
        ${INITD}/${PCSCRIPT} stop
fi


# Setup Name Service for SolarNet data
if [ $DO_SETUP -eq $TRUE ] ; then
	NameService_Setup
	if [ "$?" -ne 0 ] ; then
		ERR=1
	fi
fi

#
# Create and verify SolarNet tables
if [ $DO_CREATE -eq $TRUE ] ; then
	InitTables
	if [ "$?" -ne 0 ] ; then
		ERR=1
	fi
fi

#
# Give the Primary Administrator access to the SolarNet data
if [ $DO_ADMIN -eq $TRUE ] ; then
	Access_Setup
	if [ "$?" -ne 0 ] ; then
		ERR=1
	fi
fi


#
# Set up the network configuration for PCs
# set up DHCPTAB
if [ $DO_DHCP_SETUP -eq $TRUE ] ; then
if [ $use_bundled_dhcp -eq $FALSE ] ; then
# DEBUG
	Display_header "${SEC_HDR_DHCP}"
	mydebug "$SNDHCPINIT $QUIET $LOG -i $UPDATE"
	if [ -f $SNDHCPINIT ] ; then
		$SNDHCPINIT $QUIET $LOG -i $UPDATE
# Exit on error from dhcpinit
		if [ "$?" -eq $ERRRET ] ; then
			Display_error "$ERRDHCPINIT"
			exit 1
		fi
	fi
else
	Display_header "${SEC_HDR_DHCP}"
	OLDIFS=${IFS}
IFS='
'


# collect all symbol entries from
# the dhcptab
 
        if [ ! -f $TMPFILE3 ] ; then
		mydebug "collecting dhcptab symbols..."
		$DHTADM -P | grep -v "^=" | grep -v "^$" > $TMPFILE2

		exec 3<&0 <$TMPFILE2
		while read LINE
		do
			TYPE="`echo $LINE | awk '{ print $2 }'`"

			if [ "$TYPE" = "Symbol" ] ; then
				echo "$LINE" >> $TMPFILE3
			fi
		done
		exec 0<&3 3<&-

		rm -f $TMPFILE2
	fi
 
#	mydebug "DHCP Table symbols are..."
#	cat $TMPFILE3


	mydebug "Retrieving contents of dhcptab.tpl"
	/usr/bin/grep -v "^$" $DHCPTABTPL | /usr/bin/grep -v "^#" > $TMPFILE4
#	mydebug "contents of $DHCPTABTPL"
#	cat $TMPFILE4

	exec 3<&0 <$TMPFILE4
	while read ROW
	do
	  mydebug "Reading line from dhcptab.tpl: $ROW"
	  case "$ROW" in
	    "#"* ) continue ;;

	    * )	TMP="`echo $ROW | sed -e "s/SUNW.PCNFS.5.1.1' '//g"`"
#		mydebug "$TMP"
		NAME="`echo $TMP | /usr/bin/awk ' { print $1 } '`"
	        VAL="`echo $TMP | /usr/bin/awk '{ print $3,$4 }'`"
	        mydebug "$DHTADM -A -s $NAME -d $VAL"
	        $DHTADM -A -s $NAME -d $VAL 2> /dev/null
	        ERR="$?"

	        if [ $ERR -ne $NOERR ] ; then
	          case $ERR in
	            1 ) mydebug "Symbol exists, preparing to merge..."

			mydebug "Will merge for symbol: $NAME..."
# Given a symbol entry from dhcptab.tpl
# search through the dhcptab for the same
# symbol name to get its current value.
# Then, merge its current value with
# SUNW.PCNFSPRO.1.1 and SUNW.PCW.LAN

#given a row from dhcptab.tpl, we
#know what symbol we tried to
#add to dhcptab.  We use its name
#as a key in a search through dhcptab
#to find associated value

# isolate stuff to right of
# equal sign in Vendor= <str>

# Parse stuff to right of "="
# into two strings of
# <VENDOR.MACRO.NAMES> <,packet_desc>

#now that we have the vendor
#macro names, treat as a list
#and see if SUNW.PCW.LAN and
#SUNW.PCNFSPRO.1.1 are present

			NEWSYMVAL=`cat $TMPFILE3 | awk '
			{
				have_pro=0
				have_lan=0
				found=0
				res=""
				while( $0 )
				{
					name=$1
					if( name == tplnam )
					{
						found=1
						valindex= index($0,$3)
						len= length($0)
				        	maxlen= len - valindex + 1 

						remain=substr($0,valindex,maxlen)

						valindex= index(remain,"=")
						lefteqstart=1
						lefteqlen= valindex - 1
						valindex++
						len= length(remain)
						maxlen= len - valindex + 1

						lefteq=substr(remain,lefteqstart,lefteqlen)
						remain2=substr(remain,valindex,maxlen)

						valindex= index(remain2,",")
						field2start=1
						len= length(remain2)
						field2len= valindex - 1
						field3len= len - valindex + 1

						field2=substr(remain2,field2start,field2len)
						field3=substr(remain2,valindex,field3len)

						vendsize=split(field2,vendors," ")
						for( i= 1; i<= vendsize; i++ )
						{
							if( vendors[i] == pro ) 
								have_pro=1

							if( vendors[i] == lan )
								have_lan=1

							if( i == 1 )
								tmp= sprintf("%s",vendors[i])
							else
								tmp= sprintf("%s %s",res,vendors[i])

							res= tmp
						}
						if( have_pro == 0 )
						{
							tmp= sprintf("%s %s",res,pro)
							res= tmp
						}

						if( have_lan == 0 )
						{
							tmp= sprintf("%s %s",res,lan)
							res= tmp
						}

						tmp=sprintf("%s=%s%s",lefteq,res,field3)
						res=tmp

						break
					}
					getline
				} 
				printf("%s",res)
			}' pro="$CFG_PCPRO_CLNT_CLASS" \
                           lan="$CFG_SUNW_PCWLAN_CLASS" \
			   tplnam=$NAME `


			mydebug "New symbol value: $NEWSYMVAL"
			
			numchars=`expr "$NEWSYMVAL" : '.*'`

			if [ $numchars -gt 0 ] ; then
				mydebug "$DHTADM -M -s $NAME -d $NEWSYMVAL"
				$DHTADM -M -s $NAME -d $NEWSYMVAL 2> /dev/null
				ERR="$?"
				if [ $ERR -ne $NOERR ] ; then
					mydebug "ERR: $ERR"
					Display_error "$ERRDHCPINIT"
					exit 1
				fi
			else
				mydebug "There is nothing to merge"
			fi
	                ;;

	            * ) Display_error "$ERRDHCPINIT"
			exit 1
			;;
	          esac
	        fi

	        ;;
	  esac
	done
	exec 0<&3 3<&-

	rm -f $TMPFILE3
	IFS=${OLDIFS}

	mydebug "$DHTADM -A -m $MACRONAME -d $MACROVAL"
	$DHTADM -A -m $MACRONAME -d $MACROVAL 2> /dev/null
	ERR="$?"

	if [ $ERR -ne $NOERR ] ; then
	  case $ERR in
	    1 )  $DHTADM -M -m $MACRONAME -d $MACROVAL 2> /dev/null
	         ERR="$?"
	         mydebug "$DHTADM -M -m $MACRONAME -d $MACROVAL"
	         if [ $ERR -ne $NOERR ] ; then
		   Display_error "$ERRDHCPINIT"
	           exit 1
	         fi
	         ;;

	    * ) Display_error "$ERRDHCPINIT"
		exit 1
		;;
	  esac
	fi

        mydebug "$DHTADM -A -m $MACRONAME2 -d $MACROVAL2"
        $DHTADM -A -m $MACRONAME2 -d $MACROVAL2 2> /dev/null
        ERR="$?"

        if [ $ERR -ne $NOERR ] ; then
          case $ERR in
            1 )  $DHTADM -M -m $MACRONAME2 -d $MACROVAL2 2> /dev/null
                 ERR="$?"
                 mydebug "$DHTADM -M -m $MACRONAME2 -d $MACROVAL2"
                 if [ $ERR -ne $NOERR ] ; then
                   Display_error "$ERRDHCPINIT"
                   exit 1
                 fi
                 ;;

            * ) Display_error "$ERRDHCPINIT"
                exit 1
		;;
          esac
        fi

	SNHOSTNAME=`uname -n`
	TMP=`dhtadm -P | awk '
		{
			while( $0 )
			{
				if( $1 == hostnam )
				{
					printf("%s",$1)
					break
				}			
				getline
			}
		}' hostnam=$SNHOSTNAME `

	IPADDR=`ifconfig -a | head | grep inet | grep broadcast | awk '{ print $2 }'`

	VAL="`echo $TMP | awk '{print $3}'`"
	VAL="`echo $VAL | tr ':' '\012'`"
 
	NEWVAL=""

        HAVE_SNadmfw=$FALSE
        HAVE_Pcnfsd=$FALSE
        HAVE_SNCserv=$FALSE
        HAVE_Licserv=$FALSE

	echo "$VAL" |  /usr/bin/grep -v "^$" > $SNTMPFILE

	exec 3<&0 <$SNTMPFILE
	while read ITEM
	do
	  mydebug "$ITEM"

	  TMP="`echo $ITEM | tr '=' '\040'`"
	  TMP1=""
	  TMP1="`echo $TMP | awk '{ print $1 }'`"
	  TMP2=""
	  TMP2="`echo $TMP | awk '{ print $2 }'`"
 
	  mydebug "$TMP"

	  case "$TMP1" in
	    SNadmfw ) NEWVAL="$NEWVAL:$TMP1=\"$SNHOSTNAME\""
		      HAVE_SNadmfw=$TRUE ;;
	    Pcnfsd )  NEWVAL="$NEWVAL:$TMP1=$IPADDR"
		      HAVE_Pcnfsd=$TRUE ;;
	    SNCserv ) NEWVAL="$NEWVAL:$TMP1=$IPADDR"
		      HAVE_SNCserv=$TRUE ;;
	    Licserv ) NEWVAL="$NEWVAL:$TMP1=$IPADDR"
		      HAVE_Licserv=$TRUE ;;
	    ""      ) continue ;;
	    * ) mydebug "TMP2: $TMP2"
		NUMCHARS=`expr "$TMP2" : '.*'`
	        if [ $NUMCHARS -gt 0 ] ; then
		  mydebug "inside if TMP2 ne NULL"
	          NEWVAL=$NEWVAL:$TMP1=$TMP2
	        else
	          NEWVAL="$NEWVAL:$TMP1"
	        fi
	        ;;
	  esac

	  mydebug "built string: $NEWVAL"
	done
	exec 0<&3 3<&-
 
        if [ $HAVE_SNadmfw -eq $FALSE ] ; then
	  NEWVAL="$NEWVAL:$SNADMFW=\"$SNHOSTNAME\""
	fi

        if [ $HAVE_Pcnfsd -eq $FALSE ] ; then
	  NEWVAL="$NEWVAL:$PCNFSD=$IPADDR"
	fi

        if [ $HAVE_SNCserv -eq $FALSE ] ; then
	  NEWVAL="$NEWVAL:$SNCSERV=$IPADDR"
	fi

        if [ $HAVE_Licserv -eq $FALSE ] ; then
	  NEWVAL="$NEWVAL:$LICSERV=$IPADDR"
	fi

	NEWVAL="$NEWVAL:"

	mydebug "newval is: $NEWVAL"

	mydebug "$DHTADM -A -m $SNHOSTNAME -d $NEWVAL"
	$DHTADM -A -m $SNHOSTNAME -d $NEWVAL 2> /dev/null
	ERR="$?"

        if [ $ERR -ne $NOERR ] ; then
          case $ERR in
	    1 )  mydebug "$DHTADM -M -m $SNHOSTNAME -d $NEWVAL" 
		 $DHTADM -M -m $SNHOSTNAME -d $NEWVAL 2> /dev/null
                 ERR="$?"
		 mydebug "ERROR IS: $ERR  NOERROR IS: $NOERR"
                 if [ $ERR -ne $NOERR ] ; then
                   Display_error "$ERRDHCPINIT"
                   exit 1
                 fi
                 ;;
 
            * ) Display_error "$ERRDHCPINIT"
                exit 1
                ;;
          esac
        fi
fi
fi

# set up per network databases
if [ $DO_DHCP_NETWORK -eq $TRUE ] ; then
if [ $use_bundled_dhcp -eq $FALSE ] ; then
	DHCP_Network
	if [ "$?" -ne 0 ] ; then
		ERR=1
# Exit on error from clntinit
		Display_error "$ERRCLNTINIT"
		exit 1
	fi
fi
fi

#
# Export SolarNet executables, cfg, lang, etc.
# Export and start now, also set up to start at next reboot
# Export all as RW in /etc/dfs/dfstab
# start mountd and nfsd if needed
# see share command
if [ $DO_EXPORT -eq $TRUE ] ; then
	Display_header "${SEC_HDR_EXP}"
	if [ -d $SOLARNETDIR ] ; then
		mydebug "$SNSETUP $QUIET $LOG -p"
		SHAREDIR=`$SNSETUP $QUIET $LOG -p`
		if [ "${SHAREDIR}" = "" ] ; then
			SHAREDIR="$SOLARNETDIR"
		fi
		SHARENFS="${SHARENFS_PART1} ${SHAREDIR}"
		$SHARENFS
# Start mountd and nfsd if not already running
		MOUNTD_PID=`/usr/bin/ps -def | /usr/bin/grep -v grep |
			/usr/bin/grep /usr/lib/nfs/mountd | /usr/bin/awk '{print $2}'`
		if [ "$MOUNTD_PID" = "" ] ; then
			/usr/lib/nfs/mountd
		fi
		NFSD_PID=`/usr/bin/ps -def | /usr/bin/grep -v grep |
			/usr/bin/grep /usr/lib/nfs/nfsd | /usr/bin/awk '{print $2}'`
		if [ "$NFSD_PID" = "" ] ; then
			/usr/lib/nfs/nfsd -a 8
		fi
		
		if [ -f $DFSTAB ] ; then
			DFSTEMP=`egrep "${SHARENFS}" $DFSTAB`
			if [ "${DFSTEMP}" = "" ] ; then
				echo $SHARENFS  >> $DFSTAB &
			fi
		else
			echo $SHARENFS > $DFSTAB &
		fi
	else
		Display_error "$ERRSOLARNETDIR"
	fi
fi
#
# Install and Start SolarNet daemons:  solarnetd, in.dhcpd
# modify start/stop script template for DHCP interface option
# copy modified script to /etc/init.d
# create link to start daemons in /etc/rc3.d
# create links to stop daemons in /etc/rc0.d, rc1.d, and rc2.d
# Wait a configured number of seconds in case solarnetd stopped earlier
# start daemons again
#
if [ $DO_SOLARNET_START -eq $TRUE ] ; then
	ERR1=0
	Display_header "${SEC_HDR_SND}"
	rm -r ${SNSCRIPTPATH}/${SNSCRIPTTMP} 2>/dev/null
	if [ "${NETINTLIST}" != "" ] ; then
		/usr/bin/sed -e "s:INTERFACE_PLACEHOLD:-i ${NETINTLIST}:" \
			     -e "s:#DHCP::" \
		${SNSCRIPTPATH}/${SNSCRIPT} > ${SNSCRIPTPATH}/${SNSCRIPTTMP}
	else
		cp ${SNSCRIPTPATH}/${SNSCRIPT} ${SNSCRIPTPATH}/${SNSCRIPTTMP}
	fi
	if [ -d $INITD ] ; then
		cp ${SNSCRIPTPATH}/${SNSCRIPTTMP} ${INITD}/${SNSCRIPT}
		if [ "$?" -ne 0 ] ; then
			ERR1=1
		else
			chmod 0744 ${INITD}/${SNSCRIPT}
		fi
	else
		Display_error "$ERRINITD"
		ERR1=1
	fi
	if [ $ERR1 -eq 0 ] ; then
		if [ -d $RC3D ] ; then
			if [ -f ${RC3D}/S${SOLARNETSTART} ] ; then
				`rm ${RC3D}/S${SOLARNETSTART}`
			fi
			`ln -s  ${INITD}/${SNSCRIPT} ${RC3D}/S${SOLARNETSTART}`
		else
			Display_error "$ERRRC3D"
			ERR1=1
		fi
		if [ -d $RC2D ] ; then
			if [ -f ${RC2D}/K${SOLARNETSTART} ] ; then
				`rm ${RC2D}/K${SOLARNETSTART}`
			fi
			`ln -s ${INITD}/${SNSCRIPT} ${RC2D}/K${SOLARNETSTART}`
		else
			Display_error "$ERRRC2D"
			ERR1=1
		fi
		if [ -d $RC1D ] ; then
			if [ -f ${RC1D}/K${SOLARNETSTART} ] ; then
				`rm ${RC1D}/K${SOLARNETSTART}`
			fi
			`ln -s ${INITD}/${SNSCRIPT} ${RC1D}/K${SOLARNETSTART}`
		else
			Display_error "$ERRRC1D"
			ERR1=1
		fi
		if [ -d $RC0D ] ; then
			if [ -f ${RC0D}/K${SOLARNETSTART} ] ; then
				`rm ${RC0D}/K${SOLARNETSTART}`
			fi
			`ln -s ${INITD}/${SNSCRIPT} ${RC0D}/K${SOLARNETSTART}`
		else
			Display_error "$ERRRC0D"
			ERR1=1
		fi
	fi
	if [ $ERR1 -eq 0 ] ; then
		${INITD}/${SNSCRIPT} start
	else
		Display_error "$ERRNOSOLAR"
	fi
	if [ -f ${SNSCRIPTPATH}/${SNSCRIPTTMP} ] ; then
		rm -r ${SNSCRIPTPATH}/${SNSCRIPTTMP} 2>/dev/null
	fi
fi

#
# Add client licenses to plum
#
if [ $DO_ADD_LICENSES -eq $TRUE ] ; then

# first find out if they want to add licenses at all
	DOLICENSES="1"
	Query "${MSGDOLIC1}" "${DFLT_ADDLICENSES}" "$HELPADDLICENSES" 

	while [ "${DOLICENSES}" != "" ] 
	do
		if [ $ENTERED = "yes" -o $ENTERED = "y" -o $ENTERED = "YES" -o $ENTERED = "Y" ] ; then
			ADD_LICENSES
			DOLICENSES="1"
# ask if they want to do additional license agreements
			Query "${MSGNEXTAGREEMENT}" "${DFLT_ADDLICENSES}" "$HELPADDLICENSES" 
		else
			DOLICENSES=""

		fi
	done

fi


# Start the Pcnfs daemons:  pcnfsd, msgserv, plumd
#	Note that the start/stop script is installed as part of the
#	SUNWpcnfs package installation.  A special preinstall script
#	for SolarNet (substituted for the SolarNet version of the SUNWpcnfs
#	package) does NOT start the daemons.  This allows the starting
#	of the daemons to be controlled by this script.
#
if [ $DO_PCNFSD_START -eq $TRUE ] ; then
	Display_header "${SEC_HDR_PCD}"
	ERR1=0
	if [ -f ${INITD}/${PCSCRIPT} ] ; then
		${INITD}/${PCSCRIPT} start
	else
		Display_error "$ERRNOPCNFS"
	fi
fi
#
# If servers table exists, add SolarNet capabilities for this system
# Note that solarnetd daemon must be running to do snservadd commands!
# Ignore errors for now!
#
if [ $DO_SERVER_ADV -eq $TRUE ] ; then
	Display_header "${SEC_HDR_CAP}"
	mydebug "$SNTABLE -Q $LOG -t servers "
	$SNTABLE -Q $LOG -t servers
	if [ "$?" -eq 0 ] ; then
		mydebug "$SNSERVADD -Q $LOG -s solarnet"
		myhost=`uname -n`
		if [ $DO_SOLARNET_START -eq $TRUE ] ; then
			$SNSERVADD -s $myhost -c solarnetd -m "SolarNet server" 2>/dev/null
			if [ $use_bundled_dhcp -eq $TRUE ] ; then
				$SNSERVADD -s $myhost -c dhcpd -m "OS Bundled DHCP server" 2>/dev/null
			else
				$SNSERVADD -s $myhost -c dhcpd -m "DHCP server" 2>/dev/null
			fi
		fi
		if [ $DO_PCNFSD_START -eq $TRUE ] ; then
			$SNSERVADD -s $myhost -c pcnfsd -m "PCNFS server" 2>/dev/null
			$SNSERVADD -s $myhost -c msgserv -m "PCNFS message server" 2>/dev/null
			$SNSERVADD -s $myhost -c plumd -m "PCNFS license server" 2>/dev/null
		fi
	fi
	# flush the updates to the nis maps so the clients will see them
	if [ "$SNNAMESERVICE" =	"nis" ]
	then
		echo $MSGUPDATINGNISMAPS
		$NISUPDATE -a flush >/dev/null
	fi
fi

# Ignore error from dhcpadd
	mydebug "${SNDHCPADD} -m SUNW.PCW.LAN -d ${SUNW_PCW_LAN} 2>/dev/null"
	if [ -f ${SNDHCPADD} ] ; then
		${SNDHCPADD} -m SUNW.PCW.LAN -d ${SUNW_PCW_LAN} 2>/dev/null
	fi

# Send a SIGHUP to DHCP so that it rereads the dhcptab
	pid=`/usr/bin/ps -ef | 
		/usr/bin/grep /opt/SUNWpcnet/sbin/in.dhcpd |
		/usr/bin/grep -v grep |
		/usr/bin/sed -e 's/^  *//' -e 's/  */ /' |
		/usr/bin/cut -d' ' -f2`
	if [ "${pid}" != "" ] ; then
		echo "Sending SIGHUP to DHCP daemon"
		kill -1 ${pid} > /dev/null 2>&1
	fi

#
exit $ERR
