#!/bin/sh
################################################################3
#
#  This script will tar up the directory specified by the first
#  argument ($1) 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).
#
#  Usage: rhist_savedcom.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 ] 
then
  echo "Usage: rhist_savedcom.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_savedcom.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 "_".
#   Generate list of files to be archived
#

if [ $PDYm2 -eq $2 ]
then
   tarfile=`echo $PWD | cut -c 2- | tr "/" "_"`
   tarfile_bkp=${tarfile}.bkp.tar
   tarfile=${tarfile}.tar
elif [ $PDYm4 -eq $2 ]
#altimeter ssha observations have a greater latency
then
   tarfile=`echo $PWD | cut -c 2- | tr "/" "_"`
   tarfile_bkp=${tarfile}_PDYm4.bkp.tar
   tarfile=${tarfile}_PDYm4.tar
   cd $DATA
   ls $dir/b031/xx104 >filelist
   ls $dir/b031/xx109 >>filelist
   ls $dir/b031/xx113 >>filelist
fi


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

if [ $TSM_FLAG = 'NO' ]
then
   #dcom always goes to permanent archive
   hpssdir=${HPSSOUT}/rh${year}/${yearmo}/$2
   hpssdir_obs=${HPSSOUT_OBS}/rh${year}/${yearmo}/$2

# Check to see if tar file index already exists

  hsi "ls -l ${hpssdir}/${tarfile}.idx"
  tar_file_exists=$?
  if [ $tar_file_exists -eq 0 ]
  then
    echo "File  $tarfile already saved."
    exit 0
  fi
# Check to see if backup copy of obs data exists
  hsi "ls -l ${hpssdir_obs}/${tarfile_bkp}.idx"
  tar_file_exists=$?
  if [ $tar_file_exists -eq 0 ]
  then
    echo "File  $tarfile_bkp already saved."
    exit 0
  fi
elif [ $TSM_FLAG = 'YES' ]
then
  rhistdir=${TSMOUT}/rh${year}/${yearmo}/$2

  ssh ibmtsm1.ncep.noaa.gov "mkdir -p -m 755 $rhistdir"
  #
  #   Check if the tarfile already exists.  If it does, assume that
  #   the data for the corresponding directory has already been
  #   tarred and saved.
  #
  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

#
#   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
  if [ $PDYm2 -eq $2 ]
  then
     htar -P -cvf ${hpssdir}/$tarfile .
     htar -P -cvf ${hpssdir_obs}/$tarfile_bkp .
  elif [ $PDYm4 -eq $2 ]
  then
     htar -P -cvf ${hpssdir}/$tarfile -L ${DATA}/filelist
     htar -P -cvf ${hpssdir_obs}/$tarfile_bkp -L ${DATA}/filelist
  fi
  err=$?
  if [ $err -eq 72 ]
  then
     #ignore error for dcom since file size could change during runtime
     err=0
  fi

  if [ $err -ne 0 ]
  then
    echo "rhist_savedcom.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_savedcom.sh:  Tar file $tarfile was not successfully read to"
    echo "             generate a list of the files."
    exit 4
  fi

  htar -tvf $hpssdir_obs/$tarfile_bkp
  err=$?
  if [ $err -ne 0 ]
  then
    echo "rhist_savedcom.sh:  Tar file $tarfile_bkp 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
    ${USHrhist}/rhist_restrict.sh ${hpssdir_obs}/$tarfile_bkp
 
#
#  send to TSM
#
elif [ $TSM_FLAG = 'YES' ]
then
  #
  #   Tar up the directory and put the tarred file in the 
  #   appropriate directory in ${TSMOUT}.
  #  

  date
  gtar -cvf ${DATA}/$tarfile .
  err=$?
  if [ $err -ne 0 ]
  then
    echo "savedir.sh:  Directory $dir was not successfully tarred."
    exit 3
  fi 
  $SCP $SCP_CONFIG ${DATA}/$tarfile ibmtsm1.ncep.noaa.gov:$rhistdir/$tarfile
  date
fi

