#!/bin/bash
#Adopted from
#https://yoda.hepforge.org/trac/browser/bin/yoda-config.in
#Docs from https://cmake.org/cmake/help/v3.2/command/configure_file.html
## 
#The line below assures at least some relocation.
if [ "$(uname)" == "Darwin" ]; then
    # greadlink is only available through 'brew install coreutils'
    if [ ! -z "$(command -v greadlink)" ]; then
        PACKAGE_BIN_DIR=$(dirname $(greadlink -f $0))
    else
        PACKAGE_BIN_DIR=$(dirname $(readlink -f $0))
    fi
else
PACKAGE_BIN_DIR=$(dirname $(readlink -f $0))
fi
PACKAGE_PREFIX_DIR=$(dirname ${PACKAGE_BIN_DIR})

tmp=$(echo $* | grep -E -- '--\<help\>|-\<h\>')
if test $# -eq 0 || test -n "$tmp"; then
    echo "HepMC3-config: configuration tool for the HepMC3 library"
    echo 
    echo "Note: the features and portability of this script are limited. Projects"
    echo "using CMake are encouraged to use HepMC3's CMake utilities instead."
    echo 
    echo "Usage: $( basename $0 ) [--help|-h] | "
    echo "           [--{prefix,libdir,includedir,interfacesdir}] | "
    echo "           [--{cxxflags,ldflags,libs}] | "
    echo "           [--version]"
    echo "Options:"
    echo "  --help | -h   : show this help message"
    echo
    echo "  --prefix        : show the installation prefix"
    echo "  --includedir    : show the path to the directory containing the HepMC3 headers"
    echo "  --libdir        : show the path to the directory containing the HepMC3 libraries"
    echo "  --interfacesdir : show the path to the directory containing the interfaces to HepMC3"
    echo
    echo "  --cflags|--cppflags : returns a '-I' string for insertion into CPPFLAGS or CXXFLAGS"
    echo "  --ldflags|--libs    : returns a '-L/-l' string for insertion into LIBS or LIBADD"
    echo "  --rootIO            : returns a '-L/-l' string for insertion into LIBS or LIBADD with rootIO support"
    echo "  --protobufIO        : returns a '-L/-l' string for insertion into LIBS or LIBADD with protobufIO support"
    echo "  --search            : returns a '-L/-l' string for insertion into LIBS or LIBADD with search support"
    echo "  --static            : returns a string for insertion into LIBS or LIBADD"
    echo
    echo "  --features          : returns the list of enabled features in this HepMC3 build"
    echo "  --version           : returns the HepMC3 release version number"
    exit 0
fi

OUT=""

tmp=$( echo "$*" | grep -E -- '--\<prefix\>')
test -n "$tmp" && OUT="$OUT ${PACKAGE_PREFIX_DIR}"

tmp=$( echo "$*" | grep -E -- '--\<includedir\>')
test -n "$tmp" && OUT="$OUT ${PACKAGE_PREFIX_DIR}/include"

tmp=$( echo "$*" | grep -E -- '--\<libdir\>')
test -n "$tmp" && OUT="$OUT ${PACKAGE_PREFIX_DIR}/lib"

tmp=$( echo "$*" | grep -E -- '--\<interfacesdir\>')
test -n "$tmp" && OUT="$OUT ${PACKAGE_PREFIX_DIR}/share/HepMC3/interfaces"

tmp=$( echo "$*" | grep -E -- '--\<cflags|cppflags|cxxflags\>')
test -n "$tmp" && OUT="$OUT -I${PACKAGE_PREFIX_DIR}/include"


tmp=$( echo "$*" | grep -E -- '--\<static\>')
if test -n "$tmp"; then

    st=$( echo "lib lib_static search search_static interfaces interfaceshepmc2 interfacespythia6 rootIO protobufIO protobufIO6.32.0 protobufIO_static examples doc python" | grep -E -w -- 'lib_static')
    if test -n "$st"; then
        tmp=$( echo "$*" | grep -E -- '--\<ldflags|libs\>')
        OUT="$OUT ${PACKAGE_PREFIX_DIR}/lib/libHepMC3-static.a"
    fi

    st=$( echo "lib lib_static search search_static interfaces interfaceshepmc2 interfacespythia6 rootIO protobufIO protobufIO6.32.0 protobufIO_static examples doc python" | grep -E -w -- 'search_static')
    if test -n "$st"; then
        tmp=$( echo "$*" | grep -E -- '--\<search\>')
        test -n "$tmp" && OUT="$OUT ${PACKAGE_PREFIX_DIR}/lib/libHepMC3search-static.a"
    fi

    st=$( echo "lib lib_static search search_static interfaces interfaceshepmc2 interfacespythia6 rootIO protobufIO protobufIO6.32.0 protobufIO_static examples doc python" | grep -E -w -- 'protobufIO_static')
    if test -n "$st"; then
        tmp=$( echo "$*" | grep -E -- '--\<protobufIO\>')
        test -n "$tmp" && OUT="$OUT ${PACKAGE_PREFIX_DIR}/lib/libHepMC3protobufIO_static.a"
    fi

else

    st=$( echo "lib lib_static search search_static interfaces interfaceshepmc2 interfacespythia6 rootIO protobufIO protobufIO6.32.0 protobufIO_static examples doc python" | grep -E -w -- 'lib')
    if test -n "$st"; then
        tmp=$( echo "$*" | grep -E -- '--\<ldflags|libs\>')
        test -n "$tmp" && OUT="$OUT -L${PACKAGE_PREFIX_DIR}/lib -lHepMC3"
    fi

    st=$( echo "lib lib_static search search_static interfaces interfaceshepmc2 interfacespythia6 rootIO protobufIO protobufIO6.32.0 protobufIO_static examples doc python" | grep -E -w -- 'search')
    if test -n "$st"; then
        tmp=$( echo "$*" | grep -E -- '--\<search\>')
        test -n "$tmp" && OUT="$OUT -L${PACKAGE_PREFIX_DIR}/lib -lHepMC3search"
    fi

    st=$( echo "lib lib_static search search_static interfaces interfaceshepmc2 interfacespythia6 rootIO protobufIO protobufIO6.32.0 protobufIO_static examples doc python" | grep -E -w -- 'protobufIO')
    if test -n "$st"; then
        tmp=$( echo "$*" | grep -E -- '--\<protobufIO\>')
        test -n "$tmp" && OUT="$OUT -L${PACKAGE_PREFIX_DIR}/lib -lHepMC3protobufIO"
    fi

    st=$( echo "lib lib_static search search_static interfaces interfaceshepmc2 interfacespythia6 rootIO protobufIO protobufIO6.32.0 protobufIO_static examples doc python" | grep -E -w -- 'rootIO')
    if test -n "$st"; then
        tmp=$( echo "$*" | grep -E -- '--\<rootIO\>')
        test -n "$tmp" && OUT="$OUT -L${PACKAGE_PREFIX_DIR}/lib -lHepMC3rootIO"
    fi

fi

tmp=$( echo "$*" | grep -E -- '--\<features\>')
test -n "$tmp" && OUT="$OUT lib lib_static search search_static interfaces interfaceshepmc2 interfacespythia6 rootIO protobufIO protobufIO6.32.0 protobufIO_static examples doc python"

tmp=$( echo "$*" | grep -E -- '--\<version\>')
test -n "$tmp" && echo 3.03.01 && exit 0

echo $OUT
