#!/bin/bash -e
# --------------------------------------------------------------------------- #
# make_MPI    : Script to manage consecutive compiling of MPI and non-MPI     #
#               elements of WAVEWATCH III, using standard compile scripts.    #
#               Only ww3_shel, ww3_multi, and ww3_sbs1 are compiled for MPI.  #
#                                                                             #
# use     : make_MPI [program [...]]                                          #
#              program: program name of WAVEWATCH III (sub)program.           #
#                                                                             #
#                                                      Hendrik L. Tolman      #
#                                                      April 2010             #
#                                                      March 2014             #
#                                                                             #
#    Copyright 2010-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.                               #
#                                                                             #
# --------------------------------------------------------------------------- #


# --------------------------------------------------------------------------- #
# 1. Preparations                                                             #
# --------------------------------------------------------------------------- #

# 1.a ID header  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  echo ' '
  echo '                ************************************'
  echo '              ***   WAVEWATCH III compile for MPI  ***'
  echo '                ************************************'
  echo ' '


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

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

# 1.d Check / make directories   - - - - - - - - - - - - - - - - - - - - - - -

  # bin dir
  if [ ! -d $main_dir/bin ]
  then
    echo ' '
    echo "[ERROR] Directory $main_dir/bin not found"
    echo '        Please check the content of your directory model'
    exit 1
  fi

  # switch file
  switch_file=$main_dir/bin/switch
  if [ ! -e $switch_file ] || [ ! -r $switch_file ]
  then
    echo ' '
    echo "[ERROR] switch file $switch_file not found"
    echo "        Please run $main_dir/bin/w3_setup <main_dir> -c <comp> -s <switch>"
    exit 1
  fi

  # comp file
  comp_file=$main_dir/bin/comp
  if [ ! -e $comp_file ] || [ ! -r $comp_file ]
  then
    echo ' '
    echo "[ERROR] comp file $comp_file not found"
    echo "        Please run $main_dir/bin/w3_setup <main_dir> -c <comp> -s <switch>"
    exit 1
  fi

  # link file
  link_file=$main_dir/bin/link
  if [ ! -e $link_file ] || [ ! -r $link_file ]
  then
    echo ' '
    echo "[ERROR] link file $comp_file not found"
    echo "        Please run $main_dir/bin/w3_setup <main_dir> -c <comp> -s <switch>"
    exit 1
  fi

# 1.e Check NetCDF setup - - - - - - - - - - - - - - - - - - - - - - - - - - -

  if [ -z "$WWATCH3_NETCDF" ]
  then
    ww3_NetCDF=
  else
    ww3_NetCDF="ww3_prnc ww3_ounf ww3_ounp ww3_bounc ww3_trnc ww3_prtide"
  fi

# --------------------------------------------------------------------------- #
# 2. Process switch files                                                     #
# --------------------------------------------------------------------------- #

  ./sort_switch -s -r switch
  cp switch                             switch.hold
  sed -e 's/DIST/SHRD/g' \
      -e 's/OMPG //g'\
      -e 's/OMPX //g'\
      -e 's/OMPH //g'\
      -e 's/MPI //g'      switch.hold > switch.SEQ
  sed 's/SHRD/DIST MPI/g' switch.SEQ > switch.MPI

# --------------------------------------------------------------------------- #
# 3. Compile non-MPI codes                                                    #
# --------------------------------------------------------------------------- #

  cp switch.SEQ switch
  ./w3_make ww3_grid ww3_strt ww3_prep ww3_outf ww3_outp ww3_trck ww3_grib \
          ww3_uprstr ww3_gspl ww3_gint gx_outf gx_outp ww3_systrk ww3_bound \
          $ww3_NetCDF
  mv ../exe/exec_type ../exe/exec_type__SEQ
  cp -r ../exe ../exe__SEQ
  mv ../tmp ../tmp_SEQ

# --------------------------------------------------------------------------- #
# 3. Compile MPI codes                                                        #
# --------------------------------------------------------------------------- #

  cp switch.MPI switch
  if [ $ESMFMKFILE ]
  then
    ./w3_make ww3_shel ww3_multi ww3_multi_esmf ww3_sbs1
  else
    ./w3_make ww3_shel ww3_multi ww3_sbs1
  fi
  mv ../exe/exec_type ../exe/exec_type__MPI
  mv ../exe__SEQ/* ../exe
  rm -r ../exe__SEQ
  mv ../tmp ../tmp_MPI
  ln -sf tmp_MPI ../tmp

# --------------------------------------------------------------------------- #
# 4. Reset switch file                                                        #
# --------------------------------------------------------------------------- #

  cp switch.hold switch
  cp switch.SEQ ../exe/
  cp switch.MPI ../exe/

# --------------------------------------------------------------------------- #
# 5. Clean up                                                                 #
# --------------------------------------------------------------------------- #

  rm -f switch.hold switch.SEQ switch.MPI

  echo ' '
  echo ' '
  echo '                   *******************************'
  echo '                 ***       End of program        ***'
  echo '                   *******************************'
  echo ' '

# end of script ------------------------------------------------------------- #