#!/sbin/sh
#@(#)HP Netstation Admin Scripts
#
# ckbootp	Check operation of bootp by doing a bootpquery.
#

if [ "$OS" = "sco" ]
then
  if [ -x $BOOTPD ]
  then
    echo $BOOTPD there
    grep '^bootp' /etc/inetd.conf > /dev/null
    if [ $? -eq 0 ]
    then
      echo bootp mentioned in /etc/inetd.conf
    else
      echo bootp not mentioned in /etc/inetd.conf
      exit 1
    fi
  else
    echo cannot find $BOOTPD
    exit 1
  fi
 exit 0
fi


#
# can we do bootpqueries 
#
if [ "$UNDERSTANDSBA" = "false" ]
then
  echo 
  echo version of bootpd in $BOOTPD does not seem to understand the ba tag
  echo will not do bootpquery
  echo
  exit 0
fi


HATAG=080009000000

if [ ! -x $BOOTPQUERY ] 
then
	echo ""
	echo "No $BOOTPQUERY executable, -cannot continue."
	exit 1
fi

cat <<EOF

This test will :

	1) broadcast a bootp query for a known dummy test address
	   to the local LAN segment

	2) Prompt you for the LAN hardware address of your netstation.
	   Having got that, it will emulate the query that your netstation
	   broadcasts when trying to obtain its network parameters from bootpd.

EOF

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

echo ""

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


fgrep -i $HATAG $BOOTPTABLE > /dev/null 2>&1
if [ $? -ne 0 ]
then
  echo "Warning: the dummy test entry with the LAN hardware address of $HATAG"
  echo "is not present in $BOOTPTABLE on $HOSTNAME."
else
 
  #
  #-----------------------------------------------------------------#
  # Stuff "ba:" tag into test entry if it's absent
  #-----------------------------------------------------------------#
  #
  sed -n '/^test:/,/080009000000/p' $BOOTPTABLE | grep 'ba:' > /dev/null

  if [ $? -ne 0 ]
  then
    sed '/^test:/,/080009000000/s/ht=/ba: ht=/g' $BOOTPTABLE > /tmp/bt.$$
    if [ -s /tmp/bt.$$ ]
    then
      cmp -s /tmp/bt.$$ $BOOTPTABLE
      if [ $? -ne 0 ]
      then
        cp /tmp/bt.$$ $BOOTPTABLE
      fi
    fi
    rm -f /tmp/bt.$$
  fi

  
  echo "broadcasting query for hardware address $HATAG ..."
  echo ""
  echo "========================================================="

  # On 10.0 the rbbotd daemon may be listening on 
  # udp port 67/68 for boot requests from older diskless workstations.
  #
  RBOOTDPID=""
  if [ "$OS" = "hp10" ]
  then
    RBOOTDPID=`ps -e | awk '$NF == "rbootd" {print $1}'`
    if [ "$RBOOTDPID" != "" ]
    then
      kill "$RBOOTDPID"
      sleep 2
      kill -9 "$RBOOTDPID" > /dev/null 2>&1
    fi
  fi

  if [ "$OS" = "hp10" ]
  then
    #
    # avoid using the -s command line option as 10.10 and beyond 
    # does not seem to be able to cope with it
    #
    $BOOTPQUERY $HATAG $HTYPE  2>&1 |tee /dev/tty | \
	egrep -i 'IP Address' > /dev/null
  else
    $BOOTPQUERY $HATAG $HTYPE -s $HOSTNAME 2>&1 |tee /dev/tty | \
	egrep -i 'IP Address' > /dev/null
  fi

  RET=$?

  if [ "$OS" = "hp10" -a "$RBOOTDPID" != "" ]
  then
    rbootd
  fi


  if [ $RET -eq 0 ]
  then
	cat <<EOF



=========================================================
Above response received from bootpd.
Check completed successfully.
EOF
  else
	cat <<EOF

=========================================================
No response received from bootpd
Test failed.

Check that this line is present in /etc/inetd.conf:
$BPINETDLINE

Check that these lines are present in /etc/services:
$BOOTPCLINE
$BOOTPSLINE
If you are running NIS, make sure that these lines are in the
services map on your NIS server machine.

On a number of systems inetd has to be restarted in order for
the /etc/inetd.conf file to be re-read.
Try a "kill <processid>" of the inetd task followed by re-starting it.

EOF
    $ECHO "Do you want script to kill/restart inetd process? [y|n] (y): $NL"
    read SEL
    echo ""
    case $SEL in
      y|Y|yes|YES|Yes|"")
        if [ "$OS" = "sun5" ]
        then 
          FLAG="-s"
        else
          FLAG=""
        fi

        echo "{print $PIDFIELD}" > $TMP/awk
        P=`$PSCMD | fgrep inetd | fgrep -v fgrep \
          | awk "$PPIDFIELD == 1" | awk -f $TMP/awk`
        if [ $P ]
        then
          echo "executing \"kill $P\""
          kill $P

          echo "executing \"$INETD $FLAG\""
          $INETD $FLAG
          echo "inetd restarted"
        else
          echo "could not find pid, - restart manually using \"$INETD $FLAG\""
        fi
        ;;
      *)
	echo " .. inetd left alone"
	echo
        ;;
    esac
  fi
fi

echo ""
$ECHO "Please enter the netstation LAN hardware address (q) : $NL"

read HATAG
echo ""

if [ "x$HATAG" != "x" -a "x$HATAG" != "xq" ]
then
  X=`echo $HATAG | tr ABCDEF abcdef | sed "s/^0x//"`

  fgrep -i $HATAG  $BOOTPTABLE > /dev/null 2>&1
  if [ $? -ne 0 ]
  then
    echo "Warning: An entry with the LAN hardware address $HATAG is not"
    echo "         present in $BOOTPTABLE on $HOSTNAME."
    echo "Please run $ADMINDIR/xtadm <Add netstation>"
    exit 1
  fi
else
  exit 100
fi

egrep -v '^#' $BOOTPTABLE | tr 'A-Z' 'a-z' | sed -n "1,/$HATAG/p" \
	|  egrep 'ht=' | tail -1 | egrep 'ht=tr'
if [ $? -eq 0 ]
then
  HTYPE="ieee802"
else
  HTYPE=""
fi

RBOOTDPID=""
if [ "$OS" = "hp10" ]
then
  RBOOTDPID=`ps -e | awk '$NF == "rbootd" {print $1}'`
  if [ "$RBOOTDPID" != "" ]
  then
    kill "$RBOOTDPID"
    sleep 2
  fi
fi




echo "broadcasting query for hardware address $HATAG ..."
echo ""
echo "========================================================="

if [ "$OS" = "hp10" ]
then
  #
  # avoid using the -s command line option as 10.10 and beyond 
  # does not seem to be able to cope with it
  #
  $BOOTPQUERY $HATAG $HTYPE  2>&1 |tee /dev/tty | \
    egrep -i 'IP Address' > /dev/null
else
  $BOOTPQUERY $HATAG $HTYPE -s $HOSTNAME 2>&1 |tee /dev/tty | \
    egrep -i 'IP Address' > /dev/null
fi

RET=$?

if [ "$OS" = "hp10" -a "$RBOOTDPID" != "" ]
then
  rbootd
fi

if [ $RET -eq 0 ]
then
	echo ""
	echo "========================================================="
	echo "Above response received from bootpd"
	echo "Check completed successfully."
else
	echo ""
	echo "========================================================="
	echo "No response received from bootpd"
	echo "Test failed."
	echo "Check $BOOTPTABLE for syntax errors."
	echo "Also make sure the entry you are testing has the \"ba:\" tag."
fi

