#!/bin/sh
################################################################3
#
#  This script will tar up all the data for a given forecast cycle for
#  the directory specified by the first
#  argument ($1) and place the tar files 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 specified files from the production godas data 
#  directory into a tar file.
#  The data files are kept as proposed by EMC.
#
#  Usage: rhist_savegodas.sh Directory Date(YYYYMMDDHH format)
#
#  Where: Directory  = Directory to be tarred.
#         Date(YYYYMMDDHH format) = Day that the tar file should be saved under.
#
################################################################3
set -x


if [ $# -ne 2 ]
then
  echo "Usage: rhist_savegodas.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_savegodas.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`
hpssdir0=/hpssprod/runhistory/rh${year}/${yearmo}/$yrmoday
hpssdir1=/hpssprod/runhistory/1year/rh${year}/${yearmo}/$yrmoday
hpssdir2=/hpssprod/runhistory/2year/rh${year}/${yearmo}/$yrmoday

hsi mkdir -p -m 755 $hpssdir2

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

cd $DATA
ls -1 $dir | awk '
            /model_date_info/ { print "./"$0 > "keep" ; next }
            /[.]mom$|[.]noqc$/ { print "./"$0 > "keep" ; next }
            /^where|^restart/ { print "./"$0 > "keep" ; next } '

cd $dir

#  Now create a tar file for each group of files

for file in keep
do

   #
   #   Pick 1year, 2year, or permanent archive.
   #
   case $file in
      keep)      hpssdir=$hpssdir2;;
      *)         hpssdir=$hpssdir0;;
   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}.${file}.tar
   tarfile=${tarfile}.tar

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

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

   #
   #   Tar up the directory and put the tarred file in the 
   #   appropriate directory in /hsmprod/runhistory.
   #  
   
   date
   #htar -cvf $hpssdir/$tarfile -L $DATA/$file
   tar -cvf ${DATA}/$tarfile -L ${DATA}/$file
   err=$?
   if [ $err -ne 0 ]
   then
     echo "rhist_savegodas.sh:  File $tarfile was not successfully created."
     exit 3
   fi 
   date

#SAGT
exit

#     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 "rhist_savegodas.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

   #hpsschmod ${hpssdir}/${tarfile} 775
   rm ${DATA}/${tarfile} ${DATA}/$file
   
done

exit 0
