#!/bin/sh
#@(#)HP Netstation Admin Scripts
#
# cktftp	Check operation of tftp
#

if [ $FILEACCESS != "tftp" ]
then
	cat <<EOF

During tape installation you appear to have selected NFS as your
file transport mechanism for the netstation server image and fonts.
This test will only work for installations where TFTP is used for
fetching files.

EOF
	exit
fi


cat <<EOF

This test will:
	Emulate a netstation downloading a file from this computer
	using TFTP.

EOF

$ECHO "Proceed with the tftp test [y|n] (y): $NL"
read SEL

echo ""

case $SEL in
    y|Y|yes|YES|Yes|"")
	;;
    *)
	exit 100
	;;
esac

echo " "
echo "Operating System is $OS."
echo " "

# Check if tftp executable present

if [ ! -x "$TFTP" ]
then
	echo " "
	echo "ERROR: No $TFTP binary installed."
	exit 1
fi
  
echo " "
echo "tftpd executable is present."
  
# Check if the tftp service has been defined in /etc/services

grep "tftp.*69/" /etc/services 2>&1 | grep "^[^#]" > /dev/null 2>&1  
RET1=$?

ypcat services 2>/dev/null | grep "tftp.*69/"  > /dev/null 2>&1
RET2=$?

if  [ $RET1 -eq 1 -a $RET2 -eq 1 ]
then
	echo " "
	echo "ERROR: No tftp service is specified in file /etc/services."
	echo "Add a line like "tftp 69/udp" to /etc/services."
	echo "If you are running NIS, you will need to add this line
	echo "to the computer that provides your services database.
	exit 1
fi

echo " "
echo "tftp service present in /etc/services."

# Check if inetd binary present

if [ ! -x $INETD ]
then
	echo " "
	echo "ERROR: No $INETD executable."
	if [ $OS = "sco" ]
	then
	  echo "You may have to install ODT-NET services."
	  exit 1
	fi
	  
	if [ $OS = "sun4" ]
	then
	  echo "You may have to install Networking Services."
	  exit 1
	fi

	echo "You may have to install ARPA Services."
	exit 1
fi

echo " "
echo "inetd executable is present."

# Check if the inetd is being started up from
# any of the traditional places 

INETDSU="/dev/null"		# just in case nothing below matches
if [ -f /etc/rc ]
  then INETDSU="$INETDSU /etc/rc"
fi
if [ -f /etc/rc.local ]
  then INETDSU="$INETDSU /etc/rc.local"
fi
if [ -f /etc/netlinkrc ]
  then INETDSU="$INETDSU /etc/netlinkrc"
fi
if [ -f /etc/rc.boot ]
  then INETDSU="$INETDSU /etc/rc.boot"
fi
if [ "$OS" = "hp10" ]
then
  if [ -d /sbin/init.d/inetd ]
    then INETDSU="$INETDSU /sbin/init.d/inetd"
  fi
else
  if [ -d /sbin/rc0.d ]
    then INETDSU="$INETDSU /sbin/rc0.d/*inetd"
  fi
fi
if [ -f /etc/tcp ]
  then INETDSU="$INETDSU /etc/tcp"
fi

INETDFOUND=`fgrep '/inetd' $INETDSU | grep -v '#.*/inetd'`

if  [ "X$INETDFOUND" = "X" ]
then
  case $OS in
  hp)
	echo " "
	echo "WARNING: No inetd daemon is started from the /etc/netlinkrc script."
	echo "Please add a line like '/etc/inetd -l' to /etc/netlinkrc."
	;;
    sco)
	echo " "
	echo "WARNING: No inetd daemon is started from /etc/tcp"
	echo "Please add a line like '/etc/inetd' to /etc/tcp'"
	;;
      sun4)
	echo " "
	echo "WARNING: inetd is not started from the /etc/rc or /etc/rc.local scripts."
        echo "Please add a line like '/usr/etc/inetd ' to either."
	;;
  esac
else
  echo " "
  echo "inetd is present in the startup script."
fi


