#!/bin/sh
# --------------------------------------------------------------------------- #
# comp  : Compiler script for use in ad3 (customized for hardware and         #
#         optimization). Note that this script will not be replaced if part   #
#         of WAVEWATCH III is re-installed. Used by ad3.                      #
#                                                                             #
# use   : comp name                                                           #
#           name: name of source code file without the extension.             #
#                                                                             #
# error codes :  1 : input error                                              #
#                2 : no environment file $ww3_env found.                      #
#                3 : error in creating scratch directory.                     #
#                4 : w3adc error.                                             #
#                5 : compiler error.                                          #
#                                                                             #
# remarks :                                                                   #
#                                                                             #
#  - This script runs from the scratch directory, where it should remain.     #
#                                                                             #
#  - For this script to interact with ad3, it needs to generate / leave       #
#    following files :                                                        #
#       $name.f90   : Source code (generated by ad3).                         #
#       $name.o     : Object module.                                          #
#       $name.l     : Listing file.                                           #
#       comp.stat   : status file of compiler, containing number of errors    #
#                     and number of warnings (generated by comp).             #
#                                                                             #
#  - Upon (first) installation of WAVEWATCH III the user needs to check the   #
#    following parts of this script :                                         #
#      sec. 2.b : Provide correct compiler/options.                           #
#      sec. 3.a : Provide correct error capturing.                            #
#      sec. 3.d : Remove unnecessary files.                                   #
#                                                                             #
#                                                      Hendrik L. Tolman      #
#                                                      May 2009               #
#                                                                             #
#    Copyright 2009 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 Check and process input

  if [ "$#" != '1' ]
  then
    echo "usage: comp name" ; exit 1
  fi
  name="$1"

# 1.b Initial clean-up - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

  rm -f $name.l
  rm -f $name.o
  rm -f comp.stat

# --------------------------------------------------------------------------- #
# 2. Compile                                                                  #
# --------------------------------------------------------------------------- #
# Add here the correct compiler call including command line options
# Note: - do not invoke a link step
#       - if possible, generate a listing $name.l
#       - make sure the compiler point to the proper directory where the 
#         modules are stored ($m_path), see examples below.

# 2.a Determine file extension - - - - - - - - - - - - - - - - - - - - - - - - 
#     .f90 assumes free format, .f assumes fixed format, change if necessary
# *** file extension (fext) is set and exported by calling program (ad3) ***

# 2.b Perform compilation  - - - - - - - - - - - - - - - - - - - - - - - - - - 
#     Save compiler exit code in $OK

# Generic --------------------------------------------------------------------
# f90 -c $name.$fext                                 > $name.out 2> $name.err
# OK="$?"

# Portland Group Inc FORTRAN on Redhad Linux  --------------------------------
# 2.b.1 Build options and determine compiler name

# opt="-c -Mlist -fast -fastsse -module $path_m -byteswapio"

## if [ "$mpi_mod" = 'yes' ]
## then
## # comp=mpif90
##   comp=${FC}
## else
#   comp=pgf90
## fi

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

# if [ "$netcdf_compile" = 'yes' ]
# then
#   case $WWATCH3_NETCDF in
#     NC3) opt="$opt -I$NETCDF_INCDIR" ;;
#     NC4) if [ "$mpi_mod" = 'no' ]; then comp="`$NETCDF_CONFIG --fc`"; fi
#          opt="$opt -I`$NETCDF_CONFIG --includedir`" ;;
#   esac
# fi

# opt="$opt -I$path_i"

# 2.b.2 Compile

# $comp $opt                             $name.$fext > $name.out 2> $name.err
# OK="$?"

# 2.b.2 Process listing

# if [ -s $name.lst ] 
# then
#    mv $name.lst $name.l
# fi

# 2.b.3 Add test output to listing for later viewing

# if [ -s $name.l ] 
# then
#   echo '------------' >> $name.l
#   echo "$comp $opt"   >> $name.l
#   echo '------------' >> $name.l
#   cat $name.out       >> $name.l 2> /dev/null
#   echo '------------' >> $name.l
#   cat $name.err       >> $name.l 2> /dev/null
#   echo '------------' >> $name.l
# fi

# IBM RS6000 SP with xlf -----------------------------------------------------
# 2.b.1 Build options and determine compiler name
#       This compiler expects the .f extension
#       My compiler had problems optimizing xnlserv.f90

# if [ -f $name.f90 ]
# then
#   mv $name.f90 $name.f
# fi
# fext='f'

# opt="-c -qsource -qarch=pwr3 -I$path_m -qnosave"
# opt="$opt ${FFLAGS}"

# if [ "$name" = 'serv_xnl4v3' ]
# then
#   opt="$opt -O2"
# else
#   opt="$opt -O3"
# fi

# vamp=' -I/usrx/local/vampirtrace/include'    # Vampire Trace includes
# vamp=

# debug=' -g -C -qcheck -qfltrap:enable -qsigtrap'
# debug=

# opt="$opt$vamp$debug"

# comp=xlf90

# if [ "$mpi_mod" = 'yes' ]
# then
#   comp=mpxlf90
# fi

