#!/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 program;  if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
#  FILE             : rup
#
#  TEST DESCRIPTION : Basic test for the `rup` command. 
#
#  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.
#
#  HISTORY:
#    04/18/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Written
#
# CMD      FLAG      ARGS       
# rup
# rup                rem_host
# rup	    -d
# rup       -h
# rup       -t
# rup       -l
# rup                bogus_host
# rup      -bogus_flag
#***********************************************************************
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}

$trace_logic
this_file=${0##*/}
TC=rusers
TCtmp=${TCtmp:=`pwd`}
TCbin=${TCbin:=`pwd`}
TCsrc=${TCsrc:=$TCbin}
LUSER=${LUSER:=root}
RHOST=${RHOST:=`hostname`}
CLEANUP=${CLEANUP:="ON"}
PID=0

#=============================================================================
# FUNCTION NAME:        do_test
#
# FUNCTION DESCRIPTION: Perform the test
#
# PARAMETERS:           None.
#
# RETURNS:              None.
#=============================================================================
do_test()
{
$trace_logic

echo "Checking for rstatd on $RHOST"

rpcinfo -u $RHOST rstatd 3 > /dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "Attempting to start rstatd on $RHOST"
  rsh -n -l root $RHOST "/usr/sbin/rpc.rstatd &"
  [ $? -eq 0 ] || end_testcase "rstatd is inactive on $RHOST" 
  PID=`rsh -n $RHOST ps -ewf | grep rstatd | awk '{print $2 }'`
  echo "rstatd started on $RHOST"
fi

echo "Test rup with defaults....please be patient"
# rusers with no options broadcasts over the net and reports 
# responses as it receives them. Time-out for responses is approx. 2 minutes.

echo "rup"
rup  
[ $? -eq 0 ] || end_testcase "rup with defaults - failed"

echo "Test rusers with options set...please be patient"
# Go through matrix of rup options:

echo "rup $RHOST"
rup $RHOST | grep $RHOST  
[ $? -eq 0 ] || end_testcase "rup $RHOST - failed"

echo "rup -d"
rup -d   
[ $? -eq 0 ] || end_testcase "rup -d - failed"

echo "rup -h"
rup -h   
[ $? -eq 0 ] || end_testcase "rup -h - failed" 

echo "rup -l"
rup -l  
[ $? -eq 0 ] || end_testcase "rup -l - failed"   

echo "rup -t"
rup -t  
[ $? -eq 0 ] || end_testcase "rup -t - failed"   

echo "Test rusers with bad options"
echo "rup <invalid hostname>"
rup bogushost > /dev/null 2>&1
[ $? -ne 0 ] || end_testcase "rup <invalid hostname> should fail"

echo "rup -<invalid flag>"
rup -bogusflag > /dev/null 2>&1
[ $? -eq 1 ] || end_testcase "rup -<invalid flag> should fail"
}

#=============================================================================
# FUNCTION NAME:        do_cleanup
#
# FUNCTION DESCRIPTION: Clean up
#
# PARAMETERS:           None.
#
# RETURNS:              None.
#=============================================================================
do_cleanup()
{
    $trace_logic

    if [ "$PID" != 0 ]; then
        # Kill rup on remote machine
        rsh -n $RHOST kill -15 $PID
        echo "rstatd daemon stopped on $RHOST"
    fi
}

#=============================================================================
# FUNCTION NAME:        end_testcase
#
# FUNCTION DESCRIPTION: Clean up
#
# PARAMETERS:           string, IF AND ONLY IF the testcase fails
#
# RETURNS:              None.
#=============================================================================

end_testcase()
{
   $trace_logic
   echo "$this_file: doing $0."
   if [ "$CLEANUP" = "ON" ]; then
     do_cleanup
   fi

   [ $# = 0 ] && { echo "Test Successfull"; exit 0; }
   echo "Test Failed: $@"
   exit 1
}

#=============================================================================
# MAIN PROCEDURE
#=============================================================================

do_test
end_testcase
