#!/bin/sh
################################################################3
#
#  This script will tar up the hurricane 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).
#
#  The script first checks for the existence of storm output in the
#  directory by checking for the existence of *atcf* files.
#  If no files were found, then a storm was not run for that cycle.
#
#  Usage: rhist_savehur.sh Directory Date(YYYYMMDD 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_savehur.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_savehur.sh:  Directory $dir does not exist."
  exit 2
fi 

#
#   cd to the directory to be tarred.
# 

cd $dir

#
#   Check for existence of files like *atcf* to determine
#   if there was a run on a storm.   If not, don't archive
#   directory.
#
ls *atcf* |  wc  | read line word byte
if [ $line -eq 0 ]
then
   echo " No storm output was found in $dir."
   exit 0
fi

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

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

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

hpssdir=/hpssprod/runhistory/rh${year}/${yearmo}/$2
tst=""
tst=`hpssls /hpssprod/runhistory | grep rh${year}`
if [ -z "$tst" ]       # directory does not exist
then
   hpssmkdir /hpssprod/runhistory/rh${year}
   hpsschmod /hpssprod/runhistory/rh${year} 775
fi
tst=""
tst=`hpssls /hpssprod/runhistory/rh${year} | grep ${yearmo}`
if [ -z "$tst" ]       # directory does not exist
then
    hpssmkdir /hpssprod/runhistory/rh${year}/${yearmo}
    hpsschmod /hpssprod/runhistory/rh${year}/${yearmo} 775
fi
tst=""
tst=`hpssls /hpssprod/runhistory/rh${year}/${yearmo} | grep $2`
if [ -z "$tst" ]       # directory does not exist
then
    hpssmkdir $hpssdir
    hpsschmod $hpssdir 775
fi

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

size=`hpssls -l ${hpssdir}/${tarfile} | awk '{ print $8}'`
if [  -n "$size" ]
then
  if [ $size -gt 0 ]
  then
     echo "Directory $dir already saved."
     exit 0
  fi
fi

#
#   Tar up the directory and put the tarred file in the 
#   appropriate directory in /hpssprod/runhistory.
#  

date
#htar -cvf $hpssdir/$tarfile "-Y 132:132" .
tar -cvf ${DATA}/$tarfile .
err=$?
if [ $err -ne 0 ]
then
  echo "savehur.sh:  Directory $dir was not successfully tarred."
  exit 3
fi 
date
#     Send tar file to HPSS
cosid=`$utilscript/getcosid.sh ${DATA}/$tarfile`
pftp -v hpsscore 4021 << EOF
bin 
site setcos $cosid
put ${DATA}/${tarfile} ${hpssdir}/${tarfile}
chmod 755 ${hpssdir}/${tarfile}
EOF
#put ${DATA}/${tarfile}.idx ${hpssdir}/${tarfile}.idx
#chmod 775 ${hpssdir}/${tarfile}.idx

#
#   Read the tarfile and save a list of files that are in the tar file.
# 

htar -Xf $hpssdir/$tarfile
htar -tvf $hpssdir/$tarfile
err=$?
if [ $err -ne 0 ]
then
  echo "savehur.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

#hsi "chmod 775 $hpssdir/$tarfile"
#hpsschmod ${hpssdir}/${tarfile} 775
rm ${DATA}/$tarfile
