#!/bin/bash
set -xa
##################################################################
### Usage:  exmag_status_sync.sh.ecf $machine ########################
## Rsyncs the $MODEL status file to the backup machine after the 
## updates are made to ncorzdm.ncep.noaa.gov.
##
## 4/21/2014  Freeman  Replace random selection of remote host with
##                     production's round robin method of selecting a host
##                     If none of the hosts are pingable, print
##                     warning but do not fail.
## 8/18/2015 Deyong Xu Redmine#9090 change nmm to nmmb
## 11/02/2020 A Simon Change model name hires-fv3 to hrw-fv3
## 04/2023 A Simon    Change hwrf/hmon model names to hfsa/hfsb
##################################################################

#######################
## Begin Main Script ##
#######################
export LOCALMACH="$(hostname|cut -c-1)"
export STATUS_DIR=/com/mag/${envir}/status
export RSYNC=/usr/bin/rsync
export utilfix=/nwprod/util/fix

#############################################################

# Get next prodser node to use for transfer


if [ "$LOCALMACH" = "g" ] ; then
  REMOTEMACH="tide"
  hostlist=${utilfix}/prodser.hosts.tide
else
  REMOTEMACH="gyre"
  hostlist=${utilfix}/prodser.hosts.gyre
fi

#
# Create a new list of hosts to submit to, placing the previous host
# used at the end of the list
#

# (This doesn't run in development)

lasthost=`cat /com/logs/$envir/rsync.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

pingok="false"
for mach in `cat newlist`
do
  ping -c1 $mach
  pingerr=$?
  if [ $pingerr -eq 0 ] ; then
    pingok="true"
    WCOSSDEV=$mach
    echo $WCOSSDEV > /com/logs/$envir/rsync.lasthost
    break
  fi
done

if [ $pingok != "true" ]; then

   echo "WARNING: Could not ping any $REMOTEMACH host. Cannot rsync status files to $REMOTEMACH"

else


echo " STARTING status file sync to $WCOSSDEV for model $MODEL "

case $MODEL in 
GFS)
$RSYNC -vvr --progress --stats --delete --include='gfs_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
RAP)
$RSYNC -vvr --progress --stats --delete --include='rap_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
GEFS_MSPRD)
$RSYNC -vvr --progress --stats --delete --include='gefs-mnsprd_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
GEFS_SPAG)
$RSYNC -vvr --progress --stats --delete --include='gefs-spag_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
RTMA)
$RSYNC -vvr --progress --stats --delete --include='rtma_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
RTMA_GUAM)
$RSYNC -vvr --progress --stats --delete --include='rtma-guam_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
GFS-WAVE)
$RSYNC -vvr --progress --stats --delete --include='gfs-wave_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
NAEFS)
$RSYNC -vvr --progress --stats --delete --include='naefs_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
ATCF)
$RSYNC -vvr --progress --stats --delete --include='atcf_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
POLAR)
$RSYNC -vvr --progress --stats --delete --include='polar_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
SKEWT)
$RSYNC -vvr --progress --stats --delete --include='skewt_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
UAIR)
$RSYNC -vvr --progress --stats --delete --include='uair_*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
HRRR)
$RSYNC -vvr --progress --stats --delete --include='hrrr*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
NAM)
$RSYNC -vvr --progress --stats --delete --include='hrrr*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
NAM-HIRES)
$RSYNC -vvr --progress --stats --delete --include='hrrr*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
HRW-ARW)
$RSYNC -vvr --progress --stats --delete --include='hrrr*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
HRW-ARW2)
$RSYNC -vvr --progress --stats --delete --include='hrrr*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
HRW-FV3)
$RSYNC -vvr --progress --stats --delete --include='hrrr*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
AIGFS)
$RSYNC -vvr --progress --stats --delete --include='hrrr*' --exclude='*' $STATUS_DIR/ $WCOSSDEV:$STATUS_DIR/
;;
*)
echo "NO \$MODEL name given"
;;
esac

export err=$?
if [ "$err" -eq 24 -o "$err" -eq 23 ]; then
  # this trap was added after noticing that some status files could be destroyed whilst
  # processing MAG data - this is just a warning and should be harmless
  echo "code 24 - some files vanished before they could be transferred - NOT FATAL"
  err=0
fi
err_chk
fi
date
exit
