#!/bin/sh
################################################################3
#
#  This script will tar up files in the /nwprod directory and save the
#  the tar files on the HPSS server under ${HPSSOUT} in the
#  appropriate directory for the Date (YYYYMMDD) given as the first
#  argument.
#
##########  NOTE - THIS IS NO LONGER VALID  ###########
#  If the YYYYMMDD argument specifies the first day of a month
#  (i.e. DD = 01), then all the /nwprod/fix/clim_* will be saved in
#  nwprod.climfix.tar.
####################################################### 
#
#  If YYYYMMDD is a Sunday, all files in /nwprod, except the 
#  /nwprod/fix/clim_* files mentioned above, will be tarred up into
#  two tarfiles.  nwprod.fix.tar will contain the files in /nwprod/fix,
#  and nwprod.tar will contain everything else.
#
#  If YYYYMMDD is not a Sunday, then every files that has been modified
#  after the previous Sunday will be tarred up and saved in file
#  nwprod.update.tar.
#
#  Usage: rhist_savenwprod.sh Date(YYYYMMDD format)
#
#  Where: Date(YYYYMMDD format) = Day that the tar file(s) should be saved 
#                                 under.
#
################################################################3
set -x

if [ $# -ne 1 ]
then
   echo "usage rhish_savenwprod.sh YYYYMMDD"
   exit 1
fi
YYYYMMDD=$1
dow=$($utilexec/dayofweek $YYYYMMDD)
dom=$(echo $YYYYMMDD | cut -c 7-8)
#
#   Determine the directory where the tar file will be stored
#   and make sure that it exists on the S70.
#

year=`echo $YYYYMMDD | cut -c 1-4`
yearmo=`echo $YYYYMMDD | cut -c 1-6`
yrmoday=$YYYYMMDD

if [ $TSM_FLAG = 'NO' ]
then
  hpssdir=${HPSSOUT}/rh${year}/${yearmo}/$yrmoday 
  hsi mkdir -p -m 755 $hpssdir
elif [ $TSM_FLAG = 'YES' ]
then
  rhistdir=${TSMOUT}/rh${year}/${yearmo}/$yrmoday 
  ssh ibmtsm1.ncep.noaa.gov "mkdir -p -m 755 $rhistdir"
fi

let sumerr=0

#
#  If Sunday...
#

if [ $TSM_FLAG = 'NO' ]
then
  if [ $dow = "Sun" ]
  then
     cd /nwprod
     dirlist=`ls -1 | grep -v fix`
     touch /nwprod/ush/rhist_savenwprod.sh
     htar -cVf ${hpssdir}/nwprod.tar $dirlist
     hsi "chmod 775 ${hpssdir}/nwprod.tar"
     hsi "chmod 775 ${hpssdir}/nwprod.tar.idx"
     htar -tvf ${hpssdir}/nwprod.tar
     err=$?
     let sumerr=sumerr+err

     filelist=`find fix | grep -v clim_`
     find fix | grep -v clim_ > ${DATA}/filelist
     htar -cVf ${hpssdir}/nwprod.fix.tar -L ${DATA}/filelist
     hsi "chmod 775 ${hpssdir}/nwprod.fix.tar"
     hsi "chmod 775 ${hpssdir}/nwprod.fix.tar.idx"
     htar -tvf ${hpssdir}/nwprod.fix.tar
     err=$?
     let sumerr=sumerr+err

  else

     touch -t ${PDYm1}0000 $DATA/time_check 
     cd /nwprod
     find . -type f -newer $DATA/time_check > ${DATA}/tmplist
     if [ -s ${DATA}/tmplist ]
     then
        htar -cVf ${hpssdir}/nwprod.update.tar -L ${DATA}/tmplist
        hsi "chmod 775 ${hpssdir}/nwprod.update.tar"
        hsi "chmod 775 ${hpssdir}/nwprod.update.tar.idx"
        htar -tvf ${hpssdir}/nwprod.update.tar
        err=$?
        let sumerr=sumerr+err
     fi

  fi       #end Sunday
elif [ $TSM_FLAG = 'YES' ]
then
  if [ $dow = "Sun" ]
  then
     cd /nwprod
     dirlist=`ls -1 | grep -v fix`
     touch /nwprod/ush/rhist_savenwprod.sh
     gtar -cvf ${DATA}/nwprod.tar $dirlist
     err=$?
     let sumerr=sumerr+err
     $SCP $SCP_CONFIG ${DATA}/nwprod.tar ibmtsm1.ncep.noaa.gov:${rhistdir}/nwprod.tar
     ssh ibmtsm1.ncep.noaa.gov "chmod 775 ${rhistdir}/nwprod.tar"
     find fix | grep -v clim_ > ${DATA}/filelist
     gtar -cvf ${DATA}/nwprod.fix.tar -T ${DATA}/filelist
     err=$?
     let sumerr=sumerr+err
     $SCP $SCP_CONFIG ${DATA}/nwprod.fix.tar ibmtsm1.ncep.noaa.gov:${rhistdir}/nwprod.fix.tar
     ssh ibmtsm1.ncep.noaa.gov "chmod 775 ${rhistdir}/nwprod.fix.tar"
 
  else
     touch -t ${PDYm1}0000 $DATA/time_check
     cd /nwprod
     find . -type f -newer $DATA/time_check > ${DATA}/tmplist
     if [ -s ${DATA}/tmplist ]
     then
        gtar -cvf ${DATA}/nwprod.update.tar -T ${DATA}/tmplist
        err=$?
        let sumerr=sumerr+err
        $SCP $SCP_CONFIG ${DATA}/nwprod.update.tar ibmtsm1.ncep.noaa.gov:${rhistdir}/nwprod.update.tar
        ssh ibmtsm1.ncep.noaa.gov "chmod 775 ${rhistdir}/nwprod.update.tar"

     fi
 
  fi       #end Sunday
fi

if [ sumerr -ne 0 ]
then
  exit 4
else
  exit 0
fi
