#!/bin/ksh -p
#
# ident "@(#)dhcp_config_solaris.ksh	1.9 05/01/18 SMI"
#
# Copyright 2004-5 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

export PATH=/usr/bin:/sbin:/usr/sbin
UTLIB="$(/bin/pkginfo -r SUNWuto)/SUNWut/lib"
UTSBIN="$(/bin/pkginfo -r SUNWuto)/SUNWut/sbin"
PROGRAM_ID=$(basename $0)
LOGFILE="/var/adm/log/${PROGRAM_ID}.$(date '+%Y_%m_%d_%H:%M:%S').log"
ETCDIR="/etc/opt/SUNWut"
UTDHCPDIR="$ETCDIR"/net/dhcp
UTDHCPFILE="$UTDHCPDIR"/utdhcp
VARDIR=/var/opt/SUNWut
TMPDIR="$VARDIR"/tmp
HOSTNAME=`hostname`
HOSTIP=`getent hosts ${HOSTNAME} | awk '{print $1}'`

#
# This variable is used as a flag during dhcp configuration.
# If it is "true" then it means that prior to S8U5 there
# was no mechanism of using dhcpconfig non-interactively.
# So we manipulate the /etc/default/dhcp file directly.
# If it "false" then we know that this is a post S8U5
# machine and so we have a mechanism for non-interactive
# dhcpconfig. In this case we do not depend on the
# location of the dhcp files.
#
USE_FILES=true

# names of files and directories that will be touched by this script
#
DHCP_DIR="/var/dhcp";
HOSTNAME_R="/etc/hostname";     # with suffix .{hme,qfe,vge}
HOSTS="/etc/inet/hosts";
NETWORKS="/etc/inet/networks";
INIT_DHCP="/etc/init.d/dhcp";
CORONA_NAME="SunRay";
CORONA_TITLE="Sun Ray";
GREP=/usr/xpg4/bin/grep
DHTADM_P="${TMPDIR}/dhtadm-P.$$"

#
# Variables that will be used ONLY if USE_FILES=true
#
DHCPTAB="${DHCP_DIR}/dhcptab";
DHCP_CONFIG="/etc/default/dhcp";

