#! /bin/sh
#
# @(#)addcommtraps	1.2	LPS_UNX_COM	2/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# addcommtraps
#
# This script is used to add SNMP communities or traps table entries
# defined for a printer.
#
# Parameters:
#    $1 - Type of table to modify ("communities" or "traps")
#    $2 - Path of the file containing the current table contents
#    $3 - Path of an output file that will contain the new table entries
#
# Global variables:
#    FUNCS
#    ECHON
#    PAGER
#    TMPDIR
#    TR
#
# Exit values:
#    0 - Success, the new table entries were written to the output file
#   >0 - Failure of some kind; should be rather rare, such as a failure
#        to write a file to the $TMPDIR directory, etc.
###

TABLETYPE=$1
TABLEFILE=$2
TMPFILE=$3

MAXENTRIES=16		# Max number of entries allowed in the table

. $FUNCS		# Import the standard LPS shell functions

export ECHON FUNCS PAGER TMPDIR TR

# Ensure the output file exists and is empty

if touch $TMPFILE && cat /dev/null > $TMPFILE
then
    :
else      # Error???
    echo
    pause
    exit 1
fi

# Common function to declare a "table full" condition
# and to exit with success in that case.

TableFull ()
{
    echo
    echo "    $*"
    echo
    echo "    There is a maximum of $MAXENTRIES table entries."
    echo
    echo "    You must first remove a table entry before you"
    echo "    can add another entry."
    echo

    pause

    exit 0
}

# Determine the number of entries already in the table.
# If no more entries are available, display a message,
# then exit with success

entries=`cat $TABLEFILE | wc -l`
entries=`echo $entries`	   # To remove stupid leading spaces from wc

if [ $entries -ge $MAXENTRIES ]
then
    TableFull "The SNMP $TABLETYPE table is currently full."
fi

remaining=`expr $MAXENTRIES - $entries`

# Create a quick variable to test for communities vs. traps,
# and assign the IP address prompt based on that fact.

if [ $TABLETYPE = communities ]
then
    DOINGCOMM=true
    ADDRPROMPT="Internet host address [0.0.0.0]: "
else
    DOINGCOMM=false
    ADDRPROMPT="Internet host address: "
fi

echo
echo "The $TABLETYPE table currently has $entries entries, so you may define"
echo "up to $remaining additional ${TABLETYPE}.  Once all new $TABLETYPE have"
echo "been entered, press RETURN when prompted for a community name."
echo

while true	     # Outer loop, once per table entry definition
do
    while true	     # Loop until valid community name, or null response
    do

	$ECHON "Community name [RETURN to quit]: "
	read name

	if [ -z "$name" ]
	then
	    exit 0	# Quit when a null name is encountered
	fi

	# Ensure name is not too long, nor contains invalid characters

	if vfcommname "$name"
	then
	    break
	fi
    done


    while true
    do
	$ECHON "$ADDRPROMPT"
	read ipaddr

	if [ "$ipaddr" ]   # Non-null response?
	then
	    # Ensure a valid internet address was entered

	    if vfipaddr "$ipaddr"
	    then
		break
	    fi
	else
	    if $DOINGCOMM
	    then
		ipaddr="0.0.0.0"
		break
	    else
		echo
		echo "    You must specify an internet host address!"
		echo
		continue   # Go back to the top of the address prompt loop
	    fi
	fi
    done


    if $DOINGCOMM
    then
	    choose "read" "Access mode" read write
	    access=$RESPONSE
    else
	access=""
    fi

    # Append this new entry to the TMPFILE, then decrement the
    # count of remaining entries, display the number of entries
    # that may be added, then start all over again.

    echo "$name $ipaddr $access" >> $TMPFILE

    remaining=`expr $remaining - 1`

    case $remaining in
	0 ) TableFull "The SNMP $TABLETYPE table is now full." ;;
	1 ) msg="one last entry." ;;
	* ) msg="up to $remaining more entries." ;;
    esac

    echo
    echo "You may now enter $msg"
    echo
done

exit 0
