#!/bin/sh
# --------------------------------------------------------------------------- #
# make_OMP    : Script to manage consecutive compiling of OpenMP and non-     #
#               OpenMP elements of WAVEWATCH III, using standard compile      #
#               scripts. Excluse OpenMP without MPI.                          #
#                                                                             #
#                                                      Hendrik L. Tolman      #
#                                                      Nov. 2013              #
#                                                      April 2014             #
#                                                                             #
#    Copyright 2013-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 Internal variables

  tmp='ww3_temp'           # temporary working directory

# 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 file

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

  clear
  echo ' '
  echo '                ***************************************'
  echo '              ***   WAVEWATCH III compile for OpenMP  ***'
  echo '                ***************************************'
  echo ' '

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

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

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

  if test -d $main_dir
  then
    cd $main_dir
  else
    echo "*** Directory $main_dir not found ***"
    exit
  fi

  if test -d bin
  then
    cd bin
  else
    echo "*** Directory $main_dir/bin not found ***"
    exit
  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"
  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.shrd
  sed 's/SHRD/SHRD OMPG OMPX/g'  switch.shrd > switch.OMP

# --------------------------------------------------------------------------- #
# 3. Compile non-OpenMP codes                                                 #
# --------------------------------------------------------------------------- #

  cp switch.shrd switch
  w3_make ww3_grid ww3_strt ww3_prep ww3_outf ww3_outp ww3_trck ww3_grib \
          ww3_gspl ww3_gint gx_outf gx_outp ww3_systrk ww3_bound \
          $ww3_NetCDF

# --------------------------------------------------------------------------- #
# 3. Compile OpenMP codes                                                     #
# --------------------------------------------------------------------------- #

  cp switch.OMP switch
  w3_make ww3_shel ww3_multi ww3_sbs1

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

  cp switch.hold switch
# cp switch.shrd switch

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

  rm -f switch.hold switch.shrd switch.OMP
  w3_clean

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

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