#!/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             : rpc01
#
#  TEST DESCRIPTION : Test rpc using file transfers between a client & server
#
#  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)
#      -Ported
#
#***********************************************************************
#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}
$trace_logic
this_file=${0##*/}
TC=${TC:=rpc01}
TCbin=${TCbin:=`pwd`}
TCdat=${TCdat:=$TCbin/datafiles}
TCsrc=${TCsrc:=$TCbin}
TCtmp=${TCtmp:=$TCsrc}
NUMLOOPS=${NUMLOOPS:=3}
RHOST=${RHOST:=`hostname`}
CLEANUP=${CLEANUP:="ON"}
DATAFILES=${DATAFILES:="file.1 file.2"}

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

    # Start server process
    rsh -n $RHOST $TCbin/rpc_server 
    PID1=`rsh -n $RHOST ps -ewf | grep rpc_server | grep -v grep | awk '{print $2 }'`
    echo "The rpc_server PID is $PID1" 
    # Start client process
    echo "Starting $TC"
    COUNT=1
    while [ $COUNT -le $NUMLOOPS ]
    do
      for FILE in $DATAFILES
      do
	$TCtmp/rpc1 -s $RHOST -f $TCdat/$FILE
	[ $? -eq 0 ] || end_testcase "Fail on using $FILE"
      done 
      COUNT=`expr $COUNT + 1`
    done
}


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

    if [ "$CLEANUP" = "ON" ]; then
	if [ -f $TCtmp/core ]; then
	   echo "Core file is saved in /tmp"
	   mv $TCtmp/core /tmp
	fi

	# Kill server
        rsh -n $RHOST kill -9 $PID1
    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."

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

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

do_test
do_cleanup
end_testcase

