#!/bin/sh
###############################################################################
#                                                                             #
# This script tars the sectral or bulletin files into a single file and       #
# puts it into /com. This is a separate script to enable it to be run in      #
# parallel using poe. It also tars the spectral and bulletin files of the     $
$ old grids that are generated for backward compatibility                     #
#                                                                             #
# Remarks :                                                                   #
# - Shell script variables controling time, directories etc. are set in the   #
#   mother script.                                                            #
# - This script runs in the work directory designated in the mother script.   #
#   Under this directory it geneates a work directory TAR_$type_$ID which is  #
#   removed if this script exits normally.                                    #
# - See section 0.c for variables that need to be set.                        #
#                                                                             #
#                                                            March 13, 2007   #
# Last update : 03-15-2007                                                    #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation

  cd $DATA
  seton='+xa'
  setoff='+xa'
  set $seton
  postmsg "$jlogfile" "Making TAR FILE"


  set $setoff
  echo ' '
  echo '+--------------------------------+'
  echo '!         Make tar file          |'
  echo '+--------------------------------+'
  echo "   ID              : $1"
  echo "   Type            : $2"
  set $seton


# 0.b Check if type set

  if [ "$#" -lt '2' ]
  then
    set $setoff
    echo ' '
    echo '***************************************'
    echo '*** TYPE IN multiwavetar.sh NOT SET ***'
    echo '***************************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" "TYPE IN multiwavetar.sh NOT SET"
    exit 1
  else
    ID=$1
    type=$2
  fi

  rm -rf TAR_${type}_$ID 
  mkdir  TAR_${type}_$ID
# this directory is used only for error capturing

# 0.c Define directories and the search path.
#     The tested variables should be exported by the postprocessor script.

  if [ -z "$cycle" ] || [ -z "$COMOUT" ] || [ -z "$modID" ] ||  \
     [ -z "$SENDCOM" ] || [ -z "$SENDDBN" ]
  then
    set $setoff
    echo ' '
    echo '*****************************************************'
    echo '*** EXPORTED VARIABLES IN multiwavetar.sh NOT SET ***'
    echo '*****************************************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" "EXPORTED VARIABLES IN multiwavetar.sh NOT SET"
    exit 2
  fi

# --------------------------------------------------------------------------- #
# 2.  Generate tar file

  set $setoff
  echo ' '
  echo '   Making tar file ...'
  set $seton

  tar -cf $ID.$cycle.${type}_tar ./$ID.*.$type
  exit=$?

  if  [ "$exit" != '0' ]
  then
    set $setoff
    echo ' '
    echo '***************************************** '
    echo '*** FATAL ERROR : TAR CREATION FAILED *** '
    echo '***************************************** '
    echo ' '
    set $seton
    postmsg "$jlogfile" "FATAL ERROR : TAR CREATION FAILED"
    exit 3
  fi

# --------------------------------------------------------------------------- #
# 3.  Move data to /com

  if [ "$SENDCOM" = 'YES' ]
  then
    set $setoff
    echo ' '
    echo "   Moving tar file to $COMOUT ..."
    set $seton

    cp $ID.$cycle.${type}_tar $COMOUT

    if [ "$SENDDBN" = 'YES' ]
    then
      set $setoff
      echo "   Alerting TAR file as $COMOUT/$ID.$cycle.${type}_tar"
      set $seton
        $DBNROOT/bin/dbn_alert MODEL OMBWAVE $job $COMOUT/$ID.$cycle.${type}_tar
    fi

    exit=$?

    if  [ "$exit" != '0' ]
    then
      set $setoff
      echo ' '
      echo '************************************* '
      echo '*** FATAL ERROR : TAR COPY FAILED *** '
      echo '************************************* '
      echo ' '
      set $seton
      postmsg "$jlogfile" "FATAL ERROR : TAR COPY FAILED"
      exit 4
    fi
  fi

# --------------------------------------------------------------------------- #
# 4.  Final clean up

  rm -rf TAR_${type}_$ID
  rm -f  $ID.*.$type

  set $setoff
  echo ' '
  echo 'End of multiwavetar.sh at'
  date

# End of multiwavetar.sh ----------------------------------------------------- #
