#!/bin/sh

ERESSEA_DB=memory
pkg-config --exists sqlite3 && ERESSEA_DB=sqlite

GETOPT=getopt
GETOPT_LONG=1

if [ "Darwin" = "$(uname)" ] ; then
  if [ -x "/usr/local/opt/gnu-getopt/bin/getopt" ] ; then
    GETOPT="/usr/local/opt/gnu-getopt/bin/getopt"
  else
    GETOPT_LONG=0
  fi
fi

if [ $GETOPT_LONG -eq 1 ]; then
  options=$(${GETOPT} -o d: -l db: -- "$@")
else # assume GNU getopt (long arguments)
  options=$(${GETOPT} d: "$@")
fi

# Parse command line arguments
eval set -- "$options"
until [ -z "$1" ] ; do
  case $1 in
    -d|--db)
      ERESSEA_DB=$2
      shift
    ;;
    --) shift; break;;
    (-*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
    (*) break;;
  esac
  shift
done
while [ ! -z "$1" ] ; do
if [ "$1" = "--with-db" ] ; then
ERESSEA_DB=db
elif [ "$1" = "--with-sqlite" ] ; then
ERESSEA_DB=sqlite
elif [ "$1" = "--with-memory" ] ; then
ERESSEA_DB=memory
fi
shift 1
done

ROOT=$(git rev-parse --show-toplevel)
[ -z $BUILD ] && BUILD=Debug
[ -z "$CC" ] && [ ! -z `which clang` ] && CC="clang"
[ -z "$CC" ] && [ ! -z `which gcc` ] && CC="gcc"
[ -z "$CC" ] && [ ! -z `which tcc` ] && CC="tcc"
[ -z "$CC" ] && [ ! -z `which cc` ] && CC="cc"

MACHINE=`$CC -dumpmachine`
[ -z $MACHINE ] && MACHINE=`uname -m`
BIN_DIR="$ROOT/build-$MACHINE-$CC-$BUILD"
rm -rf $BIN_DIR
mkdir -p $BIN_DIR
rm -f $BUILD
ln -sf $BIN_DIR $BUILD

# use anything installed in /opt or /usr
LIBRARY_PATH=/opt/lib:/opt/lib/$MACHINE:/usr/lib/$MACHINE
INCLUDE_PATH=/opt/include:/usr/include
PREFIX_PATH=/opt

# I like to put stuff in ~/usr if I don't have permission to install packages on the machine:
if [ -d $HOME/usr ]; then
  LIBRARY_PATH=$HOME/usr/lib:$HOME/usr/lib/$MACHINE:$LIBRARY_PATH
  INCLUDE_PATH=$HOME/usr/include:$HOME/usr/include/$MACHINE:$INCLUDE_PATH
  PREFIX_PATH=$HOME/usr:$HOME/usr/local:$PREFIX_PATH
fi

DEST=$(dirname $ROOT)/server

git submodule update --init

LUA_VERSION="5.2"
LUA_INCLUDE=/usr/include
LUA_DIR=/usr
if [ -d /usr/include/lua5.1 ]; then
   LUA_VERSION="5.1"
   LUA_INCLUDE=/usr/include/lua5.1
elif [ -d /usr/include/lua5.2 ]; then
   export LUA_DIR=/usr
   LUA_VERSION="5.2"
   LUA_INCLUDE=/usr/include/lua5.2
elif [ -d /usr/local/include/lua5.1 ]; then
   export LUA_DIR=/usr/local
   LUA_VERSION="5.1"
   LUA_INCLUDE=/usr/local/include/lua5.1
fi

if [ ! -e ${LUA_INCLUDE}/lua.h ]; then
  echo "no compatible version of lua is installed in $LUA_INCLUDE."
  exit 1
fi

cat >| $BUILD/config.cmake <<HEREDOC
SET (ERESSEA_DB "$ERESSEA_DB" CACHE STRING "Database driver")
SET (LUA_DIR "$LUA_DIR" CACHE PATH "Lua root path")
SET (CMAKE_BUILD_TYPE "$BUILD" CACHE STRING "")
SET (CMAKE_INSTALL_PREFIX "$DEST" CACHE PATH "")
SET (CMAKE_LIBRARY_PATH "$LIBRARY_PATH" CACHE PATH "")
SET (CMAKE_PREFIX_PATH "$PREFIX_PATH" CACHE PATH "")
HEREDOC

path="$(which tolua)"
if [ "$HAVE_TOLUA" = "0" ] || [ -z $path ] ; then
 echo "tolua is not installed, building from source"
 cd $ROOT
 if [ ! -d tolua/include ]; then
  echo "fetching tolua ${LUA_VERSION} from github..."
  git clone https://github.com/ennorehling/tolua-${LUA_VERSION}.git tolua
 fi
 echo "building tolua..."
 cd tolua
 make
 cd -
cat >> $BUILD/config.cmake <<TOLUA
SET(PC_TOLUA_DIR "$ROOT/tolua" CACHE PATH "tolua root")
TOLUA

else
 echo "tolua is $path"
fi
unset path

set -e

cd $BIN_DIR
cmake -C config.cmake .. $*
cd $OLDPWD