#!/bin/ksh
#
# A script to invoke F90-compilation with correct (module) include path.
#
# Usage: odbf90 [flags] Fortran90-file(s) [object(s)] [-glue] [libraries]
#
# Author: Sami Saarinen, ECMWF, 1998-2004
#
#

if [[ $# -lt 1 ]] ; then
  head -6 $0 | tail -5
  exit 1
fi

#-- gprof style profiling on ? Set this externally to "-pg" to enable gprof
ODB_GPROF=${ODB_GPROF:=""}

ODB_ARCH=${ODB_ARCH:="$ARCH"}
test_arch=$(test_arch 2>/dev/null || echo "unknown")

#MPICH_ROOT=${MPICH_ROOT:=/vol/rdx_dir/mpi/suse90/mpich-1.2.7} # for CPU_TYPE=amd64
MPICH_ROOT=${MPICH_ROOT:=/not/available}

#-- See also use_odb/use_odb.sh -scripts whether $LIBNETCDF was already set there
if [[ "$ODB_ARCH" = @(ibm_power*) ]] ; then
  LIBNETCDF=${LIBNETCDF:=/usr/local/lib/netcdf/current/lib/libnetcdf.a}
else
  LIBNETCDF=${LIBNETCDF:=/usr/local/apps/netCDF/current/lib/libnetcdf.a}
fi

if [[ -f "$LIBNETCDF" ]] ; then
  netcdflib="-L$(dirname $LIBNETCDF) -l$(basename $LIBNETCDF | perl -pe 's/^lib//; s/\..*//;')"
else
  netcdflib=""
fi

odbglue="_odb_glue.o"
[[ -f $odbglue ]] || odbglue=""

compile_only=0
for arg in $*
do
  if [[ "$arg" = "-c" ]] ; then
    odbglue=""
    compile_only=1
    break
  fi
done

addargs=""
if [[ $compile_only -eq 0 ]] ; then
  #-- create automatically odb-glue from (possible) -lDBNAME information
  #-- first: merge possible "-l<space>XXX" into "-lXXX"
  newargs=$(echo "$*" | perl -pe 's/-l\s+/-l/g');
  dbnames=""
  for arg in $newargs
  do
    if [[ "$(echo $arg | cut -c1-2)" = "-l" ]] ; then
      db=$(echo $arg | cut -c3-)
      DB=$(echo $db | perl -pe 's/\s+//g; s/\..*$//; s/[_-]//g; tr/a-z/A-Z/')
      if [[ "$db" = "$DB" && "$db" != @(X11*) ]] ; then
        dbnames="$dbnames $db"
        # A big thanks to ttl 27/04/2006 for the idea of including -L$ODB_SRCPATH_<dbname> !!
        typeset testvar=ODB_SRCPATH_${db}
        typeset testvalue=$(eval echo \$$testvar 2>/dev/null || :)
        if [[ "$testvalue" != "" && -d "$testvalue" ]] ; then
          addargs="$addargs-L$testvalue "
        fi
      fi
    fi
  done
  if [[ "$dbnames" != "" ]] ; then
    create_odbglue $dbnames
    odbglue="_odb_glue.o"
  fi
fi

rc=0
args=""
for arg in $*
do 
  if [[ "$arg" = "-glue" ]] ; then
    args="$args$odbglue "
    odbglue=""
  elif [[ "$(echo $arg | cut -c1-2)" = "-l" ]] ; then # insert -L's in-front of the first -l
    args="$args$addargs$arg "
    addargs=""
  elif [[ "$(echo $arg | perl -pe 's/^.*\.(a|so|o|F|F90|f|f90)\b$/$1/')" = @(a|so|o|F90|F|f|f90) ]] ; then
    args="$args$arg $odbglue "
    odbglue=""
  elif [[ "$(echo $arg | perl -pe 's/^.*\.(c)\b$/$1/')" = @(c) ]] ; then
    odbcc -c $arg || rc=1
    arg=$(echo $arg | sed 's/\.c/.o/')
    args="$args$arg $odbglue "
    odbglue=""
  elif [[ "$(echo $arg | perl -pe 's/^.*\.(cc)\b$/$1/')" = @(cc) ]] ; then
    odbc++ -c $arg || rc=1
    arg=$(echo $arg | sed 's/\.cc/.o/')
    args="$args$arg $odbglue "
    odbglue=""
  else
    args="$args$arg "
  fi
done

if [[ $rc -ne 0 ]] ; then
  echo "***Error: There were problem(s) in compiling C/C++ files" >&2
  exit $rc
fi

ODB_LD=${ODB_LD:=$ODB_F90}

if [[ $compile_only -eq 1 ]] ; then
  echo ${ODB_F90} ${ODB_GPROF} $args >&2
  exec ${ODB_F90} ${ODB_GPROF} $args >&2
else
  echo ${ODB_LD} ${ODB_GPROF} -L. $args $odbglue $ODB_LIB >&2
  exec ${ODB_LD} ${ODB_GPROF} -L. $args $odbglue $ODB_LIB >&2
fi

#-- should never end up here
exit 1

