#! /bin/sh
#
# @(#)setCOMMUNITIES	1.2	LPS_UNX_COM	02/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# setCOMMUNITIES
#
# A script to manage the set of SNMP communities 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_COMMUNITIES
#
# 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_COMMUNITIES points to an existing file;
# if it doesn't exist, then create a null temp file.

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

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

# Immediately write the value of PS_COMMUNITIES 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_COMMUNITIES" > $OUTFILE

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

showhelp true "
${QUESTION}?

If the SNMP network management protocol is enabled within the printer,
then the printer can respond to requests from the network to obtain or
set various operational parameters within the printer.

To access these parameters, the requesting network management application
must specify a \"community\" name in the request.  This name is used
as a sort of password into the printer; the community name is defined
so as to provide either read-only or read/write access by the application
specifying the name in the request.

A community definition also includes the specification of an internet host
address.  This address is used as a sort of second-level authorization
mechanism.  When a network management application sends an SNMP request to
the printer, the community name sent in the request must be one of the
community names defined within the printer; additionally, the host address
specified as part of the community definition must also match the host
address of the requesting application, otherwise the printer will deny the
request.

The host address may alternatively be set to the special address value of
\"0.0.0.0\" which allows any host the network permission to use the
community name.

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.

You can define up to 16 community names for a PrintServer printer.

The associated internet 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\".

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

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

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

while true
do
    # Display the current table to the user

    $PAGER << ENDOFINPUT

Currently defined table of SNMP communities for this printer:

`dispcfgtab "$PS_COMMUNITIES" communities 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 communities $PS_COMMUNITIES $TMPFILE
	    then
		# Append the table of new communities to the existing table

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

    clear
done

# Write the final value of PS_COMMUNITIES to the OUTFILE

echo "$PS_COMMUNITIES" > $OUTFILE

exit 0
