#! /bin/sh
#
# @(#)getaddrlist	1.5	LPS_UNX_COM	02/19/95
#
# Copyright 1993   Digital Equipment Corporation, Maynard, MA
#
# getaddrlist
#
# Common support script used to generate an address list for internet
# hosts/networks or DECnet nodes/areas.  Designed exclusively for use
# with the "set{ALLOW|DENY}{IP|DN}" scripts.
#
# Parameters:
#    $1 - Mode keyword, either "deny" or "allow".
#    $2 - Network domain keyword, either "internet" or "decnet".
#    $3 - Quoted string containing the current space-separated address list.
#    $4 - Path of an output file to receive the final address list.
#
# Global variables:
#    ECHON
#    FUNCS
#    PAGER
#    TMPDIR
#    TR
#
# Exit values:
#    0 - Success, list was generated and written to the output file. 
#    1 - Failure, output file contents are undefined.
###

MODE=$1
DOMAIN=$2
ADDRLIST="$3"
OUTFILE=$4

TMPFILE=${TMPDIR:=/tmp}/addrlist.$$

: ${ECHON:="/bin/echo -n"}
: ${FUNCS:="lpsshfuncs"}
: ${PAGER:=more}
: ${TMPDIR:=/tmp}
: ${TR:=tr}

. $FUNCS

export ECHNO PAGER TMPDIR TR

# Set some text-oriented variables based on the action type

if [ $MODE = deny ]
then
    RESTRICT=restrict
    RESTRICTED=DENIED
    RESTRICTION=restriction
else
    RESTRICT=allow
    RESTRICTED=ALLOWED
    RESTRICTION=allowance
fi

# Set some text-oriented variables based on the network domain type

if [ $DOMAIN = internet ]
then
    ADDR=address
    ADDRS=addresses
    HOST=host
    NETWORK=network
else  # DECnet
    ADDR=number
    ADDRS=numbers
    HOST=node
    NETWORK=area
    DOMAIN=DECnet   # Redefine for text messages
fi

ADDRESS="$DOMAIN $HOST/$NETWORK $ADDR"
ADDRESSES="$DOMAIN $HOST/$NETWORK $ADDRS"

# Top of a loop to display/edit/add addresses to the list.  The loop
# is exited if the final list is empty, or if the user is satisfied
# with the constructed list.

confirm=false  # Confirmation is requested after the first pass of the loop

LISTTITLE="The following $ADDRESSES are currently $RESTRICTED
access to the printer:"

while true
do
    # Display the contents of the current list.  If non-empty, then ask the
    # user if modifications should be performed, then do it.

    ACTION=retain	# Controls looping and confirmation

    echo

    if [ -z "$ADDRLIST" ]
    then
	echo "$LISTTITLE"
	echo
	echo "[The current list is empty]"
    else
	cat << ENDOFINPUT > $TMPFILE
$LISTTITLE
ENDOFINPUT
	for addr in $ADDRLIST
	do
	    echo "    $addr" >> $TMPFILE
	done

	$PAGER $TMPFILE # To ensure the list doesn't scroll of the screen

	# If we've been here before, then ask if the currently constructed
	# list is complete.  If so, then we're done.

	if $confirm
	then
	    if yesno y "Are you satisfied with this list"
	    then
		break	# --> Break out of while() loop due to satisfaction
	    fi
	fi

	menulist="Retain Destroy Edit"
	question="What do you want to do with this $RESTRICTION list"

	choose Retain "$question" $menulist

	case $RESPONSE in
	     Retain ) ;;		# Current list remains intact

	    Destroy ) ADDRLIST=""	# Completely zap the current list
		      ACTION=destroy
		      ;;
	       Edit ) newlist=""	# Construct new list of retained addrs
		      ACTION=retain	# Result if no removals
		      for addr in $ADDRLIST
		      do
			  question="Retain $ADDRESS $addr"
			  if yesno y "$question"
			  then
			      newlist="$newlist $addr"
			  else
			      ACTION=edit   # At least one addr was removed
			  fi
		      done
		      ADDRLIST="$newlist"
		      ;;
	esac
    fi

    # Now ask if additional addresses should be added.  If the user does not
    # want to add addresses AND the list is currently empty, then consider
    # our job complete and write out an empty output file.

    if yesno x "Do you wish to $RESTRICT additional $ADDRESSES"
    then
	echo
	echo "When prompted, enter the $ADDR of the $DOMAIN $HOST or"
	echo "$NETWORK to be $RESTRICTED access to the printer.  Press the"
	echo "RETURN key at the prompt to end list input."
	echo
	while true
	do
	    $ECHON "Enter $DOMAIN $HOST or $NETWORK $ADDR to $RESTRICT: "
	    read addr
	    if [ -z "$addr" ]
	    then
		break
	    else
		if [ -z "$ADDRLIST" ]
		then
		    ADDRLIST="$addr"
		else
		    ADDRLIST="${ADDRLIST} $addr"
		fi
		ACTION=new
	    fi
	done
    else
	if [ $ACTION = retain ]   # No list change this time around?
	then
	    break   # ---> Break out of while() loop due to static list
	fi
    fi

    confirm=true    # Back to top of loop, request confirmation after display

done	# Bottom of overall while() loop

echo "$ADDRLIST" > $OUTFILE

exit 0
