#! /bin/sh
# -----------------------------------------------------------------------------------

    NAME_="sundials-config"
 PURPOSE_="returns required flags for linking to SUNDIALS libraries"
SYNOPSIS_="$NAME_ -m cvode|cvodes|ida|idas|kinsol -t s|p -l c|f [-s libs|cppflags -hv]"
REQUIRES_="standard GNU commands"
 VERSION_="0.1"
    DATE_="2006-07-25"
  AUTHOR_="Radu Serban <radu@llnl.gov>"

# -----------------------------------------------------------------------------------

usage () {

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
    -m cvode|cvodes|ida|idas|kinsol  SUNDIALS module
    -t s|p                           use serial or parallel vectors
    -l c|f                           use C or Fortran
    -s libs|cppflags                 show linking flags or C preprocessor flags. 
                                     (show both if option not given.)
    -h                               usage and options (this help)
    -v                               view this script
Notes:
    '-l f' is not valid for '-m cvodes' or '-m idas'
    '-s cppflags' returns an empty string for '-l f'" 
    exit 1
}


# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# process args
while getopts hvm:t:l:s: options
do
    case $options in
        m) module=$OPTARG ;;
        t) vector=$OPTARG ;;
        l) lang=$OPTARG ;;
        s) show=$OPTARG ;;
        h) usage ;;
        v) more $0; exit 1 ;;
       \?) echo invalid argument, type $NAME_ -h for help; exit 1 ;;
    esac
done
shift $(( $OPTIND - 1 ))

# args check
[[ $module ]] || { echo >&2 the -m option and argument must be specified; exit 1; }
[[ $vector ]] || { echo >&2 the -t option and argument must be specified; exit 1; }
[[ $lang ]]   || { echo >&2 the -l option and argument must be specified; exit 1; }
[[ $show ]]   || { show=both; }

# main

prefix=/Users/user/bionetgen/bng2/Network3;
exec_prefix=${prefix};
includedir=${prefix}/include;
libdir=${exec_prefix}/lib;

abs_includedir=`cd "${includedir}" > /dev/null 2>&1 && pwd`;
abs_libdir=`cd "${libdir}" > /dev/null 2>&1 && pwd`;

if test $abs_includedir != /usr/include ; then
    includes=-I$abs_includedir
fi

libdirs=-L$abs_libdir

case $module in
    cvode) 
        sun_lib="-lsundials_cvode";
        sun_flib="-lsundials_fcvode";
        ;;
    cvodes)
        sun_lib="-lsundials_cvodes";
        sun_flib=;
        ;;
    ida) 
        sun_lib="-lsundials_ida";
        sun_flib="-lsundials_fida";
        ;;
    idas) 
        sun_lib="-lsundials_idas";
        sun_flib=;
        ;;
    kinsol) 
        sun_lib="-lsundials_kinsol";
        sun_flib="-lsundials_fkinsol";
        ;;  
esac

case $vector in
    s)
        nvec_lib="-lsundials_nvecserial";
        nvec_flib="-lsundials_fnvecserial";
        ;;
    p)
        nvec_lib="-lsundials_nvecparallel";
        nvec_flib="-lsundials_fnvecparallel";
        ;;
esac

case $lang in
    c)
        cppflags=$includes;
        libs="$libdirs $sun_lib $nvec_lib -lm ";
        ;;
    f)
        cppflags=;
        if test $module = cvodes ; then
            libs="Fortran interface not available for CVODES";
        else
            libs="$libdirs $sun_flib $sun_lib $nvec_flib $nvec_lib -lm  ";
        fi
        ;;
esac

case $show in
    cppflags)
        echo $cppflags
        ;;
    libs)
        echo $libs
        ;;
    both)
        echo $cppflags
        echo $libs
        ;;
esac

# end script
