#!/bin/sh
#
#   Copyright (c) International Business Machines  Corp., 2000
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this pronram;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#  FILE   : rwho
#
#  PURPOSE: To test the basic functionality of the rwhod daemon using the
#           `rwho` and `ruptime` commands.
#
#  SETUP: The home directory of root on the machine exported as "RHOST"
#         MUST have a ".rhosts" file with the hostname of the machine
#         where the test is executed. Also, both machines MUST have
#         the rwhod daemon installed. However, it does not need to be
#         active, the test will handle this. 
#
#  HISTORY:
#    06/09 Manoj Iyer manjo@mail.utexas.edu
#    - Modified to use test harness API and also fix defects
#    03/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
#
#==============================================================================
# error codes:  0 rwho/ruptime successful
# 	        1 rwho failed no local and remote host in file
# 	        2 ruptime failed no local and remote host in file
#==============================================================================
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic

LHOST=`hostname | cut -f1 -d.`
RHOST=${RHOST:-$LHOST}
TC=rwho
TCtmp=${TCtmp:-/tmp/$TC}
CLEANUP=${CLEANUP:-ON}

PID=0
RHOST_PID=0
SLEEPTIME=${SLEEPTIME:-20}
NUMLOOPS=${NUMLOOPS:-25}
OUTFILE=${OUTFILE:-$TCtmp/rwho.out}

#-----------------------------------------------------------------------
#
# FUNCTION:  exists
#
#-----------------------------------------------------------------------

exists()
{
   for cmd in $1
   do
       which $cmd 2>&1 1>/dev/null
	   if [ $? -ne 0 ]
	   then
	       tst_resm TBROK "Test broke: command $cmd not found"
		   exit 1
       fi
   done
}


#-----------------------------------------------------------------------
#
# FUNCTION:  do_setup
#
#-----------------------------------------------------------------------

do_setup()
{
   $trace_logic

   export TCID=rwho01
   export TST_TOTAL=1
   export TST_COUNT=1

   exists "rwho rsh killall grep awk cut"

   ps -ef | grep rwhod | grep -v grep 
   if [ $? -ne 0 ]; then
      tst_resm TINFO "Starting rwhod on $LHOST"
      /usr/sbin/rwhod  
      [ $? -eq 0 ] || end_testcase "Unable to start rwhod on $LHOST"
      PID=1
      sleep $SLEEPTIME
   fi

   rsh -n -l root $RHOST ps -ef | grep rwhod
   if [ $? -ne 0 ]; then
      tst_resm TINFO "Starting rwhod on $RHOST"
      rsh -n -l root $RHOST /usr/sbin/rwhod
      if [ $? -ne 0 ]; then
	  end_testcase "Unable to start rwhod on $RHOST"
      fi
      RHOST_PID=$(rsh -n -l root $RHOST ps -ef | grep rwhod | grep -v grep | awk '{print $2 ; exit}')
      if [ -z "$RHOST_PID" ]; then
	  RHOST_PID=0
	  end_testcase "Unable to start rwhod on $RHOST"
      fi
      sleep $SLEEPTIME
   fi

   RHOSTNAME=`rsh -n -l root $RHOST hostname | cut -f1 -d.`
   if [ -z "$RHOSTNAME" ]; then
       end_testcase "Unable to determine RHOSTNAME"
   fi

   mkdir -p $TCtmp
}

#-----------------------------------------------------------------------
#
# FUNCTION:  do_test
#
#-----------------------------------------------------------------------

do_test()
{
   $trace_logic

   COUNT=1
   while [ $COUNT -le $NUMLOOPS ]
   do
      rwho -a > $OUTFILE
      HOST=`awk '{print $2}' $OUTFILE|grep "$LHOST\>"|cut -f1 -d:|sort -u`
      [ "$HOST" = "$LHOST" ] || end_testcase "$LHOST is not in rwho outfile"
      HOST=`awk '{print $2}' $OUTFILE|grep "$RHOSTNAME\>"|cut -f1 -d:|sort -u`
      [ "$HOST" = "$RHOSTNAME" ] || end_testcase "$RHOSTNAME is not in rwho outfile"

      ruptime -a > $OUTFILE
      HOST=`awk '{print $1}' $OUTFILE|grep "$LHOST\>"|sort -u`
      [ "$HOST" = "$LHOST" ] || end_testcase "$LHOST is not in ruptime outfile"
      HOST=`awk '{print $1}' $OUTFILE|grep "$RHOSTNAME\>"|sort -u`
      [ "$HOST" = "$RHOSTNAME" ] || end_testcase "$RHOSTNAME is not in ruptime outfile"

      tst_resm TINFO "Test $COUNT of $NUMLOOPS complete"
      COUNT=$(( $COUNT + 1 ))
   done
}

#-----------------------------------------------------------------------
#
# FUNCTION:  do_cleanup
#
#-----------------------------------------------------------------------

do_cleanup()
{
 rm -fr $TCtmp
 if [ $PID -ne 0 ]; then
    tst_resm TINFO "Stopping rwhod on $LHOST"
    killall rwhod 
 fi

 if [ $RHOST_PID -ne 0 ]; then
    tst_resm TINFO "Stopping rwhod on $RHOST"
    rsh -n -l root $RHOST "killall rwhod"
 fi
}
 
#=============================================================================
# FUNCTION NAME:        end_testcase
#
# FUNCTION DESCRIPTION: Clean up
#
# PARAMETERS:           string, IF AND ONLY IF the testcase fails
#
# RETURNS:              None.
#=============================================================================

end_testcase()
{
   $trace_logic
   
   if [ $CLEANUP = "ON" ]; then
     do_cleanup
   fi
   [ $# -eq 0 ] && { tst_resm TPASS "Test Successful"; exit 0; }
   tst_resm TFAIL "Test Failed: $@"
   exit 1
}

#-----------------------------------------------------------------------
#
# FUNCTION:  MAIN
#
#-----------------------------------------------------------------------
do_setup
do_test
end_testcase
