#!/sbin/sh
#@(#)HP X Station Admin Scripts
#
# ckprinter	check the operation of an X station printer
#

set -a

getxtname "test printer/plotter on"

if [ $? -ne 0 ]
then
  exit 100
fi

XTNAME=`cat $TMP/XTNAME`
rm  $TMP/XTNAME

PERIPH=`cat $TMP/NAME`
rm $TMP/NAME

#
#       Is specified name in DNS
#       or NIS
#       or /etc/hosts
#

isxtnameinh $XTNAME
if [ $? -eq 6 -o $? -eq 5 -o $? -eq 0 ]
then
  :
else
  echo ""
  echo "The X station name you entered is not in the hosts database."
  echo "Select \"Add an X station\" from the main menu,"
  echo "and then re-run this selection."
  echo ""
  exit 1
fi

whichport
if [ $? != 0 ]
then
	exit 100
fi

PERIPHPORT=`cat $TMP/PERIPHPORT`
rm  $TMP/PERIPHPORT

cat <<EOF

This test will:

	1. Do a network ping to make sure the X station is connected.

	2. Prompt you for the type of test file to send and then send it.
	   This test bypasses the print spooler.

EOF

$ECHO "Check operation of printer/plotter connected to $PERIPH? [y|n] (y) : $NL"
read SEL
case $SEL in
    y|Y|yes|YES|Yes|"")
	;;
    *)
	exit 100
	;;
esac

echo ""
echo "Testing network connectivity to $PERIPH .... "
echo ""
$PINGSTART $PERIPH $PINGEND > $TMP/PCNCT 2>&1
cat $TMP/PCNCT |grep 100% > /dev/null 2>&1
RETCODE=$?
cat $TMP/PCNCT |grep 'unknown host' > /dev/null 2>&1
RETCODE2=$?
rm $TMP/PCNCT

#  RETCODE of 0 indicates 100% packet failure and RETCODE2 of 0 indicates
#  unknown host.

if [ $RETCODE -eq 0 -o $RETCODE2 -eq 0 ]
then
  echo "$PERIPH failed the network connectivity test"
  exit 1
else
  echo "$PERIPH passed the network connectivity test"
fi
echo ""

FILE_HPGL=$HPNP/testfiles/hpgl
FILE_PS=$HPNP/testfiles/ps
FILE_TEXT=$HPNP/testfiles/text
HPNPF=$HPNP/hpnpf

DASHN=""

while [ 0 ]
do
    cat <<EOF

The following types of test files can be sent to the peripheral:

	1) Text file		(for printers)
	2) PostScript file	(for printers)
	3) HP-GL  		(for plotters)

		?) help		p) previous menu

EOF
    $ECHO "Which type of file should be sent? (default=1) $NL"
    read RESP
    case "$RESP" in
   1|"")
	FILE=$FILE_TEXT 
	DASHN="-n"
	break
	;;
      2)
	FILE=$FILE_PS 
	break
	;;
      3)
	FILE=$FILE_HPGL
	break
	;;
  p*|P*)
	exit 100
	;;
     \?)
	cat <<EOF

Choose:

	1) Text	
	   for most printers

	2) PostScript
	   for PostScript capable printers only

	3) HP-GL
	   for plotters
	
If you send the wrong type of file, your printer or plotter may produce 
garbled output or no output at all.

EOF
	;;
      *)
	echo ""
	echo "Invalid response"
	;;
    esac
done

if [ ! -f "$FILE" ]
then
  echo ""
  echo "Unable to run test because $FILE does not exist."
  exit 1
fi

echo ""
echo "Sending a test file to $PERIPH ..."
echo ""

#---------------------------------------------------------#
# The 10.0 and some 9.0 versions are newer and don't understand "-v"
#---------------------------------------------------------#
#
if [ $OS = "hp10" ]
then
  VERBOSE=""
else
  VERBOSE="-v"
fi

if [ ! -f "$HPNP/version" ]
then
  VERBOSE=""
else
  VERBOSE="-v"
fi

$HPNPF $DASHN $VERBOSE -R -x $PERIPH -p $PERIPHPORT $FILE

if [ $? = 0 ]
  then
    echo ""
    echo "The file was successfully sent to $PERIPH."
else
    cat <<EOF

An error occurred sending the file to $PERIPH.

	1) Verify that the printer/plotter is online.

	2) Run the print test on the X station "Terminal" config screen.

EOF
fi

exit 0

