#!/bin/sh
################################################################
#
#  This script will create a tar file that named with information
#  from 2 command line arguments. 
#
#  Usage: rhist_savesstoiqd.sh Directory Date(YYYYMMDDHH format)
#
#  The first argument ($1) provides the path to the data files that
#  will be inserted into the tar file. Also, the path is used as part
#  of the tar file name. e.g. /com/omb/prod/sstoiqd.20101020 will
#  be processed to become the tar file name prefix com_omb_prod_sstoiqd
#  
#  The final location, hpss directory path, of the tar file is based
#  on the duration for which the data is to be maintained as well as
#  the date when the data is produced. The base HPSS directory is the
#  following: ${HPSSOUT}.  The tar file is saved under
#  a dated directory path that is based on the date provided in
#  the second command line argument ($2). 
#  e.g. ${HPSSOUT}/rh2010/201010/20101020/
#
#  This script creates a tar file bundle that will be set with
#  the rstprod group access because of the data contained in a 
#  few bufr files. And example the tar bundle file name may be:
#  com_omb_prod_sstoiqd.2010102012.tar
#
#  Usage: rhist_savesstoiqd.sh Directory Date(YYYYMMDDHH format)
#
#  Where: Directory = Where the data files are held on CCS
#         Date(YYYYMMDDHH format) = Day that the tar file should 
#         be saved under.
#
################################################################
set -x

if [ $# -ne 2 ]
then
  echo "Usage: rhist_savesstoiqd.sh Directory Date(YYYYMMDDHH format) "
  exit 1
fi 

#
#   Get directory to be tarred from the first command line argument,
#   and check to make sure that the directory exists.
#

dir=$1
if [ ! -d $dir ]
then
  echo "rhist_savesstoiqd.sh:  Directory $dir does not exist."
  exit 2
fi 

#
#   Determine the directory where the tar file will be stored
#   and make sure that it exists in HPSS.
#

year=`echo $2 | cut -c 1-4`
yearmo=`echo $2 | cut -c 1-6`
yrmoday=`echo $2 | cut -c 1-8`
rhcyc=`echo $2 | cut -c 9-10`
rhcycle=t${rhcyc}z

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

######################################################################
#   Get a listing of all files in the directory to be tarred
#   Then cd to the directory to be tarred.
######################################################################

cd $DATA
ls -1 $dir | awk '
            /sstoiqd/ { print "./"$0 > "allsstoiqd" ; next } '

cd $dir

#  Now create a tar file for each group of files. At this time
#  there is only one group. 

for file in allsstoiqd
do

   #
   #   Keep all SSTOIQD files permanently, for reanalysis
   #

   hpssdir=$hpssdir0
   rhistdir=$rhistdir0

   #
   #   Generate the name of the tarfile, which should be the same
   #   as the absolute path name of the directory being
   #   tarred, except that "/" are replaced with "_".
   #

   tarfile=`echo $PWD | cut -c 2- | tr "/" "_"`
   #tarfile=${tarfile}${rhcyc}.${file}.tar
   res=`echo $tarfile | grep -c sstoiqd`
   if [ $res -eq 0 ]
   then
      tarfile=${tarfile}${rhcyc}.tar
   else
      tarfile=${tarfile}${rhcyc}.tar
   fi

   #
   #   Check if the tarfile index exists.  If it does, assume that
   #   the data for the corresponding directory has already been
   #   tarred and saved.
   #
 
   if [ $TSM_FLAG = 'NO' ]
   then
     hsi "ls -l ${hpssdir}/${tarfile}.idx"
     tar_file_exists=$?
     if [ $tar_file_exists -eq 0 ]
     then
       echo "File $tarfile already saved."
       continue
     fi
   elif [ $TSM_FLAG = 'YES' ]
   then
     size=`ssh ibmtsm1.ncep.noaa.gov ls -l ${rhistdir}/${tarfile} | awk '{print \$5}'`
     if [  -n "$size" ]
     then
       if [ $size -gt 0 ]
       then
          echo "Directory $dir already saved."
          continue
       fi
     fi
   fi

   #   If on Stratus:
   #   htar is used to create the archive, -P creates
   #   the directory path if it does not already exist,
   #   and an index file is also made.
   #

   if [ $TSM_FLAG = 'NO' ]
   then
     date
     htar -P -cvf ${hpssdir}/$tarfile -L ${DATA}/$file
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savesstoiqd.sh:  File $tarfile was not successfully created."
       exit 3
     fi
     date
 
   #
   #   Read the tarfile and save a list of files that are in the tar file.
   #
 
     htar -tvf $hpssdir/$tarfile
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savesstoiqd.sh:  Tar file $tarfile was not successfully read to"
       echo "             generate a list of the files."
       exit 4
     fi
 
   #
   #  Restrict tar file, if it contains restricted data.
   #
       ${USHrhist}/rhist_restrict.sh ${hpssdir}/$tarfile
 
   #
   #  send to HSM
   #
   elif [ $TSM_FLAG = 'YES' ]
   then

   #
   #   Tar up the directory and put the tarred file in the 
   #   appropriate directory in ${HPSSOUT}.
   #  
   
     date
     gtar -cvf ${DATA}/$tarfile -T ${DATA}/$file
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savesstoiqd.sh:  File $tarfile was not successfully created."
       exit 3
     fi
 
     $SCP $SCP_CONFIG ${DATA}/${tarfile} ibmtsm1.ncep.noaa.gov:${rhistdir}/${tarfile}
     date
   fi

rm  ${DATA}/$file
   
done

exit 0

