#!/bin/bash
#
# Print compiler/linker flags to use ADIOS

#
# Configuration values from configure script
#

FLAGSFILE=`dirname $0`/adios_config.flags
if [ ! -f ${FLAGSFILE} ]; then
    echo "ERROR: settings file ${FLAGSFILE} not found."
    exit 1
fi
. ${FLAGSFILE}

GITSTATFILE=`dirname $0`/git.status
if [ -f ${GITSTATFILE} ]; then
    . ${GITSTATFILE}
fi

function Usage () {
    echo "`basename $0` [-d | -c | -l] [-f] [-r] [-s] [-1] [-v] [-i]
Arguments
   -d   Base directory for ADIOS install
   -c   Compiler flags for C/C++, using ADIOS write/read methods
   -l   Linker flags for C/C++, using ADIOS write/read methods

   -f   Print above flags for Fortran90
   -r   Print above flags for using ADIOS read library only.
   -s   Print above flags for using ADIOS in a sequential code (no MPI). 
   -1   Print above flags for using old Read API of ADIOS.

   -v   Version of the installed package
   -i   More installation information about the package

Notes
   - Multiple options of d,c,l are enabled. In such a case, the output is
     a list of FLAG=flags, where FLAG is one of (DIR, CFLAGS, LDFLAGS)
   - If none of d,c,l are given, all of them is printed
   - If none of f,r,s are given, flags for C/C++, using ADIOS write/read 
     methods are printed
"
}

# default
PRINT_DIR=no
PRINT_CFLAGS=no
PRINT_LDFLAGS=no
OPT_FORTRAN=no
OPT_READ=no
OPT_SEQ=no
NFLAGS_ASKED=0

while getopts ":dclfrs1vih" Option
do          
  case $Option in               
        d) PRINT_DIR=yes; let "NFLAGS_ASKED=NFLAGS_ASKED+1";;
        c) PRINT_CFLAGS=yes; let "NFLAGS_ASKED=NFLAGS_ASKED+1";;
        l) PRINT_LDFLAGS=yes; let "NFLAGS_ASKED=NFLAGS_ASKED+1";;
        f) OPT_FORTRAN=yes;;
        r) OPT_READ=yes;;
        s) OPT_SEQ=yes;;
        1) OPT_V1=yes;;
        v) echo "$VERSIONSTRING"; 
           exit 0;;
        i) echo "ADIOS $VERSIONSTRING"; 
           echo "Installed from source directory: $SRCDIR"; 
           if [ ! -z "$GITLOG" ]; then echo "$GITLOG"; fi 
           if [ ! -z "$GITSTAT" ]; then echo "Git status of source directory:"; echo "$GITSTAT"; fi 
           exit 0;;
        h) Usage; exit 0;;
        *) echo "Invalid option -$Option."; Usage; exit 255;;   # DEFAULT
  esac
done
shift $(($OPTIND - 1))

if [ $NFLAGS_ASKED == 0 ]; then
    NFLAGS_ASKED=3;
    PRINT_DIR=yes
    PRINT_CFLAGS=yes
    PRINT_LDFLAGS=yes
fi

#if [ "$OPT_SEQ" == "yes" ]; then
#    OPT_READ=yes
#fi

# Print requested values
if [ "$PRINT_DIR" == "yes" ]; then
    if [ $NFLAGS_ASKED -gt 1 ]; then
        echo -n "DIR="
    fi
    echo $ADIOS_DIR
fi

if [ "$PRINT_CFLAGS" == "yes" ]; then
    if [ "$OPT_READ" == "yes" ]; then
        if [ "$OPT_SEQ" == "yes" ]; then
            if [ "$OPT_V1" == "yes" ]; then
                CFLAGS="$ADIOSREAD_SEQ_V1_INC"
            else
                CFLAGS="$ADIOSREAD_SEQ_INC"
            fi
        else
            if [ "$OPT_V1" == "yes" ]; then
                CFLAGS="$ADIOSREAD_V1_INC"
            else
                CFLAGS="$ADIOSREAD_INC"
            fi
        fi 
    else
        if [ "$OPT_SEQ" == "yes" ]; then
            CFLAGS="$ADIOS_SEQ_INC"
        else
            CFLAGS="$ADIOS_INC"
        fi 
    fi
    if [ $NFLAGS_ASKED -gt 1 ]; then
        echo -n "CFLAGS="
    fi
    echo $CFLAGS
fi

if [ "$PRINT_LDFLAGS" == "yes" ]; then
    if [ "$OPT_SEQ" == "yes" ]; then
        if [ "$OPT_READ" == "yes" ]; then
            # ADIOSREAD + SEQ
            if [ "$OPT_FORTRAN" == "yes" ]; then
                if [ "$OPT_V1" == "yes" ]; then
                    LDFLAGS="$ADIOSREAD_SEQ_V1_FLIB"
                else
                    LDFLAGS="$ADIOSREAD_SEQ_FLIB"
                fi
            else
                LDFLAGS="$ADIOSREAD_SEQ_CLIB"
            fi
        else  # ADIOS + SEQ
            if [ "$OPT_FORTRAN" == "yes" ]; then
                if [ "$OPT_V1" == "yes" ]; then
                    LDFLAGS="$ADIOS_SEQ_V1_FLIB"
                else
                    LDFLAGS="$ADIOS_SEQ_FLIB"
                fi
            else
                LDFLAGS="$ADIOS_SEQ_CLIB"
            fi
        fi
    elif [ "$OPT_READ" == "yes" ]; then
        # ADIOSREAD + parallel code
        if [ "$OPT_FORTRAN" == "yes" ]; then
            if [ "$OPT_V1" == "yes" ]; then
                LDFLAGS="$ADIOSREAD_V1_FLIB"
            else
                LDFLAGS="$ADIOSREAD_FLIB"
            fi
        else
            LDFLAGS="$ADIOSREAD_CLIB"
        fi
    else 
        # ADIOS + parallel code
        if [ "$OPT_FORTRAN" == "yes" ]; then
            if [ "$OPT_V1" == "yes" ]; then
                LDFLAGS="$ADIOS_V1_FLIB"
            else
                LDFLAGS="$ADIOS_FLIB"
            fi
        else
            LDFLAGS="$ADIOS_CLIB"
        fi
    fi 
    if [ $NFLAGS_ASKED -gt 1 ]; then
        echo -n "LDFLAGS="
    fi
    echo $LDFLAGS
fi




