#! /bin/bash

set -e

UNAME="$(uname)"
if [[ $UNAME == Darwin ]]; then
    OS=osx
else
    if [[ $UNAME != Linux ]]; then
        echo "Warning: could not auto-detect OS! Treat as Linux."
    fi
    OS=linux
fi

cd "$(dirname "$0")"
BASEDIR="$(pwd)"
tar xvzf Osi-0.103.0.tgz

cd Osi-0.103.0

if [[ -d $CPLEX_INCDIR && -f $CPLEX_LIB ]]; then
    CPLEX_CONFIG="--with-cplex-incdir=$CPLEX_INCDIR --with-cplex-lib=$CPLEX_LIB"
fi
if [[ -d $GUROBI_INCDIR && -f $GUROBI_LIB ]]; then
    GUROBI_CONFIG="--with-gurobi-incdir=$GUROBI_INCDIR --with-gurobi-lib=$GUROBI_LIB"
fi

if [[ $OS == osx ]]; then
    ## Use a patched configure script to work around a problem when compiling
    ## the code on Mac OS X. See http://issues.fast-downward.org/issue295.
    cp ../coinutils-configure.patched CoinUtils/configure
    chmod +x CoinUtils/configure
    ./configure CC="gcc -arch i386" CXX="g++ -arch i386" \
        --prefix "$BASEDIR/coin" --without-lapack --enable-static=no $CPLEX_CONFIG $GUROBI_CONFIG
else
    ./configure CC="gcc -m32 -pthread -Wno-long-long" CXX="g++ -m32 -pthread -Wno-long-long" \
        --prefix "$BASEDIR/coin" --without-lapack --enable-static=yes $CPLEX_CONFIG $GUROBI_CONFIG

fi
make
make test
make install
cd ..
rm -r Osi-0.103.0
