#! /bin/sh
#
# @(#)setGATEWAY	1.7	LPS_UNX_COM	4/17/95
#
# Copyright 1995   Digital Equipment Corporation, Maynard, MA
#
# setGATEWAY
#
# A script that produces a value for the printer configuration file
# keyword "gateway".
#
# 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    (for the checknumrange() function)
#
# 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

. $FUNCS   # Import the standard LPS shell functions

showhelp true "
What should the printer use for an internet gateway address, if needed?

The gateway specifies the default internet route when the printer is used
in a multiple TCP/IP network environment.  If the network does not have
gateways or the gateway is automatic, you can simply press RETURN to leave
this value blank.

You may enter either the host name of the gateway, or the internet address
of the gateway.  Either way, the name or address must be registered in this
system's hosts table.

The format of an internet address is:

            nnn.nnn.nnn.nnn

Where each \"nnn\" is a number between 0 and 255.

Your network administrator can help you determine whether you need
to specify a gateway address, and what the address should be.
"

if [ "$DEFVAL" ]
then
    # At this point we expect an internet host address as a default
    # value (unless the user manually edited the LPSODB and inserted
    # a hostname).  Validate the expected host address, and if this
    # fails, then try validating it as a hostname.  If the validation
    # for the hostname succeeds, then immediately convert it to a
    # host address; that is, the user will never see a hostname as a
    # default value.
    #
    # If both tests fail, then clear the default value, forcing the
    # user to enter a new value.

    if `testhost -v "$DEFVAL" > /dev/null 2>&1`
    then
	SUFFIX=" [$DEFVAL]: "
    else
	if DEFVAL=`testhost -a "$DEFVAL" 2> /dev/null`
	then
	    SUFFIX=" [$DEFVAL]: "
	else
	    DEFVAL=""	# zap this bad gateway address, no default
	fi
    fi
else
    SUFFIX=": "
fi

QUESTION="Internet gateway address$SUFFIX"

# The structure of this loop is such that looping will continue
# until either a valid hostname, valid host address or null
# response is entered.

while true
do
    echo
    $ECHON "$QUESTION"
    read RESPONSE

    if [ "$RESPONSE" ]
    then
	# The user has entered *something*, so first check (silently)
	# to see if the string was a valid host address; if so, then
	# we're done.  Otherwise, check to see if it is a valid hostname,
	# and if it is, then translate it to a host address and quit.
	# Anything else results in a chiding of the user and a repeat
	# of the question.

	if `testhost -v "$RESPONSE" > /dev/null 2>&1`
	then
	    break	# Valid host address (at least the format is!)
	else
	    if RESPONSE=`testhost -a "$RESPONSE"`
	    then
		break
	    else
		echo
		echo "Please try again."
	    fi
	fi

    else	# User simply hit the RETURN key (ie, a null response string)

	if [ "$DEFVAL" ]	# Was there a default value?
	then
	    if yesno y "Retain current gateway address"
	    then
		RESPONSE="$DEFVAL"
	    fi
	fi
	break
    fi
done

echo "$RESPONSE" > $OUTFILE

exit 0
