#!/bin/sh
################################################################3
#
#  This script will tar up all the files in the directory specified by the first
#  argument ($1) that contain the date specified for the second argument ($2)
#  and place the tar file on the HPSS server
#  under ${HPSSOUT}.  The tar file is put in the directory
#  appropriate for data valid for the day specified as the second 
#  command line argument ($2).
#  The third argument, if present, indicates whether the tar file 
#  should be written to the 1-year, 2-year, or permanent archive.
#  Valid values for the 3rd argument are "1", "2" or "perm"
#
#  Usage: rhist_savearkvc2.sh Directory Date(YYYYMMDD format) [1|2|perm]
#
#  Where: Directory  = Directory to be tarred.
#         Date(YYYYMMDD format) = Day that the tar file should be saved under.
#
################################################################3
set -x

if [ $# -ne 2 ] && [ $# -ne 3 ]
then
  echo "Usage: rhist_savearkvc2.sh Directory Date(YYYYMMDD format) [1|2|perm]"
  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_savearkvc2.sh:  Directory $dir does not exist."
  exit 2
fi 

#
#   cd to the directory to be tarred.
# 

cd $dir

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

if [ $TSM_FLAG = 'NO' ]
then
  if [ $# -eq 3 ]
  then
      if [ $3 = "1" ]
      then
           hpssdir=${HPSSOUT}/1year/rh${year}/${yearmo}/$2
      elif [ $3 = "2" ]
      then
           hpssdir=${HPSSOUT}/2year/rh${year}/${yearmo}/$2
      else
           hpssdir=${HPSSOUT}/rh${year}/${yearmo}/$2
      fi
  else
      hpssdir=${HPSSOUT}/rh${year}/${yearmo}/$2
  fi
elif [ $TSM_FLAG = 'YES' ]
then
  if [ $# -eq 3 ]
  then
      if [ $3 = "1" ]
      then
           rhistdir=${TSMOUT}/1year/rh${year}/${yearmo}/$2
      elif [ $3 = "2" ]
      then
           rhistdir=${TSMOUT}/2year/rh${year}/${yearmo}/$2
      else
           rhistdir=${TSMOUT}/rh${year}/${yearmo}/$2
      fi
  else
      rhistdir=${TSMOUT}/rh${year}/${yearmo}/$2
  fi
fi

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

if [ $TSM_FLAG = 'NO' ]
then
  hsi mkdir -p -m 755 $hpssdir
  hsi "ls -l ${hpssdir}/${tarfile}.idx"
  tar_file_exists=$?
  if [ $tar_file_exists -eq 0 ]
   then
   echo "Directory $dir already saved."
   exit 0
  fi
elif [ $TSM_FLAG = 'YES' ]
then
  ssh ibmtsm1.ncep.noaa.gov "mkdir -p -m 755 $rhistdir"
  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."
       exit 0
    fi
  fi
fi

#
#   Tar up the directory and put the tarred file in the 
#   appropriate directory in ${HPSSOUT}.
#  

date
ls -1 | grep ${2} | awk '{print "./"$0}'> $DATA/InputList
tar -cvf ${DATA}/$tarfile -L $DATA/InputList
err=$?
if [ $err -ne 0 ]
then
  echo "savearkvc2.sh:  Directory $dir was not successfully tarred."
  exit 3
fi
date


#     Send tar file to archive

if [ $TSM_FLAG = 'NO' ]
then

  hsi << EOF
  put ${DATA}/${tarfile} : ${hpssdir}/${tarfile}
  chmod 755 ${hpssdir}/${tarfile}
EOF

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

elif [ $TSM_FLAG = 'YES' ]
then
   gtar -cvf ${DATA}/$tarfile -T $DATA/InputList
   $SCP $SCP_CONFIG ${DATA}/${tarfile} ibmtsm1.ncep.noaa.gov:${rhistdir}/${tarfile}
   ssh ibmtsm1.ncep.noaa.gov "chmod 755 ${rhistdir}/${tarfile}"
fi

rm ${DATA}/$tarfile
