#!/bin/bash
#==============================================================
#  Name: rfScriptTest.sh
#  Abs:
#
#  Facility: LCLS-II
#
#  Auth: Sonya Hoobler, 15 May 2018
#  Rev:  dd-mmm-yyyy, First Lastname (USERNAME)
#
#
#--------------------------------------------------------------
#  Mod:
#   
#--------------------------------------------------------------
#
#  Usage:
#
#============================================================


# Check for input arguments
NARGS=$#
if [ $NARGS -ne 3 ]
then
        echo "rfScriptTest.sh: 3 arguments expected, but received ${NARGS}."
        exit 0
fi

CPU=`echo $1`
USER=`echo $2`
LOGDIR=`echo $3`

DATE=`date +%F-%H_%M_%S`
DATEPV=`echo ${DATE} | tr _ :`
FILENAME=logfiletest-${DATE}.txt

LOGFILE=${LOGDIR}/${FILENAME}

echo "rfScriptTest.sh: Starting rack checkout."
echo "Executing: ssh ${CPU} -l ${USER} sh /afs/slac/g/lcls/tools/script/rfRackTestCommand.sh 2>&1 | tee ${LOGFILE}"

# Add "-l" flag to force login shell (so that .profile is read)
# Excerpt from ash manual:
#    If the first character of argument zero to the shell is ``-'', the  shell
#    is  assumed  to be a login shell, and the files /etc/profile and .profile
#    are read if they exist. 
ssh ${CPU} -l ${USER} sh /afs/slac/g/lcls/tools/script/rfRackTestCommand.sh 2>&1 | tee ${LOGFILE}

# Capture exit code from lcls2_rack.sh (not from tee)
RETURN=${PIPESTATUS[0]}

echo "Log file can be found at ${LOGFILE}"

exit 0
