#!/bin/bash

## Find the R home directory.
: ${R_HOME=`R RHOME`}
if test -z "${R_HOME}"; then
  echo "Could not determine R_HOME."
  exit 1
fi

R="${R_HOME}/bin/R"

SYMPHONY_CPPFLAGS=`pkg-config --cflags SYMPHONY >/dev/null 2>&1`
SYMPHONY_LIBS=`pkg-config --libs SYMPHONY >/dev/null 2>&1`

test -z "${SYMPHONY_LIBS}" && SYMPHONY_LIBS="-lSym -lCgl -lOsiClp -lClp -lOsi -lCoinUtils"

## Test whether we can compile and link a minimal program.
rm -f conftest.*

cat << EOF > conftest.cc 
#include <coin/symphony.h>
extern "C"
int
main ()
{
    sym_environment *env = sym_open_environment();
    sym_close_environment(env);
    return 0;
}
EOF

_R_SHLIB_BUILD_OBJECTS_SYMBOL_TABLES_=false 
${R} CMD SHLIB conftest.cc ${SYMPHONY_CPPFLAGS} ${SYMPHONY_LIBS} >/dev/null 2>&1
status=${?}

rm -f conftest.*

if test ${status} -eq 0; then
    SYMPHONY_INCLUDE_PATH=
    SYMPHONY_LIB_PATH=
    SYMPHONY_TS=
else
	# if Mac OS X need to specify C compiler

	if [[ "$OSTYPE" == "darwin"* ]]; then 
	
	echo "Running on Darwin"

		cat << EOF > test.c
		#include <omp.h>
		#include <stdio.h>

		int main() {
			printf("Hey");
			return 0;
		}
EOF

    # first test the default gcc used by R
    `R CMD config CC` -lgomp test.c -o test >/dev/null 2>&1
		if [[ $? -eq 0 ]]; then
			echo -e "* Using the default compiler"
			CC=`"${R_HOME}/bin/R" CMD config CC`
			CXX=`"${R_HOME}/bin/R" CMD config CXX`
			OMP_FLAG="--enable-openmp"
		# if there is no brew gcc installed try to compile with clang
		else
		  echo -e "* Defaulting to clang/clang++"
		  CC=`"${R_HOME}/bin/R" CMD config CC`
			CXX=`"${R_HOME}/bin/R" CMD config CXX`
			if [ -z "$OMP_FLAG" ]; then
			    OMP_FLAG="--disable-openmp"
			fi  
		fi

    echo "CXX is ${CXX}"
    CXX="`echo ${CXX} | sed -e s/++[0-9][0-9]/++14/g`"
    echo "CXX is ${CXX}"

    rm -f test.*
	  (cd src/SYMPHONY && \
		  ./configure \
			CC="$CC" CXX="$CXX" \
			CFLAGS="-w -g -O2" \
			CXXFLAGS="-w -g -O2" \
		  --enable-static --disable-shared --with-pic \
		  --with-application=no --disable-dependency-tracking \
		  --disable-zlib --disable-bzlib "${OMP_FLAG}" \
		  --disable-cplex-libcheck --disable-glpk-libcheck \
		  --disable-osl-libcheck --disable-soplex-libcheck \
		  --disable-xpress-libcheck)
	    SYMPHONY_LIBS="-lSym -lCgl -lOsiClp -lClp -lOsi -lCoinUtils"
	    SYMPHONY_INCLUDE_PATH="-ISYMPHONY/include"
	    SYMPHONY_LIB_PATH="-LSYMPHONY/lib"
	    SYMPHONY_TS="SYMPHONY.ts"

  # for other (Unix) systems use
  else
  
  	echo "Running on Linux"
  
    CC="`${R_HOME}/bin/R CMD config CC`"
    CXX="`${R_HOME}/bin/R CMD config CXX`"
    F77="`${R_HOME}/bin/R CMD config FC`"
    FLIBS="`${R_HOME}/bin/R CMD config FLIBS`"
  
	    (cd src/SYMPHONY && \
		./configure \
		--enable-static --disable-shared --with-pic \
		--with-application=no --disable-dependency-tracking \
		--disable-zlib --disable-bzlib \
		--disable-cplex-libcheck --disable-glpk-libcheck \
		--disable-osl-libcheck --disable-soplex-libcheck \
		--disable-xpress-libcheck \
		CFLAGS="-w -g -O2" \
		CXXFLAGS="-w -g -O2" \
		CC="${CC}" \
		CXX="${CXX}" \
		F77="${F77}" \
		FLIBS="${FLIBS}")
	    SYMPHONY_LIBS="-lSym -lCgl -lOsiClp -lClp -lOsi -lCoinUtils"
	    SYMPHONY_INCLUDE_PATH="-ISYMPHONY/include"
	    SYMPHONY_LIB_PATH="-LSYMPHONY/lib"
	    SYMPHONY_TS="SYMPHONY.ts"
	fi
fi

sed -e "s|@SYMPHONY_LIBS@|${SYMPHONY_LIBS}|" \
    -e "s|@SYMPHONY_INCLUDE_PATH@|${SYMPHONY_INCLUDE_PATH}|" \
    -e "s|@SYMPHONY_LIB_PATH@|${SYMPHONY_LIB_PATH}|" \
    -e "s|@SYMPHONY_TS@|${SYMPHONY_TS}|" \
    src/Makevars.in > src/Makevars
