#!/sbin/sh
#@(#)HP Netstation Admin Scripts
#
# ckprinter	check the operation of a netstation 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 netstation name you entered is not in the hosts database."
  echo "Select \"Add a netstation\" 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 netstation is connected.

	3. Examine if netstation has loaded the proper extensions

	3. 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 ""

#
# Check if the right extension are loaded for Altima
#
$REMSH $PERIPH get installed | egrep -i 'kernel.*version' > /dev/null
if [ $? -eq 0 ]
then
  # It's an Altima
  case $PERIPHPORT in
    9101)      EXTENSION=parallelExt; DRIVER="Parallel Port harware driver" ;;
    9100|9102) EXTENSION=serialExt  ; DRIVER="Serial Port harware driver"   ;;
  esac
  echo
  echo examining extensions and drivers loaded in $PERIPH
  $REMSH $PERIPH get installed | egrep -i $EXTENSION > /dev/null
  if [ $? -ne 0 ]
  then
    echo ""
    echo "The \"$DRIVER\" does not seem to be loaded."
    echo "Please activate this driver and the \"Remote Ports Extension\""
    echo "in the netstation configuration screen."
    echo "<Depress F12 for 2 sec><Click Terminal><Click Extensions>"
    $ECHO "Press the return key to continue ... $NL"
    read RESP
    echo ""
    exit 1
  fi
  EXTENSION=remportExt; DRIVER="Remote Ports Extension" 
  $REMSH $PERIPH get installed | egrep -i $EXTENSION > /dev/null
  if [ $? -ne 0 ]
  then
    echo ""
    echo "The \"$DRIVER\" does not seem to be loaded."
    echo "Please activate this driver in the netstation configuration screen."
    echo "<Depress F12 for 2 sec><Click Terminal><Click Extensions>"
    echo "and select the driver/extension you miss"
    $ECHO "Press the return key to continue ... $NL"
    read RESP
    echo ""
    exit 1
  fi
  echo  ".. all necessary extensions and drivers seem to be loaded "
  sleep 2
  echo
fi

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
$HPNPF $DASHN -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 netstation "Terminal" config screen.

EOF
fi

exit 0

