#!/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   : rdist
#
#  PURPOSE: To test the basic functionality of the `rdist` 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:
#    06/06/03 Manoj Iyer manjo@mail.utexas.edu
#    - Modified testcases to use test harness APIs and fixed defects
#    03/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Ported
#
#
#***********************************************************************

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

do_setup()
{

    USER_LIST=${USER_LIST:-'root'}
    TCdat=${TCdat:-$LTPROOT/testcases/bin/datafiles}
    TCtmp=${TCtmp:-$LTPROOT/testcases/bin/$TC${EXEC_SUFFIX}$$}
    FILES=${FILES:-'bin.sm bin.med bin.lg bin.jmb file.dir/bin.sm file.dir/bin.med file.dir/bin.jmb'}
    SLEEPTIME=${SLEEPTIME:-10}

    tst_setup

    exists awk hostname rdist

    RHOST=${RHOST:-`hostname`}

    # start with a clean LHOST
    for i in $FILES; do
        rm -rf $i
    done

    for i in $FILES; do
        BASE=${i##*/}
        DIR=${%%/*}
        test -d "$DIR" || mkdir -p "$DIR"
        if [ $? -ne 0 ] ; then
            end_testcase "failed to create $DIR"
        fi
        if ! cp $TCdat/$BASE $DIR; then
            end_testcase "failed to copy $TCdat/$BASE to $DIR"
        fi
        chmod 764 $i
    done

    # get the sum of all the files to rdist on the local machine
    LSUM=0
    SUM=`sum -s $FILES | awk '{ print $1 }'`
    for i in $SUM; do
        LSUM=$(( $LSUM + $i ))
    done
}

#-----------------------------------------------------------------------
#
# FUNCTION:  create_distfile
# create file $TCtmp/distfile
#
#-----------------------------------------------------------------------

create_distfile()
{
    T_FILES="FILES = ( "
    for i in $FILES; do
        if [ "${i%%*/}" = "." ]; then
            T_FILES="$T_FILES $i"
        else
            T_FILES="$T_FILES $DIR"
        fi
    done
    T_FILES="$T_FILES )"

    T_HOST="HOSTS = ("
    for c_ruser in $RUSERS; do
        for c_rhost in $HOSTS; do
         T_HOST=$T_HOST"$c_ruser@$c_rhost "
        done
    done

    T_HOST="$T_HOST)"
    cat <<EOF > "$TCtmp/distfile"
$T_HOST
$T_FILES
${FILES} -> ${HOSTS}
EOF
}

#-----------------------------------------------------------------------
#
# FUNCTION:  check_result
#
# check the sum of all files rdisted from local machine to remote machine
#
#-----------------------------------------------------------------------

check_result()
{
    cd $TCtmp
    for c_rhost in $HOSTS; do
        for c_ruser in $RUSERS; do
            TOTAL_SUM=`rsh -n -l $c_ruser $c_rhost \
            x=0; SUM=\$(sum -s $FILES|awk '{ print \$1 }')
            for i in \$SUM; do
                x=\$(( \$x + \$i ))
            done
            echo \$x`
            if [ $TOTAL_SUM = $LSUM ]; then 
                tst_resm TINFO "Success rdist in $c_ruser@$c_rhost "
                rsh -n -l $c_ruser $c_rhost "rm -rf $FILES $DIRECTORIES"
            else
                end_testcase "Wrong sum doing  rdist in $curr_ruser@$curr_rhost"
            fi
       done
   done
}

#----------------------------------------------------------------------
# FUNCTION: do_test
# PURPOSE:  Perform the necessary steps to complete the test.
# INPUT:    None.
# OUPUT:    Error messages are logged if any of the tests fail.
# EXIT VAR: 0 Success
#----------------------------------------------------------------------

do_test()
{
    HOSTS=""
    RUSERS=""

    for cur_host in $RHOST; do
        HOSTS=$HOSTS" $cur_host"
        for cur_user in $USER_LIST; do
            RUSERS=$RUSERS" $cur_user"
            create_distfile
            if ! rdist -f $TCtmp/distfile; then
                end_testcase "error doing rdist -f $TCtmp/distfile"
            fi
            check_result
            sleep $SLEEPTIME
        done
    done
}

#----------------------------------------------------------------------
# FUNCTION: MAIN
# PURPOSE:  To invoke the functions to perform the tasks described in
#           the prologue.
# INPUT:    None.
# OUTPUT:   A testcase run log with the results of the execution of this
#           test.
#----------------------------------------------------------------------
. net_cmdlib.sh

read_opts $*
do_setup
do_test
end_testcase
