#!/bin/bash
# --------------------------------------------------------------------------- #
# link  : Linker script for use in make (customized for hardware and          #
#         optimization. Note that this script will not be replaced if part    #
#         of WAVEWATCH III is re-installed.                                   #
#                                                                             #
# use   : link name [name ... ]                                               #
#           name: name of source code file without the extension.             #
#                 the first name will become the program name.                #
#                                                                             #
# error codes :  all error output directly to screen.                         #
#                                                                             #
# remarks :                                                                   #
#                                                                             #
#  - Upon (first) installation of WAVEWATCH III the user needs to check the   #
#    following parts of this scripts :                                        #
#      sec. 3 : Provide correct link command                                  #
#                                                                             #
#                                                                             #
#  - This version is modified to work with the intel compiler on a mac pro.   #
#    NOTE: -g flag may result in link errors. Monte thinks this is a compiler #
#          error.                                                             #
#                                                                             #
#                                                      Hendrik L. Tolman      #
#                                                      Monte Hanson           #
#                                                      February 2008          #
# --------------------------------------------------------------------------- #
# 1. Preparations                                                             #
# --------------------------------------------------------------------------- #
# 1.a Check and process input

  if [ "$#" -lt '1' ]
  then
    echo "usage: link name [name]" ; exit 1
  fi

  prog=$1
  echo "      Linking $prog"
  input="$*"

# 1.b 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.c Initial clean-up - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

  rm -f $main_dir/exe/$prog

# --------------------------------------------------------------------------- #
# 2. Check objects                                                            #
# --------------------------------------------------------------------------- #

  cd $main_dir/obj
  objects=$NULL
  error='n'
  set $input

  while [ "$#" -gt '0' ]
  do
    file=$1.o
    if [ -f "$file" ]
    then
      objects="$objects $file"
    else
      echo "      *** file $file not found ***"
      error='y'
    fi
    shift
  done
  if [ "$error" = 'y' ]
  then
    echo "*** Missing object files ***"
    exit 3
  fi

# --------------------------------------------------------------------------- #
# 3. Link all things                                                          #
# --------------------------------------------------------------------------- #
# Add here the correct linker call including switches

# 3.a Build options and determine compiler name
#     No GRIB libraries for this one

  # linking options
  libs=""
  opt="-o $prog"

  # mpi implementation
  if [ "$mpi_mod" = 'yes' ]
  then
    comp=mpif90
  else
    comp=ifort
  fi

  # open mpi implementation
  if [ "$omp_mod" = 'yes' ]
  then
    opt="$opt -mp"
  fi

  # oasis coupler archive
  if [ "$prog" = 'ww3_shel' ] || [ "$prog" = 'ww3_multi' ] || [ "$prog" = 'ww3_sbs1' ] || \
     [ "$prog" = 'ww3_prnc' ] || [ "$prog" = 'ww3_prep' ] || [ "$prog" = 'ww3_prtide' ] || \
     [ "$prog" = 'ww3_gspl' ]
  then
    if [ "$oasis_mod" = 'yes' ]
    then
      if [ -z "$(env | grep OASISDIR)" ]
      then
        echo ''
        echo "[ERROR] OASISDIR is not defined"
        exit 1
      fi
      echo "link with oasis"
      libs="$libs $OASISDIR/lib/libpsmile.MPI1.a $OASISDIR/lib/libmct.a $OASISDIR/lib/libmpeu.a $OASISDIR/lib/libscrip.a"
    fi
  fi

  # netcdf library dir
  if [ "$netcdf_compile" = 'yes' ]
  then
    case $WWATCH3_NETCDF in
      NC3) libs="$libs -L$NETCDF_LIBDIR -lnetcdf" ;;
      NC4) if [ "$mpi_mod" = 'no' ]; then comp="`$NETCDF_CONFIG --fc`"; fi
           libs="$libs `$NETCDF_CONFIG --flibs`" ;;
    esac
  fi

  # parmetis library 
  if [ "$prog" = 'ww3_shel' ] || [ "$prog" = 'ww3_multi' ] || [ "$prog" = 'ww3_sbs1' ] 
  then
    if [ "$pdlib_mod" = 'yes' ]
    then
      if [ -z "$(env | grep METIS_PATH)" ]
      then
        echo ''
        echo "[ERROR] METIS_PATH is not defined"
        exit 1
      fi
      echo "link with parmetis" 
      libs="$libs $METIS_PATH/lib/libparmetis.a $METIS_PATH/lib/libmetis.a"
    fi
  fi

  opt="$opt $EXTRA_LINK_OPTIONS"

# 3.b Link

  rprfx="-Wl,-rpath,"
  rpath=
  for path in $libs
  do
    if [ "${path:0:2}" = '-L' ]
    then
      rpath="$rpath ${rprfx}${path//-L/}"
    fi
  done

  $comp $opt $objects $libs $rpath                  > link.out 2> link.err
  OK="$?"

# --------------------------------------------------------------------------- #
# 4. Postprocessing                                                           #
# --------------------------------------------------------------------------- #

  if [ "$OK" != '0' ]
  then
    echo "      *** error in linking ***"
    echo ' '
    cat link.out
    echo ' '
    cat link.err
    echo ' '
    rm -f link.???
    rm -f $prog
    exit $OK
  else
    if [ ! -f $prog ]
    then
      echo "      *** program $prog not found ***"
      echo ' '
      cat link.out
      echo ' '
      cat link.err
      echo ' '
      rm -f link.???
      exit 1
    else
      mv $prog $main_dir/exe/.
      rm -f link.???
    fi
  fi

# end of link --------------------------------------------------------------- #