#
# initialize various dhcp network tables.
#  usage:  BuildNetworkTables
#  builds the network tables for all of the newtnet interfaces in ${NEWT_INTFS}
#  returns 0 on success, and 1 on failure
#
function BuildNetworkTables {
    mode=$1

    if [[ $mode = "subnet" ]]; then
        INTF_NET=${UT_DHCP_NETWORK}
        INTF_NETNAME=${CORONA_NAME}-${UT_DHCP_NETWORK}
    else
	INTF_NET=${UT_DHCP_NETWORK}
	INTF_NETNAME=${CORONA_NAME}-${UT_DHCP_INTERFACE}
    fi

    if [[ -z $UT_DHCP_RANGE ]]; then
	START_NUM=0
	END_NUM=0
    else
	START_NUM=${UT_DHCP_RANGE% *}
	END_NUM=${UT_DHCP_RANGE#* }
	IP2Host "${START_NUM}" ${UT_DHCP_NETMASK}
	if [[ $? != 0 ]]; then
		print -u2 "Error: could not get host number from ${START_NUM}"
		exit 1
	fi
	if [[ $H = $MAXHOST ]]; then
		print -u2 "Error: new first address is subnet broadcast address";
		exit 1
	fi
	if [[ $H = 0 ]]; then
		print -u2 "Error: new first address is subnet address";
		exit 1
	fi
       	START_NUM=$H
	IP2Host "${END_NUM}" ${UT_DHCP_NETMASK}
	if [[ $? != 0 ]]; then
		print -u2 "Error: could not get host number from ${END_NUM}"
                exit 1
        fi
	if [[ $H = $MAXHOST ]]; then
                print -u2 "Error: new last address is subnet broadcast address";
                exit 1
        fi
        if [[ $H = 0 ]]; then
                print -u2 "Error: new last address is subnet address";
                exit 1
        fi
	END_NUM=$H
    fi

    if [[ ${START_NUM} = "0" ]]; then
        # in order to support DHCPINFORM, an empty network table must
        # be created.
        pntadm -C ${INTF_NET} 2> ${TMPDIR}/Err.$$
        if [[ ${?} != "0" ]]; then
            print -u2 "Error:  cannot create DHCP network table for \"${INTF_NET}\":\n     `cat ${TMPDIR}/Err.$$`"
            rm -f ${TMPDIR}/Err.$$;
            return 1
        fi
        rm -f ${TMPDIR}/Err.$$;
        return 0
    fi

    if [[ "${MULTINET}" = "N" ]]; then
        pntadm -C ${INTF_NET} 2> ${TMPDIR}/Err.$$
        if [[ ${?} -ne 0 ]]; then
            print -u2 "Error:  cannot create DHCP network table for \"${INTF_NET}\":\n    `cat ${TMPDIR}/Err.$$`"
            rm -f ${TMPDIR}/Err.$$;
            return 1;
        else
            rm -f ${TMPDIR}/Err.$$;
        fi
    fi

    # add entries to the dhcp network table
    NUM=${START_NUM};
    INPOOL=0;
    typeset TMPBATCH=${TMPDIR}/pntadm-batch.$$
    rm -f $TMPBATCH
    touch $TMPBATCH
    MaxHostNum ${UT_DHCP_NETMASK}
    MAXHOST=$H
    while [ ${NUM} -le ${END_NUM} ]; do
      NET=$UT_DHCP_NETWORK
      NetPlusHost ${NET} ${NUM} ${MAXHOST}
      ADDR=$IP
      if [[ ${UT_DHCP_IP} = ${ADDR} ]]; then
        print "### Skipping host address ${UT_DHCP_IP} in IP address pool";
        let NUM=${NUM}+1;
	continue;
      fi
      if [[ ${UT_DHCP_FIRMWARESRVR} = ${ADDR} ]]; then
        print "### Skipping firmware server address ${UT_DHCP_FIRMWARESRVR} in IP address pool";
        let NUM=${NUM}+1;
        continue;
      fi
      let INPOOL=${INPOOL}+1;
      print "pntadm -A ${ADDR} -m ${INTF_NETNAME} -s ${HOSTNAME} ${INTF_NET}" \
        >> $TMPBATCH
      let NUM=${NUM}+1;
    done

    # CR 6217989 - dhtadm -B errors on Solaris x86
    DoDhcpBatch pntadm $TMPBATCH 2> ${TMPDIR}/Err.$$;
    if [ ${?} -ne 0 ]; then
        print -u2 "Error:  failed to add the following IP addresses to network\
                \ntable for \"${INTF_NET}\":\n    `cat ${TMPDIR}/Err.$$`"
        rm -f ${TMPBATCH} ${TMPDIR}/Err.$$;
        return 1;
    fi
    rm -f ${TMPBATCH} ${TMPDIR}/Err.$$;

    if [ ${INPOOL} -eq 0 ]; then
      print -u2 "Warning: IP address pool for network ${INTF_NET} is empty";
    fi

    return 0;
}

#
# generic routine to retrieve a macro from dhcp table
# DHTADM_P is the file containing dhtadm -P output generated from function
# GetCurrentCfg
# Parameter $1: is the macro to desired
#
GetDHCPMacro() {
    if [[ -s $DHTADM_P ]]; then
        cat ${DHTADM_P} |
            nawk '
                {
                    if (($1 == macro) && ($2 == "Macro")) {
                        for (i = 3; i <= NF; ++i) printf ("%s ", $i)
                        printf ("\n")
                    }
                }
                ' macro=${1} - | \
                tr ':' '\012'
    fi
}

#
# generic routine to retrieve a symbol from a macro file that's generated
# by GetDHCPMacro.
# Parameter $1: is the symbol to extract
#           $2: the filename containing fields for the macro
#
GetDHCPSymbol() {

    if [ $# -ne 2 -o ! -s "$2" ]; then
        return 1
    fi
    typeset VAL=`grep "^${1}\>" $2`
    if [ -n "$VAL" ]; then
        print $VAL | sed -e "s/^${1}=\"*\(.*[^\"]\)\"*$/\1/"
        return 0
    fi
    return 1
}

# get the current default SunRay macro parameters
GetSRDefaultMacro() {

    rm -f ${TMPDIR}/srdefaults.$$ 
    GetDHCPMacro ${CORONA_NAME} > ${TMPDIR}/srdefaults.$$
}


#
# get the value for the specified symbol from the srdefaults.$$ file
# it prints the value and reutrn status 0 if found.  Else, returns 1.
# $1 the specified symbol
#
GetSRDefaultSym() {

    if [[ -z "$1" ]]; then
        print ""
        return 1
    fi
    GetDHCPSymbol "$1" "${TMPDIR}/srdefaults.$$"
    return $?
}

#
# removes the DHCP info
#
DoRemoveDHCP() {

    if $SUBNETMODE; then
        # deleting subnetwork
        INTF_NETNAME=${CORONA_NAME}-${SUBNET_DEL}
        INTF_NET=${SUBNET_DEL}
    else
        # deleting interface
        INTF_NETNAME=${CORONA_NAME}-${DHCP_DEL}
        INTF_NET=`awk '{ if ($1 == interface) { print $2 } }' interface=${INTF_NETNAME} ${NETWORKS}`
        if [ -z "${INTF_NET}" ]; then
            # not defined in the networks file, try checking in dhtadm
            INTF_NET=`$GREP -s ":Include=${INTF_NETNAME}:" $DHTADM_P | awk '{print $1}'`
            if [ -z "${INTF_NET}" ]; then
                print -u2 "Error: Cannot translate interface \"${DHCP_DEL}\" to a network number"
                return 1
            fi
        fi
    fi

    typeset MULTINET=N
    typeset TMPFILE=${TMPDIR}/pntadm-multi.$$
    typeset retcode=0
    typeset pntstat

    rm -f ${TMPFILE} 2> /dev/null
    pntadm -P ${INTF_NET} | sed -e '1,2d' > ${TMPFILE}
    pntstat=${?}
    # check for MULTINET by looking into the network table, not the networks file
    if $GREP -q -s -v "${INTF_NETNAME}" ${TMPFILE}; then
        MULTINET=Y
    fi

    if $SUBNETMODE; then
        print "### Removing ${CORONA_TITLE} information for subnetwork \"${SUBNET_DEL}\""
    else
        print "### Removing ${CORONA_TITLE} information for interface \"${DHCP_DEL}\""
    fi
    if [ $MULTINET = "N" ]; then
        dhtadm -D -m "${INTF_NET}";
        if [[ $pntstat -eq  0 ]]; then
            # remove the network table.
            pntadm -R "${INTF_NET}"
        fi
    else
        typeset TMPBATCH=${TMPDIR}/pntadm-rm-batch.$$
        rm -f $TMPBATCH 2> /dev/null
        awk "/${INTF_NETNAME}/ { print \"pntadm -D \" \$3 \" ${INTF_NET}\" }" ${TMPFILE} \
                > $TMPBATCH
        if [[ -s $TMPBATCH ]]; then
	    # CR 6217989 - dhtadm -B errors on Solaris x86
	    DoDhcpBatch pntadm $TMPBATCH 2> ${TMPDIR}/Err.$$;
            if [[ ${?} != 0 ]]; then
                print -u2 "Error:  failed to remove the entries for subnetwork \"${INTF_NET}\":\n\
                        `cat ${TMPDIR}/Err.$$`"
                # even when it failed, continue with the removal
		retcode=1
            fi
            rm -f $TMPBATCH ${TMPDIR}/Err.$$ 2> /dev/null
        fi
    fi
    rm ${TMPFILE}
    if ! dhtadm -D -m ${INTF_NETNAME}; then
        retcode=1
    fi
    return $retcode
}

#
# get parameter in the following order:
# 1. macro referenced from network table (eg. SunRay-hme0)
# 2. macro included from referenced macro (eg. SunRay)
# 3. network macro (eg. 192.168.129.0)
# The caller is to provide the files generated from GetDHCPMacro and they
# should be specified in the order specified above.
# Parameters:
# $1 the symbol to retrieve
# $2 the file containing info for #1
# $3 the file containing info for #2
# $4 the file containing info for #3
#
# Returns 0 if found, else 1
#
getDHCPSymbolFromList() {

    typeset value
    if value=`GetDHCPSymbol $1 $2`; then
        print $value
        return 0
    fi
    if value=`GetDHCPSymbol $1 $3`; then
        print $value
        return 0
    fi
    if value=`GetDHCPSymbol $1 $4`; then
        print $value
        return 0
    fi
    return 1
}

#
# get a list of current Sunray interfaces and subnetworks.
# This routine also create many output files which will be used later on.
# This is done to avoid having to call java commands like dhtadm again.
# Here are a list of files created under the ${TMPDIR} directory.
#	dhtadm-P.$$ contains the current dhtadm -P output without the first
#		2 header lines.
#	newtifs.$$ contains the list of sunray interfaces currently defined in
#		the /etc/networks file.
#	dhcpifs.$$ contains the list of sunray interfaces that are listed by
#		the SunRay-<intfs> macros in the dhcp table.
#	allifs.$$ contains all the interfaces for SunRay network.
#	allnets.$$ contains the list of sunray subnetworks that are listed
#		by the SunRay-<subnet> macros in the dhcp table.
#	allSRnets.$$ contains the list of all sunray subnets, both subnetworks
#		and interfaces.
#	allethers.$$ contains the list of sunray macaddresses.
#
function GetCurrentCfg {

    rm -f $DHTADM_P 2> /dev/null
    dhtadm -P 2>/dev/null | sed -e "1,2d" > $DHTADM_P

    # get interfaces that currently configure to support SunRay
    sed -n "s/^${CORONA_NAME}-\([^0-9 	][^ 	]*\)[ 	].*$/\1/p" ${NETWORKS} \
	>${TMPDIR}/newtifs.$$
    sed -n "s/.*${CORONA_NAME}-\([^0-9 	][^\": 	]*\).*$/\1/p" \
	$DHTADM_P | sort -u >${TMPDIR}/dhcpifs.$$
    sort -u ${TMPDIR}/newtifs.$$ ${TMPDIR}/dhcpifs.$$ >${TMPDIR}/allifs.$$

    # get subnetworks that are currently configured for SunRay
    # this does not include the networks for interfaces.
    sed -n -e \
	"s/.*${CORONA_NAME}-\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)[^0-9].*$/\1/p" \
      	$DHTADM_P | sort -u >${TMPDIR}/allnets.$$

    # get all sunray networks, including the ones for the interfaces
    egrep "${CORONA_NAME}|Intf=" $DHTADM_P | \
    awk '/^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*/ { print $1 }' \
	>${TMPDIR}/dhcpallnets.$$
    sort -u ${TMPDIR}/dhcpallnets.$$ ${TMPDIR}/allnets.$$ \
	> ${TMPDIR}/allSRnets.$$

    sed -n "s/^\(01[0-9a-zA-Z]*\).*$/\1/p" $DHTADM_P > ${TMPDIR}/allethers.$$

    rm ${TMPDIR}/dhcpallnets.$$ 2> /dev/null
}

ConstructOptionsMacro() {
    CORONA_MACRO=":"
    if [[ -n $UT_DHCP_AUTHPORT ]]; then
		CORONA_MACRO="${CORONA_MACRO}AuthPort=${UT_DHCP_AUTHPORT}:"
    fi
    if [[ -n $UT_DHCP_NEWTVER ]]; then
		CORONA_MACRO="${CORONA_MACRO}NewTVer=\"${UT_DHCP_NEWTVER}\":"
    fi
    if [[ -n $UT_DHCP_LOGHOST ]]; then
		CORONA_MACRO="${CORONA_MACRO}LogHost=${UT_DHCP_LOGHOST}:"
    fi
    if [[ -n $UT_DHCP_LOGKERNEL ]]; then
		CORONA_MACRO="${CORONA_MACRO}LogKern=${UT_DHCP_LOGKERNEL}:"
    fi
    if [[ -n $UT_DHCP_LOGNET ]]; then
    		CORONA_MACRO="${CORONA_MACRO}LogNet=${UT_DHCP_LOGNET}:"
    fi
    if [[ -n $UT_DHCP_LOGUSB ]]; then
    		CORONA_MACRO="${CORONA_MACRO}LogUSB=${UT_DHCP_LOGUSB}:"
    fi
    if [[ -n $UT_DHCP_LOGVIDEO ]]; then
    		CORONA_MACRO="${CORONA_MACRO}LogVid=${UT_DHCP_LOGVIDEO}:"
    fi
    if [[ -n $UT_DHCP_LOGAPPLICATION ]]; then
    		CORONA_MACRO="${CORONA_MACRO}LogAppl=${UT_DHCP_LOGAPPLICATION}:"
    fi
    if [[ -n $UT_DHCP_NEWTBANDWIDTH ]]; then
    		CORONA_MACRO="${CORONA_MACRO}NewTBW=${UT_DHCP_NEWTBANDWIDTH}:"
    fi
    if [[ -n $UT_DHCP_BARRIERLEVEL ]]; then
    		CORONA_MACRO="${CORONA_MACRO}BarrierLevel=${UT_DHCP_BARRIERLEVEL}:"
    fi
    if [[ -n $UT_DHCP_NEWTFLAGS ]]; then
    		CORONA_MACRO="${CORONA_MACRO}NewTFlags=${UT_DHCP_NEWTFLAGS}:"
    fi
    if [[ -n $UT_DHCP_NEWTDISPINDEX ]]; then
    		CORONA_MACRO="${CORONA_MACRO}NewTDispIndx=${UT_DHCP_NEWTDISPINDEX}:"
    fi
}

#
# list all the SR subnetworks currently configured
#
TranslateDhtadmP() {
    ISSETUP=false
    if $GREP -q -w "${CORONA_NAME}" ${DHTADM_P}; then
	ISSETUP=true
    fi

    if ! $ISSETUP; then
            # any other case, this is considered an error
            exit 1;
    fi
    rm -f $UTDHCPFILE 2> /dev/null
    TranslateSunRayOptionsBlock
    if [ -s ${TMPDIR}/allSRnets.$$ ]; then
    	TranslateSubnetInterface
    fi
}

TranslateSunRayOptionsBlock() {
	typeset sunray_macro="${TMPDIR}/sunray_tmp.$$"
	typeset ether_macro="${TMPDIR}/ether_tmp.$$"
	GetDHCPMacro "$CORONA_NAME" > $sunray_macro
	if [[ ! -s $sunray_macro ]]; then
		rm -f $sunray_macro 2> /dev/null
	else
		print "begin options" >> $UTDHCPFILE
		TranslateSunRayOptions $sunray_macro
		print "end" >> $UTDHCPFILE
	fi
	
	for ether in `cat ${TMPDIR}/allethers.$$`; do
		GetDHCPMacro "$ether" > $ether_macro
		if [[ ! -s $ether_macro ]]; then
			rm -f $ether_macro 2> /dev/null
		else
			ether=${ether#01}
			print "begin ether" >> $UTDHCPFILE
			print "macaddress=$ether" >> $UTDHCPFILE
			TranslateSunRayOptions $ether_macro
			print "end" >> $UTDHCPFILE
		fi
	done
	return 0
}

TranslateSunRayOptions() {
	macro_file=$1

	if AuthPort=`GetDHCPSymbol AuthPort $macro_file`; then
		print "authport=$AuthPort" >> $UTDHCPFILE
	fi
	if NewTVer=`GetDHCPSymbol NewTVer $macro_file`; then
                print "newtver=$NewTVer" >> $UTDHCPFILE
        fi
	if LogHost=`GetDHCPSymbol LogHost $macro_file`; then
                print "loghost=$LogHost" >> $UTDHCPFILE
        fi
	if LogKern=`GetDHCPSymbol LogKern $macro_file`; then
                print "logkernel=$LogKern" >> $UTDHCPFILE
        fi
	if LogNet=`GetDHCPSymbol LogNet $macro_file`; then
                print "lognet=$LogNet" >> $UTDHCPFILE
        fi
	if LogUSB=`GetDHCPSymbol LogUSB $macro_file`; then
                print "logusb=$LogUSB" >> $UTDHCPFILE
        fi
	if LogVid=`GetDHCPSymbol LogVid $macro_file`; then
                print "logvideo=$LogVid" >> $UTDHCPFILE
        fi
	if LogAppl=`GetDHCPSymbol LogAppl $macro_file`; then
                print "logapplication=$LogAppl" >> $UTDHCPFILE
        fi
	if NewTBW=`GetDHCPSymbol NewTBW $macro_file`; then
                print "newtbandwidth=$NewTBW" >> $UTDHCPFILE
        fi
	if BarrierLevel=`GetDHCPSymbol BarrierLevel $macro_file`; then
                print "barrierlevel=$BarrierLevel" >> $UTDHCPFILE
        fi
	if NewTFlags=`GetDHCPSymbol NewTFlags $macro_file`; then
                print "newtflags=$NewTFlags" >> $UTDHCPFILE
        fi
	if NewTDispIndx=`GetDHCPSymbol NewTDispIndx $macro_file`; then
                print "newtdispindex=$NewTDispIndx" >> $UTDHCPFILE
        fi
}

TranslateSubnetInterface() {

    typeset Netmask
    typeset Broadcast
    typeset Router
    typeset FWSrvr
    typeset AuthSrvr
    typeset AltAuthSrvr
    typeset netmacro
    typeset Intf
    typeset IntfAddr
    typeset IPassign
    typeset FirstIP
    typeset LastIP
    typeset NewTVer
    typeset MTU
    typeset today
    typeset network_macro="${TMPDIR}/network_tmp.$$"
    typeset refer_macro="${TMPDIR}/netrefer_tmp.$$"
    typeset sunray_macro="${TMPDIR}/sunray_tmp.$$"
    typeset TSUBNET

    rm -f $sunray_macro 2> /dev/null
    GetDHCPMacro "$CORONA_NAME" > $sunray_macro
    if [[ ! -s $sunray_macro ]]; then
        rm -f $sunray_macro 2> /dev/null
    fi

    for TSUBNET in `cat ${TMPDIR}/allSRnets.$$`; do
        # setup the network macro file
        rm -f $network_macro 2> /dev/null
        GetDHCPMacro "$TSUBNET" > $network_macro
        if [[ ! -s $network_macro ]]; then
            print -u2 "No Subnet macro defined for \"${TSUBNET}\""
            rm -f ${TMPDIR}/net*_tmp.$$ 2> /dev/null
            continue
        fi

        # check to see if it's a local interface
        if Intf=`GetDHCPSymbol Intf $network_macro`; then
	    ANYINTFSFOUND=true
            if [[ ! -s ${HOSTNAME_R}.${Intf} ]]; then
                IntfAddr=""
            else
                typeset H=`cat ${HOSTNAME_R}.${Intf}`
                IntfAddr=`getent hosts $H | awk '{ print $1 }'`
                if [[ -z "$IntfAddr" ]]; then
                    IntfAddr=""
                fi
            fi
        fi

        # get the include macro
        if ! netmacro=`GetDHCPSymbol Include $network_macro`; then

            if [ -z "${Intf}" ]; then
                print -u2 "missing Include macro in Subnet macro for \"${TSUBNET}\""
                rm -f ${TMPDIR}/net*_tmp.$$ 2> /dev/null
                continue
            fi
	    # for backward compatibility
            netmacro="${CORONA_NAME}-${Intf}"
        fi

        # setup the referenced macro file
        rm -f $refer_macro 2> /dev/null
        GetDHCPMacro "$netmacro" > $refer_macro
        if [[ ! -s $refer_macro ]]; then
            print -u2 "No \"${netmacro} macro defined for \"${TSUBNET}\""
            rm -f ${TMPDIR}/net*_tmp.$$ 2> /dev/null
            continue
        fi

        # establish the search order
        typeset PNTADM_PX="${TMPDIR}/pntadm_px.$$"
	typeset PNTADM_TMP="${TMPDIR}/pntadm_tmp.$$"
	pntadm -Px ${TSUBNET} > $PNTADM_TMP 2>/dev/null
	if [[ -s $PNTADM_TMP ]]; then
        	rm -f $PNTADM_PX 2>/dev/null
                nawk '
                    NR > 2 {
                        if ($6 == macro) {
                            print $3, $5;
                        }
                    }' macro=${netmacro} - < $PNTADM_TMP | \
                    sort -u -t . -k1,1n -k2,2n -k3,3n -k4,4n > $PNTADM_PX
	fi
	rm -f $PNTADM_TMP
        if [[ -s $PNTADM_PX ]]; then
            # network table defined, the macro referenced in the network
            # table has precedence.
            typeset order="$refer_macro $sunray_macro $network_macro"
        else
            # not defined in the network table, the network macro has
            # precedence.
            typeset order="$network_macro $refer_macro $sunray_macro"
        fi

        # retrieve broacast value
        if ! Broadcast=`getDHCPSymbolFromList Broadcst $order`; then
            Broadcast=""
        fi

        # retrieve netmask value
        if ! Netmask=`getDHCPSymbolFromList Subnet $order`; then
            Netmask=""
        fi

        # retrieve router value
        if ! Router=`getDHCPSymbolFromList Router $order`; then
            Router=""
        fi

        # retrieve firmware server value
        if ! FWSrvr=`getDHCPSymbolFromList FWSrvr $order`; then
            FWSrvr=""
        fi

        # retrieve MTU value
	if ! MTU=`getDHCPSymbolFromList MTU $order`; then
	    MTU=""
	fi

        # retrieve NewTver value
	if ! NewTVer=`getDHCPSymbolFromList NewTVer $order`; then
            NewTVer=""
        fi

        # retrieve auth server value
        if ! AuthSrvr=`getDHCPSymbolFromList AuthSrvr $order`; then
            AuthSrvr=""
        fi

        # retrieve altauth server list
        AltAuthSrvr=`getDHCPSymbolFromList AltAuth $order`

        if [[ -s $PNTADM_PX ]]; then
            today=`/opt/SUNWut/lib/uttime`
            IPassign=`nawk -v today=$today 'BEGIN { count = 0; total = 0 }
                    {
                    if ($2 < 0 || today < $2) {
                        count++
                    }
                    total++
                }
                END {
                        print count, total
                }' $PNTADM_PX`
            FirstIP=`head -1 $PNTADM_PX | awk '{ print $1 }'`
	    LastIP=`tail -1 $PNTADM_PX | awk '{ print $1 }'`
        else
            IPassign=""
        fi
        rm -f $PNTADM_PX
            typeset HostName=""
            typeset ReqParams=""
            typeset ErrStr="- missing the following required parameter(s):"
            typeset ErrType=""
            typeset ErrFound=false

	    # check for required parameters that are common for both private and lan
            [[ -z "${TSUBNET}" ]] && ReqParams=${ReqParams}" network" && ErrFound=true
            [[ -z "${Netmask}" ]] && ReqParams=${ReqParams}" netmask" && ErrFound=true

            if [[ -n "$Intf" ]]; then
                # private interconnect, need to get the hostname and ip
                if [[ -n "${IntfAddr}" ]]; then
                    HostName=`getent hosts ${IntfAddr} | cut -f2-`
                fi

                # check for the required parameters for private interconnect only
                [[ -z "${Intf}" ]] && ReqParams=${ReqParams}" interface" && ErrFound=true
                [[ -z "${HostName}" ]] && ReqParams=${ReqParams}" hostname" && ErrFound=true
                [[ -z "${IntfAddr}" ]] && ReqParams=${ReqParams}" ip" && ErrFound=true

                # set the ineterface ErrType for error message
                ErrType="interface ${Intf}"
            else
                # set the subnet ErrType for error message
                ErrType="subnet ${TSUBNET}"
            fi

            if ${ErrFound}; then
                print -u2 "Error: ${ErrType} ${ErrStr}\n\t${ReqParams}"
                exit 1
            fi

            if [[ -n "$Intf" ]]; then
                # private interconnect
                print "begin interface" >> $UTDHCPFILE
                print "interface=${Intf}" >> $UTDHCPFILE
                print "hostname=${HostName}" >> $UTDHCPFILE
                print "ip=${IntfAddr}" >> $UTDHCPFILE
            else
                # subnetwork
                print "begin subnet" >> $UTDHCPFILE
            fi
            print "network=${TSUBNET}" >> $UTDHCPFILE
            print "netmask=${Netmask}" >> $UTDHCPFILE

	if [[ -n $Broadcast ]]; then
		print "broadcast=${Broadcast}" >> $UTDHCPFILE
	fi
	if [[ -n $Router ]]; then
		print "routers=$Router" >> $UTDHCPFILE
	fi
	if [[ -n $FWSrvr ]]; then
		print "firmwaresrvr=$FWSrvr" >> $UTDHCPFILE
	fi
	if [[ -n $MTU ]]; then
		print "mtu=$MTU" >> $UTDHCPFILE
	fi
	if [[ -n $NewTVer ]]; then
		print "newtver=$NewTVer" >> $UTDHCPFILE
	fi
	if [[ -n $AuthSrvr ]]; then
		print "authsrvr=$AuthSrvr" >> $UTDHCPFILE
        fi
	if [[ -n $AltAuthSrvr ]]; then
		print "altauthlist=$AltAuthSrvr" >> $UTDHCPFILE
	fi
	if [[ -n $IPassign ]]; then
		print "range=$FirstIP $LastIP" >> $UTDHCPFILE
		print "assigned=${IPassign% *}" >> $UTDHCPFILE
	fi
	TranslateSunRayOptions $network_macro
	print "end" >> $UTDHCPFILE
	rm -f ${TMPDIR}/net*_tmp.$$ 2> /dev/null
    done
}

# CR 6217989 - dhtadm -B errors on Solaris x86
# workaround - issue the commands one at a time
# $1 - dhtadm or pntadm command
# $2 - batch dhtadm / pntadm commands
DoDhcpBatch() {
    typeset -i DHCPRC=0
    typeset BATCH=$2

    if [ `uname -p` = "sparc" ] ; then
	$1 -B $BATCH
	DHCPRC=$?	# remember result
    else
	while read cmd
	do
	    echo ${cmd} | sh
	    if [[ $? -ne 0 ]] ; then
		DHCPRC=1	# remember an error occurred
	    fi
	done < ${BATCH}
    fi

    return $DHCPRC

}
