#!/bin/sh
set +xa
###############################################################################
#                                                                             #
# This script tars the sectral or bulleting 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.                                                         #
#                                                                             #
# 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 spec_$loc which is      #
#   removed if this script exits normally.                                    #
# - See section 0.c for variables that need to be set.                        #
#                                                                             #
#                                                            April 27, 1999   #
#                                                                             #
###############################################################################
#
# --------------------------------------------------------------------------- #
# 0.  Preparations
# 0.a Basic modes of operation
#
  cd $DATA
  postmsg "$jlogfile" "Making TAR FILE"
#
  rm -rf tar_$1
  mkdir tar_$1
#
# this directory is used only for error capturing
#
  echo ' '
  echo '+--------------------------------+'
  echo '!         Make tar file          |'
  echo '+--------------------------------+'
  echo "   Model ID        : $modIE"
  echo "   Type            : $1"
#
# 0.b Check if type set
#
  if [ "$#" -lt '1' ]
  then
    echo ' '
    echo '**********************************'
    echo '*** TYPE IN wavetarens.sh NOT SET ***'
    echo '**********************************'
    echo ' '
    postmsg "$jlogfile" "TYPE IN wavetarens.sh NOT SET"
    exit 1
  else
    type=$1
  fi
#
# 0.c Define directories and the search path.
#     The tested variables should be exported by the postprocessor script.
#
  if [ -z "$cycle" ] || [ -z "$COMOUT" ] || [ -z "$modIE" ] ||  \
     [ -z "$SENDCOM" ]
  then
    echo ' '
    echo '************************************************'
    echo '*** EXPORTED VARIABLES IN wavetarens.sh NOT SET ***'
    echo '************************************************'
    echo ' '
    postmsg "$jlogfile" "EXPORTED VARIABLES IN wavetarens.sh NOT SET"
    exit 2
  fi
#
# --------------------------------------------------------------------------- #
# 2.  Generate tar file
#
  echo ' '
  echo '   Making tar file ...'
#
  tar -cf $modIE.$cycle.${type}_tar ./$modIE.*.$type
  exit=$?
#
  if  [ "$exit" != '0' ]
  then
    echo ' '
    echo '***************************************** '
    echo '*** FATAL ERROR : TAR CREATION FAILED *** '
    echo '***************************************** '
    echo ' '
    postmsg "$jlogfile" "FATAL ERROR : TAR CREATION FAILED"
    exit 3
  fi
#
# --------------------------------------------------------------------------- #
# 3.  Move data to /com
#
  if [ "$SENDCOM" = 'YES' ]
  then
    echo ' '
    echo "   Moving tar file to $COMOUT ..."
#
    cp $modIE.$cycle.${type}_tar $COMOUT
#
    exit=$?
#
    if  [ "$exit" != '0' ]
    then
      echo ' '
      echo '************************************* '
      echo '*** FATAL ERROR : TAR COPY FAILED *** '
      echo '************************************* '
      echo ' '
      postmsg "$jlogfile" "FATAL ERROR : TAR COPY FAILED"
      exit 4
    fi
  fi
#
# --------------------------------------------------------------------------- #
# 4.  Final clean up
#
  rm -rf tar_$type
#
  echo ' '
  echo 'End of wavetarens.sh at'
  date
#
# End of wavetarens.sh ------------------------------------------------------ #
