#!/bin/sh

set -e

src_dir="src"
template="tools/Makevars.in"
output="${src_dir}/Makevars"

gurobi_cppflags=""
gurobi_libs=""
pthread_cflags=""
pthread_cppflags=""
pthread_libs=""
disable_pthread="no"

for arg in "$@"; do
    case "${arg}" in
        --disable-pthread|--disable-pthreads)
            disable_pthread="yes"
            ;;
    esac
done

if [ -n "${GUROBI_HOME}" ]; then
    gurobi_home="${GUROBI_HOME}"
else
    gurobi_home=""
    for dir in /Library/gurobi*/macos_universal2 /opt/gurobi*/linux64 /opt/gurobi*/linux64*; do
        if [ -d "${dir}" ]; then
            gurobi_home="${dir}"
            break
        fi
    done
fi

if [ -n "${gurobi_home}" ] && [ -f "${gurobi_home}/include/gurobi_c.h" ]; then
    gurobi_libfile=""

    for lib in "${gurobi_home}"/lib/libgurobi*.dylib "${gurobi_home}"/lib/libgurobi*.so; do
        case "${lib}" in
            *\**)
                continue
                ;;
            *_light.*)
                continue
                ;;
        esac

        if [ -f "${lib}" ]; then
            gurobi_libfile="${lib}"
            break
        fi
    done

    if [ -n "${gurobi_libfile}" ]; then
        gurobi_libname=`basename "${gurobi_libfile}"`
        gurobi_libname=${gurobi_libname#lib}
        gurobi_libname=${gurobi_libname%.*}
        gurobi_cppflags="-DHAVE_GUROBI -I${gurobi_home}/include"
        gurobi_libs="-L${gurobi_home}/lib -l${gurobi_libname}"
        echo "configure: using Gurobi from ${gurobi_home}"
    fi
fi

if [ "${QCA_DISABLE_PTHREAD}" = "yes" ] || [ "${QCA_DISABLE_PTHREAD}" = "true" ] || [ "${QCA_DISABLE_PTHREAD}" = "1" ]; then
    disable_pthread="yes"
fi

if [ -n "${R_HOME}" ] && [ -x "${R_HOME}/bin/R" ]; then
    R_BIN="${R_HOME}/bin/R"
else
    R_BIN="R"
fi

CC=`"${R_BIN}" CMD config CC`
CFLAGS=`"${R_BIN}" CMD config CFLAGS`
CPPFLAGS=`"${R_BIN}" CMD config CPPFLAGS`
LDFLAGS=`"${R_BIN}" CMD config LDFLAGS`

if [ "${disable_pthread}" = "yes" ]; then
    echo "configure: pthread support disabled"
else
cat > conftest.c <<'EOF'
#include <pthread.h>
static void *worker(void *arg) { return arg; }
int main(void) {
    pthread_t thread;
    if (pthread_create(&thread, 0, worker, 0) != 0) return 1;
    if (pthread_join(thread, 0) != 0) return 1;
    return 0;
}
EOF

if ${CC} ${CPPFLAGS} ${CFLAGS} ${LDFLAGS} conftest.c -o conftest >/dev/null 2>&1; then
    pthread_cppflags=" -DHAVE_PTHREAD"
    echo "configure: pthread support enabled"
elif ${CC} ${CPPFLAGS} ${CFLAGS} -pthread ${LDFLAGS} conftest.c -o conftest -pthread >/dev/null 2>&1; then
    pthread_cflags=" -pthread"
    pthread_cppflags=" -DHAVE_PTHREAD"
    pthread_libs=" -pthread"
    echo "configure: pthread support enabled with -pthread"
else
    echo "configure: pthread support not available"
fi

rm -rf conftest conftest.c conftest.o conftest.dSYM
fi

sed \
    -e "s|@QCA_GUROBI_CPPFLAGS@|${gurobi_cppflags}|" \
    -e "s|@QCA_GUROBI_LIBS@|${gurobi_libs}|" \
    -e "s|@QCA_PTHREAD_CFLAGS@|${pthread_cflags}|" \
    -e "s|@QCA_PTHREAD_CPPFLAGS@|${pthread_cppflags}|" \
    -e "s|@QCA_PTHREAD_LIBS@|${pthread_libs}|" \
    "${template}" > "${output}"
