#!/bin/sh
# --------------------------------------------------------------------------- #
# w3_clean : Clean-up the work, test and scratch directories by removing the  #
#            files generated by WAVEWATCH III and its compilation system.     #
#                                                                             #
# use  : w3_clean [options]                                                   #
#           -c: "clobber", also clean-up model & regtests directories         #
#           -m: also clean-up model directories                               #
#           -r: also clean-up regtests directories                            #
#                                                                             #
#                                                      Hendrik L. Tolman      #
#                                                      Jan. 2014              #
#                                                                             #
#    Copyright 2009-2014 National Weather Service (NWS),                      #
#       National Oceanic and Atmospheric Administration.  All rights          #
#       reserved.  WAVEWATCH III is a trademark of the NWS.                   #
#       No unauthorized use without permission.                               #
#                                                                             #
# --------------------------------------------------------------------------- #
# 0. Process command line arguments                                           #
# --------------------------------------------------------------------------- #
myname="`basename $0`"  #name of script

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

# 0.b Usage function
  optstr="cmr"  #option string for getopt function
  usage ()
  {
cat 2>&1 << EOF

Usage: $myname [options]
Required: none
Options:
  -c : "clobber", also clean-up model & regtests directories
  -m : also clean-up model directories
  -r : also clean-up regtests directories

EOF
  }

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

# 0.d Process command-line options
  while :
  do
    case "$1" in
    -c) cleanup_model=1; cleanup_regtests=1 ;;
    -m) cleanup_model=1 ;;
    -r) cleanup_regtests=1 ;;
    --) break ;;
    esac
    shift
  done
  shift #remove the trailing --
  if [ $help ]
  then
    usage
    exit 1
  fi

# --------------------------------------------------------------------------- #
# 1. Preparations                                                             #
# --------------------------------------------------------------------------- #
# 1.a Internal variables - - - - - - - - - - - - - - - - - - - - - - - - - - - 

# The following line must not be removed: it is a switch for local install
# so that all bin scripts point to the local wwatch3.env
  export ww3_env=$COAWST_WW3_DIR/wwatch3.env
# For manual install (without install_ww3_tar or install_ww3_svn) make sure to
# either use the generic ww3_env or to add your own ww3_env="${my_directory}"

  if [ ${WWATCH3_ENV} ]; then ww3_env="${WWATCH3_ENV}"; fi # alternate setup file

# 1.b Read data from the environment file  - - - - - - - - - - - - - - - - - -

  if test -f $ww3_env
  then
    set `grep WWATCH3_DIR $ww3_env` ; shift
    main_dir="$*"
    set `grep WWATCH3_TMP $ww3_env` ; shift
    temp_dir="$*"
  else
    errmsg "*** Set-up file $ww3_env not found ***"
    exit 2
  fi

# --------------------------------------------------------------------------- #
# 2. Scratch directory                                                        #
# --------------------------------------------------------------------------- #

  if [ -d $temp_dir ]
  then
    cd $temp_dir
    echo "   Clean up $temp_dir"
    rm -f core
    rm -f core.*
    rm -fr coredir.*
    rm -f *.c
    rm -f *.f
    rm -f *.f90
    rm -f *.F90
    rm -f *.l
    rm -f *.o
#   rm -f *.inp
    rm -f *.out
    rm -f *.err
    rm -f *.ps
    rm -f *.eps
    rm -f *.ww3
    rm -f *.mww3
    rm -f mod_def.*
    rm -f out_pnt.*
    rm -f out_grd.*
    rm -f restart*.*
    rm -f partition*.*
    rm -f buoy_log*.*
    rm -f log.*
    rm -f test.*
    rm -f test???.*
    rm -f w3list
    rm -f ww3.????????.*
    rm -f xnl4v5*
    rm -f quad*
    rm -f prf.*.mww3
    rm -f plot.grads*
    rm -f buoy.data
    rm -f fort.*
    rm -f times.*
    rm -f *.grads
    rm -f *.ctl
    rm -f map.gs
    rm -f ww3_mask.*
    rm -rf ww3_gspl.temp
    rm -f ww3.ww3_gspl

    set +e
    files=`ls ww3_multi.* 2> /dev/null`
    set -e
    if [ -n "$files" ]
    then
      for file in $files
      do
        if [ "$file" != 'ww3_multi.inp' ] 
        then
          rm -f $file
        fi
      done
    fi

    # remove tmp folder if empty
    cd $main_dir
    if [ "$(ls -A $temp_dir 2> /dev/null)" = "" ]
    then
      rm -r $temp_dir
    fi
  fi

# --------------------------------------------------------------------------- #
# 3. Work and test directories                                                #
# --------------------------------------------------------------------------- #

  if [ -d $main_dir/work ]
  then
    echo "   Clean up $main_dir/work"
    for dir in $main_dir/work 
    do
      cd $dir
      rm -f core
      rm -f core.*
      rm -fr coredir.*
      rm -f *.f
      rm -f *.f90
      rm -f *.l
      rm -f *.o
