#!/bin/sh
#  Based on configure of Rblpapi
#  https://github.com/Rblp/Rblpapi/blob/master/configure

## Check that we are on Unix
which uname
if [ $? -ne 0 ]; then
    echo "You do not have 'uname' so this is unlikely to be a Unix system. Exiting."
    exit 1
fi

# Library settings
PKG_TEST_HEADER="<kiwi/Kiwi.h>"
PKG_LIBS="-lkiwi"
PKG_CPPFLAGS=""
LIB_VER="0.11.2"

# Copy libcache for local development.
if [ -f "`pwd`/kiwilibtmp/libs/libkiwi.a" ]; then
  echo "Found kiwilibtmp folder!"
  mkdir -p kiwilibs
  cp -rf kiwilibtmp/* kiwilibs/
fi

# Check for custom locations
if [ "$INCLUDE_DIR" ] || [ "$LIB_DIR" ]; then
  echo "Found INCLUDE_DIR and/or LIB_DIR!"
  PKG_CPPFLAGS="-I$INCLUDE_DIR $PKG_CPPFLAGS"
  PKG_LIBS="-L$LIB_DIR $PKG_LIBS"

elif [ -d "`pwd`/kiwilibs/libs/" ]; then
  echo "Found libkiwi on pwd installation..."
  PKG_CPPFLAGS="-I`pwd`/kiwilibs/include $PKG_CPPFLAGS"
  PKG_LIBS="-L`pwd`/kiwilibs/libs $PKG_LIBS"

else
  echo "Prior system libkiwi installation not found"
  echo "Preparing to download and build library from source..."

  which git
  if [ $? -ne 0 ]; then
    echo "------------------------------[ ELBIRD ]------------------------------"
    echo "Configuration failed because 'git' was not found."
    echo "If you want to kiwi build from source in package installation prosess,"
    echo "make sure git and cmake work in system."
    echo "-------------------------------------------------------------------------"
    exit 1
  fi

  which cmake
  if [ $? -ne 0 ]; then
    export PATH=$PATH:/Applications/CMake.app/Contents/bin
  fi

  which cmake
  if [ $? -ne 0 ]; then
    echo "------------------------------[ ELBIRD ]------------------------------"
    echo "Configuration failed because 'libkiwi' and 'cmake' were not found."
    echo "If 'libkiwi' is already installed but in a non-standard location, you"
    echo "may set INCLUDE_DIR and LIB_DIR manually via:"
    echo "R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'"
    echo ""
    echo "If you want to kiwi build from source in package installation prosess,"
    echo "make sure cmake work."
    echo "-------------------------------------------------------------------------"
    exit 1
  fi

  git config --global advice.detachedHead false
  git clone -b v$LIB_VER --depth 1 https://github.com/bab2min/Kiwi
  cd Kiwi

  git rm third_party/googletest
  git rm third_party/cpuinfo
  git submodule update --init --recursive

  mkdir build
  cd build
  cmake -DCMAKE_BUILD_TYPE=Release -DKIWI_BUILD_TEST=OFF -DKIWI_USE_CPUINFO=OFF -DCMAKE_POSITION_INDEPENDENT_CODE=ON --log-level=ERROR ..
  echo "** make is running..."
  make kiwi_static 1> makelog.txt
  echo "** done!"
  cd ../..

  mkdir -p kiwilibs/libs
  mv -f Kiwi/build/libkiwi_static.a kiwilibs/libs/libkiwi.a
  mv -f Kiwi/include kiwilibs/include
  rm -rf Kiwi

  # Copy libcache for local development.
  cp -rf kiwilibs/ kiwilibtmp/
  PKG_CPPFLAGS="-I`pwd`/kiwilibs/include $PKG_CPPFLAGS"
  PKG_LIBS="-L`pwd`/kiwilibs/libs $PKG_LIBS"
fi

# For debugging
echo "Using PKG_CPPFLAGS=$PKG_CPPFLAGS"
echo "Using PKG_LIBS=$PKG_LIBS"

# Find compiler
CXX11=`${R_HOME}/bin/R CMD config CXX11`
CXXFLAGS=`${R_HOME}/bin/R CMD config CXXFLAGS`

# Test for libkiwi
echo "#include $PKG_TEST_HEADER" | ${CXX11} ${PKG_CPPFLAGS} ${CXXFLAGS} -E -xc++ - >/dev/null 2>&1

if [ $? -ne 0 ]; then
    echo "------------------------------[ ELBIRD ]------------------------------"
    echo "Configuration failed because 'libkiwi' was not found."
    echo "If 'libkiwi' is already installed but in a non-standard location, you"
    echo "may set INCLUDE_DIR and LIB_DIR manually via:"
    echo "R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'"
    echo "-------------------------------------------------------------------------"
    exit 1
fi

# Write to Makevars
sed -e "s|@cppflags@|$PKG_CPPFLAGS|" -e "s|@libs@|$PKG_LIBS|" -e "s|@cxxflags@|$CXXFLAGS|" src/Makevars.in > src/Makevars

# Success
exit 0

