#!/bin/sh
################################################################3
#
#  This script will tar up all the data for a given forecast day for
#  the directory specified by the first
#  argument ($1) and place the tar file on the HPSS server,
#  under /hpssprod/runhistory.  The tar file is put in the directory
#  appropriate for data valid for the day specified as the second 
#  command line argument ($2).
#
#  This script saves all vsdb precip files for a specific day into
#  a single tar file.
#
#  Usage: rhist_savevsdbprecip.sh Directory Date(YYYYMMDDHH format)
#
#  Where: Directory  = Directory to be tarred.
#         Date(YYYYMMDD format) = Day that the tar file should be saved under.
#
################################################################3
set -x


if [ $# -ne 2 ]
then
  echo "Usage: rhist_savevsdbprecip.sh Directory Date(YYYYMMDD 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_savevsdbprecip.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`

if [ $SUBMIT_HOST = 's' ]
then
  hpssdir0=/hpssprod/runhistory/rh${year}/${yearmo}/$yrmoday
  hpssdir1=/hpssprod/runhistory/1year/rh${year}/${yearmo}/$yrmoday
  hpssdir2=/hpssprod/runhistory/2year/rh${year}/${yearmo}/$yrmoday

elif [ $SUBMIT_HOST = 'c' ]
then
  rhistdir0=/hsmprod/runhistory/rh${year}/${yearmo}/$yrmoday
  rhistdir1=/hsmprod/runhistory/1year/rh${year}/${yearmo}/$yrmoday
  rhistdir2=/hsmprod/runhistory/2year/rh${year}/${yearmo}/$yrmoday
                                                                                                   
  mkdir -p -m 755 $rhistdir0; mkdir -p -m 755 $rhistdir1; mkdir -p -m 755 $rhistdir2
fi


#
#   Get a listing of all files in the directory to be tarred
#   and break the file list up into a group of files.
#   The list of files names the contents of its associated tar file.
#   Then cd to the directory to be tarred.
# 

cd $dir

for model in cmc cmcglb dgex dwd eastarw eastnmm ecmwf gec00 gfs jma medley \
nam ndas ndassoil ngm srarwctl srarwn1 srarwp1 srarwn2 srarwp2 sreect sreen1 sreep1 \
srerct sreen1 srern2 srerp1 srerp2 srkect srken2 srkep2 \
srmean srnmmctl srnmmn1 srnmmp1 srnmmn2 srnmmp2 st2 st4 ukmo westarw westnmm
do
  ls -1 ${model}/${model}_${yrmoday}.vsdb >> ${DATA}/input_list
done

#  Now create a tar file for the files

for file in vsdb_precip
do

   # 
   #   Pick 1year, 2year, or permanent archive.
   #
   case $file in
      vsdb_precip)   hpssdir=$hpssdir0
                rhistdir=$rhistdir0
                cos_id=131;;
      *)        hpssdir=$hpssdir0
                rhistdir=$rhistdir0;;
   esac

   #
   #   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}.${yrmoday}.tar

   #
   #   Check if the tarfile index exists.  If it does, assume that
   #   the data for the corresponding directory has already been
   #   tarred and saved.
   #

   if [ $SUBMIT_HOST = 's' ]
   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 [ $SUBMIT_HOST = 'c' ]
   then
     size=`ls -l ${rhistdir}/${tarfile} | awk '{ print \$5}'`
     if [  -n "$size" ]
     then
       if [ $size -gt 0 ]
       then
          echo "File $tarfile 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 [ $SUBMIT_HOST = 's' ]
   then
     date
     htar -P -cvf ${hpssdir}/$tarfile -L ${DATA}/input_list -Y $cos_id
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savevsdbprecip.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_savevsdbprecip.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
 
   #
   #  If on Cirrus send to HSM
   #

   elif [ $SUBMIT_HOST = 'c' ]
   then

   #
   #   Tar up the directory and put the tarred file in the 
   #   appropriate directory in /hsmprod/runhistory.
   #  
   
     date
     tar -cvf ${rhistdir}/$tarfile -L ${DATA}/input_list
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savevsdbprecip.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.
   #
                                                                          
     tar -tvf $rhistdir/$tarfile
     err=$?
     if [ $err -ne 0 ]
     then
       echo "rhist_savevsdbprecip.sh:  Tar file $tarfile was not successfully read to"
       echo "                      generate a list of the files."
       exit 4
     fi
   fi             
   #rm ${DATA}/$file
   
done

