#!/bin/bash
# --------------------------------------------------------------------------- #
# sort_all_switches:  Run sort_switch on known switch files.                  #
#                                                                             #
# use        : sort_all_switches [options] switch_file                        #
#              See usage function for options.                                #
#                                                                             #
# error codes : Program ends if error occurs in sort_switch.                  #
#                                                                             #
# remarks:                                                                    #
#   * Use all_switches to update data in 1.a.6.                               #
#                                                                             #
#                                                      Hendrik L. Tolman      #
#                                                      November 2013          #
#                                                                             #
#    Copyright 2013 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.                               #
#                                                                             #
# --------------------------------------------------------------------------- #


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

  set -e
  home_dir=`pwd`

# 1.a.2 Usage function

  scriptname="`basename $0`"
  optstr="hu"

  usage ()
{
cat 2>&1 << EOF

Usage: $scriptname [options]
Options:
  -h               : help, print this.
  -u               : allow updating (adding) switches,
                     otherwise this gives an error exit
EOF
}
 
# 1.a.3 Process input (part 1)

  args=`getopt $optstr $*`

  if [ $? != 0 ]
  then
    usage
    exit 1
  fi

  set -- $args

  while :
  do
    case "$1" in
    -h) help=1 ;;
    -u) update=1 ;;
    --) break ;;
    esac
    shift
  done
  shift

  if [ $help ]
  then
    usage
    exit 1
  fi

# 1.a.4 Get data from setup file - - - - - - - - - - - - - - - - - - - - - - - - 

  source $(dirname $0)/w3_setenv
  main_dir=$WWATCH3_DIR
  temp_dir=$WWATCH3_TMP
  source=$WWATCH3_SOURCE
  list=$WWATCH3_LIST

# 1.a.5 Other parameters

  options='-s -r'

  if [ "$update" ] ; then
    options="$options -u" ; fi

  i_count='0'

# 1.b ID header  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  echo ' '
  echo '            ********************************************'
  echo '          *** sorting most WAVEWATCH III switch files ***'
  echo '            *******************************************'
  echo ' '
  echo " Main directory      : $main_dir"
  echo " sort_switch options : $options"
  echo ' '

# --------------------------------------------------------------------------- #
# 2. Switches in bin directory                                                #
# --------------------------------------------------------------------------- #

  echo ' '
  echo " WAVEWATCH III bin directory ..."
  echo " -------------------------------"
  cd $main_dir/bin

  for file in switch*
  do
    echo "   processing $file ..."
    ./sort_switch $options $file
    if [ "$?" != '0' ] ; then
      exit 1 ; fi
    i_count=$(($i_count + 1))
  done

# --------------------------------------------------------------------------- #
# 3. Switches in regtests directory                                           #
# --------------------------------------------------------------------------- #

  echo ' '
  echo " WAVEWATCH III regtests directory ..."
  echo " ------------------------------------"
  cd $main_dir/regtests

  for dir1 in `ls`
  do
    if [ -d $dir1 ] && [ "$dir1" != 'bin' ]
    then
      echo "   Directory $dir1 ..."
      cd $dir1
      for dir2 in `ls -d input*`
      do
        echo "     Directory $dir2 ..."
        cd $dir2
        for file in switch*
        do
          echo "       processing $file ..."
          ./sort_switch $options $file
          if [ "$?" != '0' ] ; then
            exit 1 ; fi
          i_count=$(($i_count + 1))
        done
        cd ..
      done
      cd ..
    fi
  done

# --------------------------------------------------------------------------- #
# x. End of program ID / clean up                                             #
# --------------------------------------------------------------------------- #

# cd $home_dir
# rm -rf $temp_dir

  echo ' '
  echo " A total of $i_count switch files processed."
  echo ' '
  echo ' '
  echo '                     *************************'
  echo '                   *** end of switch sorting ***'
  echo '                     *************************'
  echo ' '

# End of sort_all_switches -------------------------------------------------- #
