#!/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
#==============================================================================

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

LHOST_PID=""
RHOST_PID=""

do_setup()
{
    TCtmp=${TCtmp:-$LTPROOT/testcases/bin/$TC${EXEC_SUFFIX}$$}

    SLEEPTIME=${SLEEPTIME:-5}
    NUMLOOPS=${NUMLOOPS:-25}
    OUTFILE=${OUTFILE:-$TCtmp/${TC}.out}

    tst_setup

    exists awk cut hostname killall ps rsh rwho

    LHOST=`hostname | cut -f1 -d.`
    RHOST=${RHOST:-$LHOST}

    trap do_cleanup EXIT

    if ! pgrep -x rwhod > /dev/null; then
        tst_resm TINFO "Starting rwhod on $LHOST"
        rwhod || end_testcase "Unable to start rwhod on $LHOST"
        LHOST_PID=$(pgrep -x rwhod)
        sleep $SLEEPTIME
    fi

    if [ "$(rsh -n -l root $RHOST pgrep -x rwhod)" == "" ]; then
        tst_resm TINFO "Starting rwhod on $RHOST"
        rsh -n -l root $RHOST /usr/sbin/rwhod
        RHOST_PID=$(rsh -n -l root $RHOST pgrep -x rwhod)
        if [ -z "$RHOST_PID" ]; then
            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
}

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

do_test()
{
    while [ $TST_COUNT -le $NUMLOOPS ]; do
        rwho -a > $OUTFILE
        HOST=`grep $LHOST $OUTFILE | sed 's/[^ ]* *//; s/:.*//' | uniq`
        [ "$HOST" = "$LHOST" ] || end_testcase "$LHOST is not in rwho outfile"
        HOST=`grep $RHOSTNAME $OUTFILE | sed 's/[^ ]* *//; s/:.*//' | uniq`
        [ "$HOST" = "$RHOSTNAME" ] || end_testcase "$RHOSTNAME is not in rwho outfile"

        ruptime -a > $OUTFILE
        HOST=`grep $LHOST $OUTFILE | sed 's/ .*//' | uniq`
        [ "$HOST" = "$LHOST" ] || end_testcase "$LHOST is not in ruptime outfile"
        HOST=`grep $RHOSTNAME $OUTFILE | sed 's/ .*//' | uniq`
        [ "$HOST" = "$RHOSTNAME" ] || end_testcase "$RHOSTNAME is not in ruptime outfile"

        tst_resm TINFO "Test $TST_COUNT of $NUMLOOPS complete"
        incr_tst_count
    done
}

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

do_cleanup()
{
    if [ -n "$LHOST_PID" ]; then
        tst_resm TINFO "Stopping rwhod on $LHOST"
        killall rwhod
    fi

    if [ -n "$RHOST_PID" ]; then
        tst_resm TINFO "Stopping rwhod on $RHOST"
        rsh -n -l root $RHOST "killall rwhod"
    fi

    tst_cleanup
}

#-----------------------------------------------------------------------
#
# FUNCTION:  MAIN
#
#-----------------------------------------------------------------------
. net_cmdlib.sh

read_opts $*
do_setup
do_test
do_cleanup
end_testcase