#!/sbin/sh
#@(#)HP Netstation Admin Scripts
#
# getxtname	Get hostname of netstation
#
#	1) Get a name for a netstation and return it in $TMP/XTNAME
#	   with a 0 exit code. 
#	2) Exit code 1 is "user didn't provide name and wants to quit".
#

#
#       If we were called from printer addition scripts
#       Then provide special help.

unset LANG

echo $1 | grep printer > /dev/null
if [ $? -eq 0 ]
then
	PRINTER="true"
else
	PRINTER="false"
fi


#	Set max length of a host name
#
HOSTLEN=64


while [ 0 ] 
do
	echo ""
  	$ECHO "Enter name of netstation to $1 [q|?] : $NL"
	read XTNAME 
	case $XTNAME in
	\?)
	 	if [ $PRINTER = "true" ]
	   	then
	          cat <<EOF

The name of your netstation is available on the "Network" config screen.
To display it:

	1. Press and hold the <F12> key on the keyboard.

	2. Click on "Network".

	3. The netstation name should appear in the "Terminal Name" field.

	4. If the "Terminal Name" field is empty, use the number
	   from the "IP Address" field instead.

	5. Click on "Exit".

EOF
		else
		cat <<EOF

You must assign your netstation a name in order to continue.

The netstation name can be from 1 to $HOSTLEN characters long.

It should contain only alphanumeric characters and should start
with an alphabetic character. (.eg a123)

Do not include the domain portion of the hostname (if any).

EOF
		fi
		continue
		;;
	q)
		exit 1
		;;
	esac

        #
	# If called from Printer scripts do special processing
	# where terminal name is fetched from ip address
	#
	echo $XTNAME | egrep '^[0-9].*[.]' > /dev/null 2>&1
	if [ $? -eq 0 -a $PRINTER = "true" ]
	then
	  IPADDR=$XTNAME
	  isipaddrinh $IPADDR
	  RET=$?
	  if [ $RET -eq 6 -o $RET -eq 9 -o $RET -eq 0 ]
	  then
	     XTNAME=`cat $TMP/HOSTNAME`
	     rm $TMP/HOSTNAME
	  else
            case $RET in
	      4)
		  DB=/etc/hosts
		  ;;
	      5|7)
		  DB=BIND
		  ;;
	      10)
	          DB=NIS
		  ;;
	       *)
		  ;;
	     esac

             echo ""
	     echo "The IP address of the netstation is not in the $DB database."
	     echo "You should select the QUIT option and add the netstation"
	     echo "by selecting \"Add netstation\" from the main menu."
	     echo "When the netstation has been added you may re-run this selection."
	     echo ""
	     continue
	   fi
        fi

	echo $XTNAME | egrep '^[a-zA-Z](([a-zA-Z0-9])|([-]))*$' > /dev/null
	if [ $? -ne 0 ]
	then
		cat <<EOF
Invalid name.
Netstation names should contain only alphanumeric characters
and should start with an alphabetic character.

Please re-try!
EOF
	else
		NC=`echo $XTNAME | wc -c | awk '{print $1}'`

	 	if [ $NC -gt 1 -a $NC -le $HOSTLEN ]
		then
			break
		else
	 	    echo "Invalid name (must contain 1-$HOSTLEN chars), - please re-try!"
		fi
	fi
done

echo $XTNAME > $TMP/NAME
echo $XTNAME$DOMN > $TMP/XTNAME
exit 0
