#! /bin/sh
#
# @(#)rmcommtraps	1.2	LPS_UNX_COM	2/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# rmcommtraps
#
# This script is used to remove 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
#
# Global variables:
#    FUNCS
#    ECHON
#    PAGER
#    TMPDIR
#    TR
#
# Exit values:
#    0 - Success, the new table was written to the table file
#   >0 - Failure of some kind; should be rather rare, such as a failure
#        to write a file to the $TMPDIR directory, etc.
###

SCRIPTNAME=`basename $0`

TABLETYPE=$1
TABLEFILE=$2

TMPFILE=$TMPDIR/comtrps.$$
NEWTABLE=$TMPDIR/newtab.$$

. $FUNCS	# Import the standard LPS shell functions

export ECHON FUNCS PAGER TMPDIR TR

# Simple catch-all fatal error function

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

    pause	# Let the user see the error situation

    exit 1
}

# Parameter validation

if   [ -z "$TABLETYPE" ]
then
    Fatal "No table type found!!!"
elif [ -z "$TABLEFILE" ]
then
    Fatal "No $TABLETYPE table path found!!!"
fi

# Check to see if the table file exists; if it doesn't, then be sure
# to create an empty file so nothing else breaks down the line...

if [ -f $TABLEFILE ]
then
    entries=`cat $TABLEFILE | wc -l`
    entries=`echo $entries`   # To remove stupid leading spaces from wc
else
    if touch $TABLEFILE
    then
	entries=0
    else
	Fatal "Unable to create the $TABLETYPE file!"
    fi
fi

# If there are no entries in the file, then we certainly don't
# have anything to do here!

if [ $entries -eq 0 ]
then
    echo
    echo "   The SNMP $TABLETYPE table is currently empty!"
    echo

    pause

    exit 0
fi

# Copy the current table to the new (working) table

if cp $TABLEFILE $NEWTABLE
then
    :
else
    Fatal "Unable to create working copy of $TABLETYPE file ($TABLEFILE)!!"
fi

# Repeatedly loop:
#    - Display the current working table
#    - Solicit the number of the table entry to remove (quit if null)
#    - Remove that line from the working table file
#    - If no more entries exist, then quit

while true
do
    # Display the current table contents

##    clear

    if cat << ENDOFINPUT > $TMPFILE

Currently defined table of SNMP $TABLETYPE for this printer:

`dispcfgtab $NEWTABLE $TABLETYPE true`
ENDOFINPUT
    then
	$PAGER $TMPFILE
    else
	Fatal "Unable to display the current $TABLETYPE table!!!"
    fi

    # Repeated request a valid entry number until we get one

    while true
    do
	echo
	$ECHON "Enter the number of the entry to remove [RETURN to quit]: "

	read response
	if [ "$response" ]
	then
	    if checknumrange 1 $entries 0 "$response"
	    then
		break   # Response is valid, value is in RESPONSE
	    else
		echo
		echo "   Invalid entry number!"
		echo
		echo "Please enter a number between 1 and $entries"
	    fi
	else
	    break 2	# null response, time to quit
	fi
    done

    # Zap that line in the working table file

    if sed "${RESPONSE}d" $NEWTABLE > $TMPFILE
    then
	if cp $TMPFILE $NEWTABLE
	then
	    :
	else
	    Fatal "Failure to recreate working table file!!!"
	fi
    else
	Fatal "Failure to remove entry from working table file!!!"
    fi

    # Decrement the number of table entries and if we end up with an
    # empty table, tell the user, then terminate the loop in success.

    entries=`expr $entries - 1`

    if [ $entries -eq 0 ]
    then
	echo
	echo "   The SNMP $TABLETYPE table is now empty."
	echo

	pause   # Let the user ponder this fact...

	break
    fi

done

# All done, so copy the working file to the table file and get out

if cp $NEWTABLE $TABLEFILE
then
    :
else
    Fatal "Unable to copy working file ($NEWTABLE) to table file ($TABLFILE)!!!"
fi

exit 0
