#! /bin/sh
#
# @(#)setTRAPS	1.2	LPS_UNX_COM	02/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# setTRAPS
#
# A script to manage the set of SNMP traps for a printer.
#
# Parameters:
#    $1 - LPS class identifier, one of {PC, MC, PS, ENV}
#    $2 - Operating system type identifier (eg: SV2, BSD, etc)
#    $3 - Print client type identifier (eg: AIX, BSD, SV3, etc)
#    $4 - PrintServer printer model (eg: LPS17, LPS20)
#    $5 - Default value for the attribute
#    $6 - Path of the output file to receive the final attribute value
#
# Global variables:
#    ECHON
#    FUNCS
#    PAGER
#    TMPDIR
#    TR
#
#    PS_TRAPS
#
# Exit values:
#    0 - Success, output file contains newly set attribute value.
#    1 - Error or interruption occurred, error messages go to stderr,
#	 contents of the output file are undefined.
###

CLASS=$1
OSTYPE=$2
PCTYPE=$3
PSMODEL=$4
DEFVAL="$5"
OUTFILE=$6

TMPFILE=$TMPDIR/setct.$$

. $FUNCS   # Import the standard LPS shell functions

export ECHON FUNCS PAGER TMPDIR

# Simple catch-all fatal error function

Fatal ()
{
    echo          1>&2
    echo "    $*" 1>&2
    echo          1>&2

    pause	# Let the user see the error situation

    exit 1
}

# Ensure that PS_TRAPS points to an existing file;
# if it doesn't exist, then create a null temp file.

if [ -z "$PS_TRAPS" ]
then
    PS_TRAPS=$TMPDIR/setctpc.$$
fi

if [ ! -f $PS_TRAPS ]
then
    if touch $PS_TRAPS
    then
	:
    else
	Fatal "Unable to create a traps table temporary file!"
    fi
fi

# Immediately write the value of PS_TRAPS to the OUTFILE in
# case anything happens out of our control.  Later, when we exit
# with success, we'll write the value again in case it has changed.

echo "$PS_TRAPS" > $OUTFILE

QUESTION="Do you want to define one or more SNMP traps"

showhelp true "
${QUESTION}?

If the SNMP network management protocol is enabled within the printer,
then the printer can automatically send authentication error alert
messages to predefined hosts on the network.  These alert messages
are called \"trap\" messages.

In order for the printer to automatically send such traps, you must:

    1. Enable SNMP traps for the printer, and
    2. Define one or more trap specifications that describe which
       network host(s) are to receive the trap messages.

The collection of trap specifications is called the trap table.

A trap specification includes two components:

    1. A community name
    2. An internet host address

When an authentication error in the printer occurs, the printer
sends a trap message to each each host defined in the trap table.

You can define up to 16 trap specifications for a PrintServer printer.

Each trap message contains a predefined \"community\" name that you
specify along with the target host address.  This name is used by
the receiving host for identification purposes.

A community name can have from one to 64 characters, where each character
is from the \"NVT ASCII\" character set; these characters are the kind
generally used to display text information on computer display devices,
such as terminals and workstation monitors.

A trap specification also includes the internet address of the host
designated to receive the trap message.  This host address must be
specified in \"dotted octet\" format.  Examples of addresses in this
format include:

	192.9.200.67
	16.0.4.210
	138.95.0.22
	8.0.0.173

Note that all four octet fields of the internet address must be entered;
this means that you can not specify an abbreviated form of the address.
For example, the internet host address \"16.1234\" is not valid as a host
address specification when defining a community; instead, this address must
be entered as \"16.0.4.210\".

Note that the address \"0.0.0.0\" is NOT valid for a trap host address!
The internet host address for a trap specification must refer to a real
host on your network.

See your network administrator if you are unsure as to the proper format
for the desired host addresses.

The table of currently defined traps is displayed below, at which
which point you may either add or remove traps to the table, or
retain the table as it currently exists.

For more information about SNMP and traps, please refer to the
Management Guide.
"

while true
do
    # Display the current table to the user

    $PAGER << ENDOFINPUT

Currently defined table of SNMP traps for this printer:

`dispcfgtab "$PS_TRAPS" traps true`
ENDOFINPUT

    # Ask the user to select a table operation (or quit)

    if choose Quit "How would you like to modify this table" Add Remove Quit
    then
	# Either Add or Remove was selected, so find out which one
	# and dispatch accordingly

	if [ $RESPONSE = Add ]
	then
	    if addcommtraps traps $PS_TRAPS $TMPFILE
	    then
		# Append the table of new traps to the existing table

		if cat $TMPFILE >> $PS_TRAPS
		then
		    :
		else
		    Fatal "Failure to add new traps to the table!"
		fi
	    else
		Fatal "Failure to define new traps!"
	    fi
	else
	    if rmcommtraps traps $PS_TRAPS
	    then
		:
	    else
		Fatal "Failure to define new traps!"
	    fi
	fi
    else
	break	# Quit was entered, so we're all done
    fi

    clear
done

# Write the final value of PS_TRAPS to the OUTFILE

echo "$PS_TRAPS" > $OUTFILE

exit 0
