#! /bin/sh
#
# @(#)dobootptab	1.8	LPS_UNX_COM	2/19/95
#
# Copyright 1995   Digital Equipment Corporation, Maynard, MA
#
# dobootptab
#
# Do what is necessary for dealing with the /etc/bootptab file
# using the specified parameters and many, many shell variables.
#
# Parameters:
#    $1 - Mode of operation: "add", "update" or "remove"
#    $2 - Path of scratch output file to use
#
# Global variables:
#    LOCALHOST
#    PAGER
#    TR
#
# In addition, the following variables are expected to be set
# and available for use:
#
#    BOOTABENTRY
#
#    ENV_BOOTOPTIONS
#    ENV_LPSBOOT
#    ENV_LPSDOC
#    ENV_MAILADMADDRS
#    ENV_MAILOPTS
#    ENV_MAILPROG
#
#    PS_BOOTFILE
#    PS_DESCRIPTION
#    PS_DNETADDR
#    PS_ENETADDR
#    PS_HOSTNAME
#    PS_OBJNAME
#
# Exit values:
#    0 - Success
#    1 - Error or interruption occurred, error messages go to stderr
###

MODE=$1
OUTFILE=$2

SCRIPTNAME=`basename $0`

BOOTPTAB="/etc/bootptab"

boot=$ENV_LPSDOC/${PS_OBJNAME}.bpt

case "$MODE" in
       add ) operated="Created"
	     subjprefix="New"
	     intro="
The definition of the PrintServer printer \"$PS_OBJNAME\" on
host \"$LOCALHOST\" has been modified such that either the
printer's Ethernet address and/or DECnet address has changed."
	     ;;

    update ) operated="Updated"
	     subjprefix="Updated"
	     intro="
A new PrintServer printer definition called \"$PS_OBJNAME\"
has been created on host \"$LOCALHOST\"."
	     ;;

    remove ) operated="Removed"
	     subjprefix="Obsolete"
	     intro="
The definition of the PrintServer printer \"$PS_OBJNAME\"
has been removed from host \"$LOCALHOST\"."
	     ;;

         * ) echo
	     echo "${SCRIPTNAME}: Invalid operation: \"$MODE\""
	     exit 1
	     ;;
esac

helptext="
Additional information on booting PrintServer printers and editing
the $BOOTPTAB file can be found in the \"PrintServer Software
Management Guide\" for your system type.
"

if [ $MODE != remove ] # <<<------ For add or update operations only
then
    echo
    echo "    [Constructing the $BOOTPTAB entry]"

    enet="`echo $PS_ENETADDR | $TR -d '-'`"
    inet="`testhost -a $PS_HOSTNAME`"

    #
    # Construct the second line of the bootptab entry that describes
    # the location of the bootfile.  This is VERY PLATFORM DEPENDENT,
    # hence the use of the $KITCLASS variable.
    #
##    entry="td=${ENV_LPSBOOT}:  bf=${PS_BOOTFILE}:"

    entry=`eval echo $BOOTABENTRY`

    #
    # If the DECnet address is present, then we add the T128 field
    # to the entry's second line, and append another entire entry
    # for the DECnet physical address.
    #
    if [ "$PS_DNETADDR" ]
    then
	dnpa=`dn2pa $PS_DNETADDR`

	entry="$entry  T128=${dnpa}:
${PS_HOSTNAME}:  ht=ethernet:  ha=${dnpa}:  ip=${inet}:"
    fi

    #
    # Write the complete bootptab entry to a known file for later use
    # by both this script and the user.
    #
    cat > $boot << EOF
#---
# PrintServer printer "$PS_OBJNAME"  [${PS_DESCRIPTION}]
# (${operated}: `date`)
#
${PS_HOSTNAME}:  ht=ethernet:  ha=${enet}:  ip=${inet}: \\
	sm=${PS_SUBNETMASK}:  gw=${PS_GATEWAY}: \\
	$entry
#---
EOF

    chmod a+r $boot

    cat > $OUTFILE << EOF
PrintServer Software notification from host "$LOCALHOST":
$intro

If host ("$LOCALHOST") is expected to boot this printer, then you
must edit the $BOOTPTAB file to include all the necessary booting
information for the printer.

The entry in $BOOTPTAB for this printer should appear as follows:

`cat $boot`

Note carefully that the trailing "\\" character MUST be the last
character on the first line that starts with "$PS_HOSTNAME" above.

If an entry for this printer already exists in $BOOTPTAB, be sure
to either replace the existing entry with the one above, or edit the
existing entry to contain exactly the information shown above.

A complete copy of the $BOOTPTAB entry for this printer can be found
in the following file:

    $boot

The contents of this file can be directly included into the $BOOTPTAB
file using your favorite editor.
$helptext
EOF

else	# --------------------------- For remove operation only

    if [ -f $boot ]   # Does the old bootptab entry file exist?
    then
	entryhelp="
The entry in $BOOTPTAB for this printer should appear similar to
the following:

`cat $boot`

Note that the actual $BOOTPTAB entry may differ somewhat from
that shown above.
"
    else
	entryhelp=""
    fi

    cat > $OUTFILE << EOF
PrintServer Software notification from host "$LOCALHOST":
$intro

If an entry for this printer has been created in the $BOOTPTAB file,
you may now remove it using your favorite editor.
$entryhelp
Additional information on booting PrintServer printers and editing
the $BOOTPTAB file can be found in the "PrintServer Software
Management Guide" for your system type.
EOF

fi

# If this system is configured such that lpsbootd is NOT performing
# the BOOTP service for PrintServers, then display the instructions
# just created and mail a copy to the assigned email addressee.
# address.

if [ `expr "$ENV_BOOTOPTIONS" : bootp` -eq 0 ]
then
    echo

    $PAGER $OUTFILE

    echo
    echo "    [Sending email message describing booting information]"

    subj="$subjprefix $BOOTPTAB entry for PrintServer printer \"$PS_OBJNAME\""
    cmd="$ENV_MAILPROG $ENV_MAILOPTS"

    if $cmd -s "${subj}" $ENV_MAILADMADDRS < $OUTFILE
    then
	:
    else
	echo
	echo "    [ Sending the electronic mail message failed ??? ]"
	echo
	exit 1
    fi
fi

exit 0