# Check that the inetd process is active

INETDFOUND=`$PSCMD | fgrep 'inetd' | fgrep -v 'fgrep' | wc -l`

if  [ "$INETDFOUND" = "" ]
then
	  echo "ERROR: No inetd daemon process running on `hostname`."
	  echo "Please start daemon!"
	  exit 1
fi

echo " "
echo "inetd daemon is currently running."

$PINGSTART $HOSTNAME $PINGEND > /dev/null 2>&1 
if [ ! $? -eq 0 ]
then
	echo " "
	echo "Cannot ping $HOSTNAME."
	echo "LAN card on $HOSTNAME may need to be configured with \"ifconfig\""
	echo "Also, check LAN connectors/cables and routing tables."
	exit 1
fi
 
echo " "
echo "ping of $HOSTNAME successful"

cd $TMP

if [ $OS != "sco" ]
then
  SPACELEFT=`$DFCMD $TMP | tail -1 | awk '{print $(NF-2)}'`
  if [ $SPACELEFT -lt 100 ]
  then
	echo " "
	echo "ERROR: Insufficient space for test file in $TMP."
	echo "       Cannot complete tftp tests."
	exit 1
  fi
fi

TESTFILE=$TMP/rgb.txt
FOUNDINSTALL="false"
SUCCESS="false"

RGBFILE=/opt/hpxt/enware2/xthome/lib/rgb.txt

if [ -f $RGBFILE ]
then
  FOUNDINSTALL="true"
  cat <<EOF

It appears that some version of HP Netstation Software for ENVIZEX II and
ENTRIA II netstations is installed on this system. This test will check that
these files are accessible using TFTP.

EOF

  if [ -f $TESTFILE ]
  then	rm $TESTFILE 
  	if [ $? != 0 ]
  	then
  		echo " "
  		echo "ERROR: Cannot write files to directory $TMP."
  		echo "       Please change permissions for $TMP and re-run."
  		exit 1
  	fi
  fi
  
  echo "tftp: Attempting to get a file from $HOSTNAME."
  
  tftp $HOSTNAME > /dev/null 2>&1 <<-EOF
get $RGBFILE
quit
EOF
  
  diff $RGBFILE $TESTFILE > /dev/null
  
  if [ $? != 0 ]
  then
  	echo " "
  	echo "ERROR: File rgb.txt couldn't be retrieved from file server."
  	echo "       Check routing table for $HOSTNAME."
  else
        SUCCESS="true"
  	echo "tftp: Sucessfully read the file $RGBFILE from $HOSTNAME."
  fi
  
fi

RGBFILE=/usr/lib/X11/700X/rgb.txt

if [ -f $RGBFILE ]
then
  FOUNDINSTALL="true"
  cat <<EOF

It appears that some version of ENWARE software for ENVIZEX and ENTRIA
netstations is installed on this system. This test will check that these 
files are accessible using TFTP.

EOF

  if [ -f $TESTFILE ]
  then	rm $TESTFILE 
  	if [ $? != 0 ]
  	then
  		echo " "
  		echo "ERROR: Cannot write files to directory $TMP."
  		echo "       Please change permissions for $TMP and re-run."
  		exit 1
  	fi
  fi
  
  echo "tftp: Attempting to get a file from $HOSTNAME."
  
  tftp $HOSTNAME > /dev/null 2>&1 <<-EOF
get $RGBFILE
quit
EOF
  
  diff $RGBFILE $TESTFILE > /dev/null
  
  if [ $? != 0 ]
  then
  	echo " "
  	echo "ERROR: File rgb.txt couldn't be retrieved from file server."
  	echo "       Check routing table for $HOSTNAME."
  else
        SUCCESS="true"
  	echo "tftp: Sucessfully read the file $RGBFILE from $HOSTNAME."
  fi
  
fi

if [ $FOUNDINSTALL = "false" ]
then
  echo 
  echo "Couldn't find ENWARE installed on this system."
  echo
else
  echo " "
  echo "All checks of tftp completed successfully."
  echo " "
fi

