#! /bin/sh
#
#pragma ident   "@(#)fdl_rshstatus.sh 1.4   01/03/28 SMI"
#
# Copyright (c) 1994-1998 by Sun Microsystems, Inc.
# All rights reserved.
#

# Usage: fdl_rshstatus [-y] <normal rsh arguments other than -n>
#
# Like rsh, but:
#
# (1) captures the exit status of the command running 
# on the remote host and passes it back as our own exit status.
# Some errors in the caller side cause us to exit 1.
#
# (2) Also, this script by default passes the -n switch to rsh.
# That behavior may be overriden by suppying the -y argument to
# this script, which must be the very first argument.
#
# (3) Using this command will cause all stderr output from the
# remote command to appear before all stdout output from the remote
# command.  If you would prefer to see them interpersed, prefix
# your real command name with the subroutine command merge_err_on_out, 
# e.g.:
#
#    fdl_rshstatus remotehost ${FM_PROGS}/fdl_merge_err_on_out command args
#


argv0=`basename $0`

# Use existing or default value of HA_BIN:
CLUSTERBIN=/opt/SUNWcluster/bin
export CLUSTERBIN

NFS_BIN=/opt/SUNWcluster/ha/nfs
PATH=$NFS_BIN:$CLUSTERBIN:${PATH}:/usr/bin:/usr/sbin:/etc:/usr/bin
export PATH

pre="fdl_rshstatus"

errboth()
{
    echo "${PROG}: Error: $*" 1>&2
    log_info "{pre}.$1" "$2"
}    


cleanup()
{
    rm -f $TMP $TMP2
}


PROG=`basename $0`
TMPDIR=/var/opt/SUNWcluster/run
if [ -n "$HA_TMP" ]; then
    TMPDIR="$HA_TMP"
fi
TMP=$TMPDIR/fdl_rshstatus.tmp.$$
TMP2=$TMPDIR/fdl_rshstatus.tmp2.$$

TRAPSIGNALS="1 2 3 15"
trap "cleanup; trap 0; exit 1" $TRAPSIGNALS

NSW=1
while [ "$1" = "-n" -o "$1" = "-y" ]; do
    if [ "$1" = "-n" ]; then
	NSW=1
        shift
    fi
    if [ "$1" = "-y" ]; then
	NSW=0
        shift
    fi
done

REMOTEHOST=$1
shift

if [ $NSW -eq 1 ]; then
    NSTR="-n"
else
    NSTR=""
fi

touch $TMP
if [ ! -f $TMP ]; then
    errboth "3010" "Could not create file $TMP"
fi    
rm $TMP
RMTCMD=$1
CMD="rsh $NSTR $REMOTEHOST /opt/SUNWcluster/ha/nfs/fdl_printstatus $*"
$CMD > $TMP
RSHRC=$?
if [ $RSHRC -eq 0 ]; then
    ping $REMOTEHOST 20 >/dev/null 2>&1
    if [ $? -ne 0 ]; then
	errboth "3040" "ping of $REMOTEHOST failed"
        RSHRC=1
    fi
fi    
if [ $RSHRC -ne 0 ]; then
    MSG="rsh exited failure (${RSHRC}), host $REMOTEHOST, cmd $RMTCMD"
    errboth "3050"  $MSG
    # Fall thru in case all of the output did show up on $TMP
fi

# If $TMP contains no lines of output, then the tail will produce nothing,
# and the grep will fail (which is okay).
tail -1 $TMP | grep '^fdl_printstatus: status =' > $TMP2
if [ $? -ne 0 ]; then
    cat $TMP
    # If the rsh or ping failed above, do not complain about the grep failing:
    if [ $RSHRC -eq 0 ]; then
        MSG="could not find 'fdl_printstatus' line in rsh output, \
host $REMOTEHOST, cmd $RMTCMD"
        errboth "3060"  $MSG
    fi
    #cleanup
    exit 1
fi

RC=`awk '{print $4}' < $TMP2`
LINECNT=`wc -l <$TMP`
LINECNT=`expr $LINECNT - 1`
head -${LINECNT} $TMP
cleanup
if [ $RSHRC -ne 0 ]; then
    exit 1
else
    exit $RC
fi
