#!/bin/sh
###############################################################################
#                                                                             #
# This script copies the sectral or bulletin files into the old grid formats  #
# for backward compatibility with the old system. 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 COPY_$type_$grdID       #
#   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" "Copying point information into old grid format"


  set $setoff
  echo ' '
  echo '+--------------------------------+'
  echo '!         Copy files             |'
  echo '+--------------------------------+'
  echo "   Grid            : $1"
  echo "   Type            : $2"
  set $seton

# 0.b Check if type set

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

  rm -rf COPY_${type}_$grdID
  mkdir  COPY_${type}_$grdID
# 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 multiwavecopy.sh NOT SET ***'
    echo '******************************************************'
    echo ' '
    set $seton
    postmsg "$jlogfile" "EXPORTED VARIABLES IN multiwavecopy.sh NOT SET"
    exit 2
  fi

# --------------------------------------------------------------------------- #
# 2.  Copy files

  set $setoff
  echo ' '
  echo '   Copying files ...'
  set $seton
  
  if [ -f buoy_$grdID.loc ]
  then
    buoys="`sed "s/\'//g" buoy_$grdID.loc | awk '{ print $3 }'`"
    for buoy in $buoys
    do
      if [ -f $modID.$buoy.$type ]
      then
        cp $modID.$buoy.$type $grdID.$buoy.$type
      else
        echo ' '
        echo "  Buoy file for $buoy not found !"
        echo ' '
        exit 3
      fi
    done
  else
    set $setoff
    echo ' '
    echo '***************************************** '
    echo '*** FATAL ERROR : BUOY FILE NOT FOUND *** '
    echo '***************************************** '
    echo ' '
    set $seton
    postmsg "$jlogfile" "FATAL ERROR : File Copy FAILED"
    exit 4
  fi

  exit=$?
  if  [ "$exit" != '0' ]
  then
    set $setoff
    echo ' '
    echo '******************************************** '
    echo '*** FATAL ERROR : COPYING PROCESS FAILED *** '
    echo '******************************************** '
    echo ' '
    set $seton
    postmsg "$jlogfile" "FATAL ERROR : COPYING PROCESS FAILED"
    exit 5
  fi

# --------------------------------------------------------------------------- #
# 3.  Clean up

  rm -rf COPY_${type}_$grdID

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

# End of multiwavecopy.sh ---------------------------------------------------- #
