#!/bin/sh

# Anticonf (tm) script by Jeroen Ooms, Jim Hester (2017)
# This script will query 'pkg-config' for the required cflags and ldflags.
# If pkg-config is unavailable or does not find the library, try setting
# INCLUDE_DIR and LIB_DIR manually via e.g:
# R CMD INSTALL --configure-vars='INCLUDE_DIR=/.../include LIB_DIR=/.../lib'

# Library settings
PKG_CONFIG_NAME="odbc"
PKG_DEB_NAME="unixodbc-dev"
PKG_RPM_NAME="unixODBC-devel"
PKG_CSW_NAME="unixodbc_dev"
PKG_PACMAN_NAME="unixodbc"
PKG_BREW_NAME="unixodbc"
PKG_TEST_HEADER="<sql.h>"
PKG_LIBS="${PKG_LIBS:--lodbc}"


# Note that cflags may be empty in case of success
if [ "$INCLUDE_DIR" ] || [ "$LIB_DIR" ]; then
  echo "Found INCLUDE_DIR and/or LIB_DIR!"
  PKG_CFLAGS="-I$INCLUDE_DIR $PKG_CFLAGS"
  PKG_LIBS="-L$LIB_DIR $PKG_LIBS"
else
  # Use odbc_config, iodbc-config if available
  if [ "`command -v odbc_config`" ]; then
    echo "Found odbc_config cflags and libs!"
    PKGCONFIG_CFLAGS=`odbc_config --cflags`
    PKGCONFIG_LIBS=`odbc_config --libs`
  elif [ "`command -v iodbc-config`" ]; then
    echo "Found iodbc-config cflags and libs!"
    PKGCONFIG_CFLAGS=`iodbc-config --cflags`
    PKGCONFIG_LIBS=`iodbc-config --libs`
  elif [ "`command -v pkg-config`" ]; then
    PKGCONFIG_CFLAGS=`pkg-config --cflags $PKG_CONFIG_NAME 2>/dev/null`
    echo "Found pkg-config cflags and libs!"
    PKGCONFIG_LIBS=`pkg-config --libs $PKG_CONFIG_NAME 2>/dev/null`
  fi
  if [ "$PKGCONFIG_CFLAGS" ] || [ "$PKGCONFIG_LIBS" ]; then
    PKG_CFLAGS=${PKGCONFIG_CFLAGS}
    PKG_LIBS=${PKGCONFIG_LIBS}
  elif [ "`uname`" = 'Darwin' ]; then
    if [ "`command -v brew`" ]; then
      BREWDIR=`brew --prefix`
      PKG_CFLAGS="-I$BREWDIR/opt/$PKG_BREW_NAME/include"
      PKG_LIBS="-L$BREWDIR/lib $PKG_LIBS"
    else
      curl -sfL "https://autobrew.github.io/scripts/unixodbc" > autobrew
      . ./autobrew
    fi
  fi
fi

# Find compiler
CXX="`"${R_HOME}"/bin/R CMD config CXX` -E"

CXXFLAGS=`"${R_HOME}"/bin/R CMD config CXXFLAGS`
CPPFLAGS=`"${R_HOME}"/bin/R CMD config CPPFLAGS`

# For debugging
echo "PKG_CFLAGS=$PKG_CFLAGS"
echo "PKG_LIBS=$PKG_LIBS"

# Test for odbc
echo "#include $PKG_TEST_HEADER" | ${CXX} ${PKG_CFLAGS} ${CPPFLAGS} ${CXXFLAGS} -xc++ - > /dev/null
if [ $? -ne 0 ]; then
  echo "------------------------- ANTICONF ERROR ---------------------------"
  echo "Configuration failed because $PKG_CONFIG_NAME was not found. Try installing:"
  echo " * deb: $PKG_DEB_NAME (Debian, Ubuntu, etc)"
  echo " * rpm: $PKG_RPM_NAME (Fedora, CentOS, RHEL)"
  echo " * csw: $PKG_CSW_NAME (Solaris)"
  echo " * pacman: $PKG_PACMAN_NAME (Archlinux, Manjaro, etc)"
  echo " * brew: $PKG_BREW_NAME (Mac OSX)"
  echo "To use a custom $PKG_CONFIG_NAME set INCLUDE_DIR and LIB_DIR and PKG_LIBS manually via:"
  echo "R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=... PKG_LIBS=...'"
  echo "--------------------------------------------------------------------"
  exit 1;
fi

# Write to Makevars
sed -e "s|@PKG_CFLAGS@|$PKG_CFLAGS|" -e "s|@PKG_LIBS@|$PKG_LIBS|" src/Makevars.in > src/Makevars

# Success
exit 0
