#!/bin/bash -e
# --------------------------------------------------------------------------- #
# 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="hcmr"  #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
  -h : print usage and exit

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 ;;
    -h) help=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 ID header  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  echo ' '
  echo '                *****************************'
  echo '              ***   WAVEWATCH III clean     ***'
  echo '                *****************************'
  echo ' '



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

  source $(dirname $0)/w3_setenv
  # source again in case of updated wwatch3.env
  source $(dirname $0)/w3_setenv 
  main_dir=$WWATCH3_DIR
  temp_dir=$WWATCH3_TMP
  source=$WWATCH3_SOURCE
  list=$WWATCH3_LIST

# --------------------------------------------------------------------------- #
# 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 *.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 *.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 [ -d $main_dir ]
  then
    if [ -e $main_dir/nuopc.mk ]
    then
      rm -f $main_dir/nuopc.mk
    fi
    # clean up bin
    if [ -d $main_dir/bin ]
    then
      echo "   Clean up $main_dir/bin"
      cd $main_dir/bin
      rm -f switch.old switch.HOLD switch.hold
      for type in DIST SHRD OMP HYB MPI SEQ
      do
        rm -f switch.old_${type} switch.${type}
      done
    fi
    # clean up ftn & aux
    for folder in ftn aux
    do
      if [ -d $main_dir/$folder ]
      then
        echo "   Clean up $main_dir/$folder"
        rm -f $main_dir/$folder/makefile
        for type in DIST SHRD OMP HYB MPI SEQ
        do
          rm -f $main_dir/$folder/makefile_${type}
        done
      fi
    done
    # clean up work & mod & obj & tmp
    for folder in work mod obj tmp
    do
      if [ -d $main_dir/$folder ] || [ -L $main_dir/$folder ]
      then
        echo "   Remove $main_dir/$folder"
        rm -fr $main_dir/$folder
      fi
      for type in DIST SHRD OMP HYB MPI SEQ
      do
        if [ -d $main_dir/${folder}_${type} ]
        then
          echo "   Remove $main_dir/${folder}_${type}"
          rm -fr $main_dir/${folder}_${type}
        fi
      done
   done

  else
    echo "   Directory does not exist: $main_dir"
  fi

# --------------------------------------------------------------------------- #
# 5. Setup settings                                                           #
# --------------------------------------------------------------------------- #

  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
        rm -f comp link switch
      fi
    # clean up exe
    for folder in exe
    do
      if [ -d $main_dir/$folder ] || [ -L $main_dir/$folder ]
      then
        echo "   Remove $main_dir/$folder"
        rm -fr $main_dir/$folder
      fi
      for type in DIST SHRD OMP HYB MPI SEQ
      do
        if [ -d $main_dir/${folder}_${type} ]
        then
          echo "   Remove $main_dir/${folder}_${type}"
          rm -fr $main_dir/${folder}_${type}
        fi
      done
   done

    else
      echo "   Directory does not exist: $main_dir"
    fi
  fi

# --------------------------------------------------------------------------- #
# 6. 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/cmplr
          rm -f input/toy/toy_model
          rm -f input/toy/*__*.f90
          rm -f input/toy/*__*.mod
          rm -f input/toy/*.o
          rm -f input/ww3_shel.inp
          rm -f input/ww3_shel.nml
        fi
      fi
    fi
  fi

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