set -x

#
# Suspend running transfer_gefs jobs
#
if [ ${SENDSMS} = 'YES' ]; then
echo "login -t 60 $SMSNODE $SMSNAME $SMSPASS" > cdp_cmds
fi

echo "suspend /transfers/transfer1/gefs/gefs"${cyc} >> cdp_cmds
echo "requeue -f /transfers/transfer1/gefs/gefs"${cyc}"/gefs"${cyc}"_1" >> cdp_cmds
echo "requeue -f /transfers/transfer1/gefs/gefs"${cyc}"/gefs"${cyc}"_2" >> cdp_cmds
echo "exit" >> cdp_cmds

$SMSBIN/cdp < cdp_cmds

#
# Check the running transfer_gefs jobs for perticular cycle and cancel it
#

whoami

llq -f %jn %id %o %dd %st %p %c %nh | sort -k2 | grep prod | grep transfer_gefs${cyc} |awk '{print $2}' >>cancel.list
cat cancel.list

if [ -s cancel.list ]; then
  for id in `cat cancel.list`
  do
    llcancel $id
  done
else
  echo " No runing tansfer_gefs jobs for cycle: "${cyc}z
fi

#
# Transfer necessary GEFS files for SREF
#

sorf=`hostname | cut -c1`
if [ "$sorf" = "s" ] ; then
  hostlist=${utilfix}/interactive.hosts.cirrus
else
  hostlist=${utilfix}/interactive.hosts.stratus
fi

#
# Create a new list of hosts to submit to, placing the previous host
# used at the end of the list
#
lasthost=`cat /com/logs/rdist.lasthost`
numhosts=`cat $hostlist | wc -l`
lastnum=`grep -n $lasthost $hostlist | awk -F: '{print $1}'`
let newnum=numhosts-lastnum
tail -$newnum $hostlist > newlist
head -$lastnum $hostlist >> newlist

for mach in `cat newlist`
do
  ping -c1 $mach
  pingerr=$?
  if [ $pingerr -eq 0 ] ; then
    host=$mach
    echo $host > /com/logs/rdist.lasthost
    break
  fi
done

if [ $pingerr -ne 0 ] ; then
  err_exit "NO AVAILABLE MACHINES TO TRANSFER FILES TO"
fi

echo "HOSTS = ( $host )" > ./distlist

sed s/_PDY_/$PDY/g $PARMtrans/transfer_gefs4sref_${cyc}.list > ./tmp
sed s/_PDYp1_/$PDYp1/g ./tmp >> ./tmp5
sed s/_PDYm1_/$PDYm1/g ./tmp5 >> ./tmp4
sed s/_PDYm2_/$PDYm2/g ./tmp4 >> ./tmp3
sed s/_PDYm3_/$PDYm3/g ./tmp3 >> ./tmp2
sed s/_PDYm4_/$PDYm4/g ./tmp2 >> ./tmp1
sed s/_PDYm5_/$PDYm5/g ./tmp1 >> ./tmp6
sed s/_PDYm6_/$PDYm6/g ./tmp6 >> ./tmp7
sed s/_PDYm7_/$PDYm7/g ./tmp7 >> ./distlist

set +x
echo "################# DISTLIST FOLLOWS ###################"
cat ./distlist
echo "######################################################"
set -x

timex /usr/sbin/rdist -P /nwprod/scripts/nwprod.ossh -f ./distlist

errdist=$?
if [ $errdist -ne 0 ]
then
  echo "$errdist : $jobid" >> /com/logs/test/rdist_log
fi

rm -f cdp_cmds

if [ ${SENDSMS} = 'YES' ]; then
echo "login -t 60 $SMSNODE $SMSNAME $SMSPASS" > cdp_cmds
fi

echo "requeue -f /transfers/transfer1/gefs/gefs"${cyc} >> cdp_cmds
echo "exit" >> cdp_cmds

$SMSBIN/cdp < cdp_cmds
