
  #
  # xtutil_dobootp - Set a bootptab entry as specified by
  #	keyword parameters argXXX
  #

set -u				# Find undefined variables

#--------------------------------------------------------------------
#	Also remove current entry by Link-Level-Address
#--------------------------------------------------------------------
removeByEA()
{
    xtutil_rmBootpEntry "ha=${argEA:-000000000000}" $argTable >$argResult
    exitIfNonZero $? 1 "xtutil_rmBootpEntry `cat $argResult`"

    changes $argTable $argResult >$TMP/changes
    if [ $? -eq 0 ]
    then
	$NLECHO "bootpRemoved 'ha=${argEA:-000000000000}' $theTable"
	[ "$SCRIPTDEBUG" = "true" ] && cat $TMP/changes
	rm $TMP/changes
	return 1
    fi
    return 0
}

#--------------------------------------------------------------------
#	Remove current entry by name
#--------------------------------------------------------------------
removeByName()
{
    xtutil_rmBootpEntry "^$argName:" $argTable >$argResult
    exitIfNonZero $? 1 "xtutil_rmBootpEntry `cat $argResult`"

    changes $argTable $argResult >$TMP/changes
    if [ $? -eq 0 ]
    then
	$NLECHO "bootpRemoved $argName $theTable"
	[ "$SCRIPTDEBUG" = "true" ] && cat $TMP/changes
	rm $TMP/changes
	return 1
    fi
    return 0
}

addEntry()
{
    cp $argTable $TMP/bbbak		# Save for later compare

    /bin/echo ""				>> $argTable
    /bin/echo "$argName:\\"			>> $argTable

    [ ! -z "${UNDERSTANDSBA:-}" ] && \
	/bin/echo "	ba:\\"			>> $argTable

    [ ! -z "${argHN:-}" ] && \
	/bin/echo "	hn:\\"			>> $argTable

    /bin/echo "	ht=${argHT:-ether}:\\"		>> $argTable

    /bin/echo "	ha=${argEA:-000000000000}:\\" 	>> $argTable

    /bin/echo "	ip=${argIP}:\\"			>> $argTable

    [ ! -z "${argT145:-}" ] && \
	/bin/echo "	T145=$argT145:\\"	>> $argTable

    [ ! -z "${argGW:-}" ] && \
	/bin/echo "	gw=$argGW:\\" 		>> $argTable

    [ ! -z "${argSM:-}" ] && \
	/bin/echo "	sm=${argSM}:\\" 	>> $argTable

    [ ! -z "${argBF:-}" ] && \
	/bin/echo "	bf=$argBF:\\"		>> $argTable

    DT144TAG=${argT144:-}
    [ -z "$DT144TAG" ] && \
	DT144TAG=`/bin/echo ${argName:-} | awk -F\. '{print $1}'`.cfg

    /bin/echo "	T144=\"$DT144TAG\":\\" 		>> $argTable

    [ ! -z "${DOMA:-}" ] && \
	/bin/echo "	ds=$DOMA:\\" 		>> $argTable

#
#	Leave this one last so \\ doesn't cause trouble.
#
    /bin/echo "	vm=rfc1048" 			>> $argTable

    changes $TMP/bbbak $argTable >$TMP/changes
    if [ $? -eq 0 ]
    then
	cp $argTable $theTable 	; exitIfNonZero $? 1 `$NLECHO "modBootp $theTable"`

	$NLECHO	"bootpAdded $argName $theTable"

	[ "$SCRIPTDEBUG" = "true" ] && cat $TMP/changes
	rm $TMP/changes
    fi
}


#----------------------------------------------------------------------------
#	Mainline starts here
#----------------------------------------------------------------------------
#/bin/echo "xtutil_dobootp"

#[ "$SCRIPTDEBUG" = "true" ] && \
	env|grep arg

# The assumption here is that all the required data are passed as parameters
# argXXX...
# All entries with the same name are first removed from bootptab
# If argEA is non-zero, all entries with matching ha=... are removed from bootptab
#

if [ "$1" = "ADD" ]
then
    whereToAdd=$TMP/boot2
    export theTable=$BOOTPTAB

    argTable=$BOOTPTAB		argResult=$TMP/boot1	removeByEA
    argTable=$TMP/boot1		argResult=$TMP/boot2	removeByName

    argTable=$whereToAdd addEntry

    rm -f $TMP/boot1 $TMP/boot2 $TMP/cold1 $TMP/cold2 $TMP/bbbak
else
    export theTable=$BOOTPTAB

    argTable=$BOOTPTAB		argResult=$TMP/boot1	removeByName
    mv $TMP/boot1 $BOOTPTAB
    rm -f $TMP/boot1 $TMP/bbbak
fi    
exit 0