# if [ "$omp_mod" = 'yes' ]
# then
#   comp=xlf90_r
#   opt="$opt -qsmp=noauto"
# fi

# if [ "$name" = 'ww3_ounf' ] || [ "$name" = 'ww3_ounp' ] || [ "$name" = 'ww3_prnc' ]
# then
#   case $WWATCH3_NETCDF in
#     NC3) opt="$opt -I$NETCDF_INCDIR" ;;
#     NC4) if [ "$mpi_mod" = 'no' ]; then comp="`$NETCDF_CONFIG --fc`"; fi
#          opt="$opt -I`$NETCDF_CONFIG --includedir`" ;;
#   esac
# fi

# 2.b.2 Compile

# $comp $opt                             $name.$fext > $name.out 2> $name.err
# OK="$?"

# 2.b.2 Process listing
#       For this compiler i need to get error information from the listing

# if [ -s $name.lst ] 
# then
#    mv $name.lst $name.l
#    sed -n '/^TOTAL   UNR/,/^$/p' $name.l | head -3 > $name.err2
# fi

# 2.b.3 Add test output to listing for later viewing

# if [ -s $name.l ] 
# then
#   echo '------------' >> $name.l
#   echo "$comp $opt"   >> $name.l
#   echo '------------' >> $name.l
#   cat $name.out       >> $name.l 2> /dev/null
#   echo '------------' >> $name.l
#   cat $name.err       >> $name.l 2> /dev/null
#   echo '------------' >> $name.l
# fi

# --------------------------------------------------------------------------- #
# 3. Postprocessing                                                           #
# --------------------------------------------------------------------------- #
# 3.a Capture errors
#     nr_err : number of errors.
#     nr_war : number of errors.

  nr_err='0'
  nr_war='0'

  if [ -s $name.err ]
  then
    echo > /dev/null

# Portland Group

#   grep inform $name.err | grep warnings | grep severes > $name.err2
#   mv $name.err2 $name.err
#   err_lines=`wc -l $name.err | awk '{ print $1 }'`

#   nr_inf=0
#   nr_war=0
#   nr_sev=0
#   nr_fat=0

#   line=1
#   while [ "$line" -le "$err_lines" ]
#   do
#     nr_loc=`sed -n "$line,${line}p" $name.err | awk '{ print $1}'`
#     nr_inf=`expr $nr_inf + $nr_loc`
#     nr_loc=`sed -n "$line,${line}p" $name.err | awk '{ print $3}'`
#     nr_war=`expr $nr_war + $nr_loc`
#     nr_loc=`sed -n "$line,${line}p" $name.err | awk '{ print $5}'`
#     nr_sev=`expr $nr_sev + $nr_loc`
#     nr_loc=`sed -n "$line,${line}p" $name.err | awk '{ print $7}'`
#     nr_fat=`expr $nr_fat + $nr_loc`
#     line=`expr $line + 1`
#   done

#   nr_err=`expr $nr_sev + $nr_fat`
#   nr_war=`expr $nr_war + $nr_inf`

# IBM xlf

# if [ -s $name.err2 ]
# then

#   nr_tot2="`tail -1 $name.err2 | awk '{ print $1}'`"
#   nr_unr2="`tail -1 $name.err2 | awk '{ print $2}'`"
#   nr_sev2="`tail -1 $name.err2 | awk '{ print $3}'`"
#   nr_err2="`tail -1 $name.err2 | awk '{ print $4}'`"
#   nr_war2="`tail -1 $name.err2 | awk '{ print $5}'`"
#   nr_inf2="`tail -1 $name.err2 | awk '{ print $6}'`"

#   nr_err=`expr $nr_unr2 + $nr_sev2`
#   nr_war=`expr $nr_err2 + $nr_war2`
# else
#   OK=1
# fi

# rm -f $name.err2

# End hardware / software dep. -------------

  else
    if [ "$OK" != '0' ]
    then
      nr_err='1'
    fi
  fi

# 3.b Make file comp.stat  - - - - - - - - - - - - - - - - - - - - - - - - - - 

  echo "ERROR    $nr_err"   > comp.stat
  echo "WARNING  $nr_war"  >> comp.stat

# 3.c Prepare listing  - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#     if compiler does not provide listing, make listing from source code
#     and compiler messages. Second input line for w3list identifies if
#     comment lines are to be numbered.

  if [ ! -f $name.l ]
  then
    echo "$name.$fext" > w3list.inp
    echo "T"          >> w3list.inp
    w3list < w3list.inp 2> /dev/null
    rm -f w3list.inp
    mv w3list.out $name.l
    echo '------------' >> $name.l
    echo "$comp $opt"   >> $name.l
    echo '------------' >> $name.l
    cat $name.out >> $name.l #2> /dev/null
    echo '------------' >> $name.l
    cat $name.err >> $name.l #2> /dev/null
    echo '------------' >> $name.l
  fi

# 3.d Remove unwanted files  - - - - - - - - - - - - - - - - - - - - - - - - -
#     include here unwanted files generated by the compiler

  rm -f $name.out
  rm -f $name.err

# end of comp --------------------------------------------------------------- #