#!/bin/bash
# --------------------------------------------------------------------------- #
#                                                                             #
# Script for cleaning WW-III tests and model.                                 #
#                                                                             #
# --------------------------------------------------------------------------- #

# --------------------------------------------------------------------------- #
# 1. Function definitions                                                     #
# --------------------------------------------------------------------------- #

# 1.a Error message function
errmsg ()
{
  echo "" 2>&1
  while [ $# != 0 ]
  do
    echo "ERROR: $1" 2>&1
    shift
  done
  echo "" 2>&1
}

# 1.b Usage function
myname="`basename $0`"  #name of script
optstr="cmrC:"  #option string for getopt function
usage ()
{
cat 2>&1 << EOF

Usage: $myname [options] source_dir
Required:
  source_dir : path to top-level of WW3 source
Options:
  -c               : "clobber", also clean-up model & regtests directories
  -m               : also clean-up model directories
  -r               : also clean-up regtests directories
  -C coupl         : invoke clean for <coupl> coupled application
                   :   OASIS : OASIS3-mct ww3_shel coupled application
                   :   ESMF  : ESMF ww3_multi coupled application

EOF
}


# --------------------------------------------------------------------------- #
# 2. Preparations                                                             #
# --------------------------------------------------------------------------- #

# 2.a Setup array of command-line arguments
args=`getopt $optstr $*`
if [ $? != 0 ]
then
  usage
  exit 1
fi
set -- $args

# 2.b Process command-line options
coupl=none
while :
do
  case "$1" in
  -c) w3_clean_arg=$1 ;;
  -m) w3_clean_arg=$1 ;;
  -r) w3_clean_arg=$1 ;;
  -C) shift; coupl="$1" ;;
  --) break ;;
  esac
  shift
done
shift #remove the trailing --
if [ $help ]
then
  usage
  exit 1
fi
case $coupl in
  none) ;;
  OASIS) ;;
  ESMF)
    if [ -z "$ESMFMKFILE" ]
    then
      errmsg "ESMFMKFILE must be defined with coupl = $coupl"
      usage
      exit 1
    fi
  ;;
  *) errmsg "coupl = $coupl not supported" ; usage ; exit 1 ;;
esac

# 2.c Get required arguments
if [ ! $# = 0 ]
then
  path_s="$1" ; shift
else
  usage
  exit 1
fi

# 2.d Convert source path from "relative" to "absolute"
if [ ! -d $path_s ]
then
  errmsg "$path_s not found"
  usage
  exit 1
fi
path_s="`cd $path_s 1>/dev/null 2>&1 && pwd`"

# 2.e Paths to source subdirectories
path_e="$path_s/exe"
path_a="$path_s/aux"
path_b="$path_s/bin"
if [ ! -d $path_a ]
then
  errmsg "$path_a not found"
  exit 1
fi
if [ ! -d $path_b ]
then
  errmsg "$path_b not found"
  exit 1
fi

# 2.f Set env file
source $(dirname $0)/../../model/bin/w3_setenv
main_dir=$WWATCH3_DIR
temp_dir=$WWATCH3_TMP
source=$WWATCH3_SOURCE
list=$WWATCH3_LIST

# --------------------------------------------------------------------------- #
# 3. Execute Clean                                                            #
# --------------------------------------------------------------------------- #


if [ -f $path_b/w3_clean ]
then
  if $path_b/w3_clean $w3_clean_arg
  then :
  else
    errmsg "Error occured during w3_clean"
    exit 1
  fi
  if [ $coupl = "ESMF" ]
  then
    if make -C $path_s/esmf clean
    then :
    else
      errmsg "Error occured during WW3 ESMF clean"
      exit 1
    fi
  fi
  if [ $w3_clean_arg = "-c" ]
  then
    \rm -f $WWATCH3_ENV
  fi
else
  errmsg "WW3 clean script $path_b/w3_clean not found"
  exit 1
fi

# --------------------------------------------------------------------------- #
# End of script                                                               #
# --------------------------------------------------------------------------- #