#     rm -f *.inp
      rm -f *.out
      rm -f *.err
      rm -f *.ps
      rm -f *.eps
      rm -f *.ww3
      rm -f *.mww3
      rm -f mod_def.*
      rm -f out_pnt.*
      rm -f out_grd.*
      rm -f restart*.*
      rm -f partition*.*
      rm -f buoy_log*.*
      rm -f log.*
      rm -f test.*
      rm -f test???.*
      rm -f w3list
      rm -f ww3.????????.*
      rm -f xnl4v5*
      rm -f quad*
      rm -f prf.*.mww3
      rm -f plot.grads*
      rm -f buoy.data
      rm -f fort.*
      rm -f times.*
      rm -f *.grads
      rm -f *.ctl
      rm -f map.gs
      rm -f ww3_mask.*
      rm -rf ww3_gspl.temp
      rm -f ww3.ww3_gspl

      set +e
      files=`ls ww3_multi.* 2> /dev/null`
      set -e
      if [ -n "$files" ]
      then
        for file in $files
        do
          if [ "$file" != 'ww3_multi.inp' ] 
          then
            rm -f $file
          fi
        done
      fi
    done

    # remove work folder if empty
    cd $main_dir 
    if [ "$(ls -A $main_dir/work 2> /dev/null)" = "" ]
    then
      rm -r $main_dir/work
    fi
  fi

# --------------------------------------------------------------------------- #
# 4. Model directories                                                        #
# --------------------------------------------------------------------------- #

  if [ $cleanup_model ]
  then
    if [ -d $main_dir ]
    then
      if [ -d $main_dir/bin ]
      then
        echo "   Clean up $main_dir/bin"
        cd $main_dir/bin
        rm -f w3adc w3list w3prnt w3split comp link switch switch.old
        rm -f switch.old_DIST switch.old_SHRD switch.old_OMP switch.old_HYB
      fi
      if [ -d $main_dir/auxx ]
      then
        echo "   Clean up $main_dir/auxx"
        cd $main_dir/auxx
        rm -f makefile
        rm -f makefile_DIST
        rm -f makefile_SHRD
        rm -f makefile_OMP
        rm -f makefile_HYB
      fi
      if [ -d $main_dir/ftn ]
      then
        echo "   Clean up $main_dir/ftn"
        cd $main_dir/ftn
        rm -f makefile
        rm -f makefile_DIST
        rm -f makefile_SHRD
        rm -f makefile_OMP
        rm -f makefile_HYB
      fi
      if [ -d $main_dir/exe ]
      then
        echo "   Remove $main_dir/exe"
        rm -fr $main_dir/exe
      fi
      if [ -d $main_dir/obj ] || [ -L $main_dir/obj ]
      then
        echo "   Remove $main_dir/obj"
        rm -fr $main_dir/obj
      fi
      if [ -d $main_dir/obj_DIST ]
      then
        echo "   Remove $main_dir/obj_DIST"
        rm -fr $main_dir/obj_DIST
      fi
      if [ -d $main_dir/obj_SHRD ]
      then
        echo "   Remove $main_dir/obj_SHRD"
        rm -fr $main_dir/obj_SHRD
      fi
      if [ -d $main_dir/obj_OMP ]
      then
        echo "   Remove $main_dir/obj_OMP"
        rm -fr $main_dir/obj_OMP
      fi
      if [ -d $main_dir/obj_HYB ]
      then
        echo "   Remove $main_dir/obj_HYB"
        rm -fr $main_dir/obj_HYB
      fi
      if [ -d $main_dir/mod ] || [ -L $main_dir/mod ]
      then
        echo "   Remove $main_dir/mod"
        rm -fr $main_dir/mod
      fi
      if [ -d $main_dir/mod_DIST ]
      then
        echo "   Remove $main_dir/mod_DIST"
        rm -fr $main_dir/mod_DIST
      fi
      if [ -d $main_dir/mod_SHRD ]
      then
        echo "   Remove $main_dir/mod_SHRD"
        rm -fr $main_dir/mod_SHRD
      fi
      if [ -d $main_dir/mod_OMP ]
      then
        echo "   Remove $main_dir/mod_OMP"
        rm -fr $main_dir/mod_OMP
      fi
      if [ -d $main_dir/mod_HYB ]
      then
        echo "   Remove $main_dir/mod_HYB"
        rm -fr $main_dir/mod_HYB
      fi
    else
      echo "   Directory does not exist: $main_dir"
    fi
  fi

# --------------------------------------------------------------------------- #
# 5. Regtests directories                                                      #
# --------------------------------------------------------------------------- #

  if [ $cleanup_regtests ]
  then
    if [ -d $main_dir ]
    then
      if [ -d $main_dir/../regtests ]
      then
        echo "   Clean up $main_dir/../regtests"
        cd $main_dir/../regtests
        for work in $(find . -type d -name "work*")
        do
          echo "   Remove $work"
          rm -fr $work
        done
        if [ -d $main_dir/../regtests/ww3_tp2.14 ]
        then
          cd $main_dir/../regtests/ww3_tp2.14
          rm -f input/oasis3-mct/util/make_dir/make
          rm -f input/toy/toy_model
          rm -f input/ww3_shel.inp
        fi
      fi
    fi
  fi

# End of w3_clean ----------------------------------------------------------- #
