# Anticonf (tm) script by Jeroen Ooms (2023)
# 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="openssl"
PKG_DEB_NAME="libssl-dev"
PKG_RPM_NAME="openssl-devel"
PKG_CSW_NAME="libssl_dev"
PKG_BREW_NAME="openssl"
PKG_TEST_FILE="tools/version.c"
PKG_LIBS="-lssl -lcrypto"
PKG_CFLAGS=""

# Build against a specific openssl version
# export PKG_CONFIG_PATH="$(brew --prefix openssl@4)/lib/pkgconfig"

# Use pkg-config if available
pkg-config ${PKG_CONFIG_NAME} ${MINVERSION} 2>/dev/null
if [ $? -eq 0 ]; then
  PKGCONFIG_CFLAGS=`pkg-config --cflags ${PKG_CONFIG_NAME}`
  PKGCONFIG_LIBS=`pkg-config --libs ${PKG_CONFIG_NAME}`
fi

# 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"
elif [ "$PKGCONFIG_CFLAGS" ] || [ "$PKGCONFIG_LIBS" ]; then
  echo "Found pkg-config cflags and libs!"
  PKG_CFLAGS=${PKGCONFIG_CFLAGS}
  PKG_LIBS=${PKGCONFIG_LIBS}
elif [ `uname` = "Darwin" ]; then
  test ! "$CI" && brew --version 2>/dev/null
  if [ $? -eq 0 ]; then
    BREWDIR=`brew --prefix`
    PKG_CFLAGS="-I$BREWDIR/opt/openssl/include"
    PKG_LIBS="-L$BREWDIR/opt/openssl/lib $PKG_LIBS"
  else
    curl -sfL "https://autobrew.github.io/scripts/$PKG_BREW_NAME" > autobrew
    . ./autobrew
  fi
fi

# Find compiler
CC=`${R_HOME}/bin/R CMD config CC`
CFLAGS=`${R_HOME}/bin/R CMD config CFLAGS`
CPPFLAGS=`${R_HOME}/bin/R CMD config CPPFLAGS`

# For debugging
echo "Using PKG_CFLAGS=$PKG_CFLAGS"

# Test configuration
${CC} ${CPPFLAGS} ${PKG_CFLAGS} ${CFLAGS} -E ${PKG_TEST_FILE} >/dev/null 2>configure.log

# Customize the error
if [ $? -ne 0 ]; then
  echo "--------------------------- [ANTICONF] --------------------------------"
  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 " * brew: $PKG_BREW_NAME (Mac OSX)"
  echo "If $PKG_CONFIG_NAME is already installed, check that 'pkg-config' is in your"
  echo "PATH and PKG_CONFIG_PATH contains a $PKG_CONFIG_NAME.pc file. If pkg-config"
  echo "is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:"
  echo "R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'"
  echo "-------------------------- [ERROR MESSAGE] ---------------------------"
  cat configure.log
  echo "--------------------------------------------------------------------"
  exit 1
fi

# Try to link against the correct OpenSSL version
if [ -z "$AUTOBREW" ]; then
SONAME=`${CC} -E ${PKG_CFLAGS} src/tests/soname.h | grep 'echo' | sh | xargs`
if [ "$SONAME" ]; then
if [ `uname` = "Darwin" ]; then
  PKG_LIBS_VERSIONED=`echo "${PKG_LIBS}" | sed "s/-lssl/-lssl.${SONAME}/" | sed "s/-lcrypto/-lcrypto.${SONAME}/"`
else
  PKG_LIBS_VERSIONED=`echo "${PKG_LIBS}" | sed "s/-lssl/-l:libssl.so.${SONAME}/" | sed "s/-lcrypto/-l:libcrypto.so.${SONAME}/"`
fi

# Test if versioned linking works
${CC} ${PKG_CFLAGS} src/tests/main.c ${PKG_LIBS_VERSIONED} -o src/main.exe 2>/dev/null
if [ $? -eq 0 ]; then PKG_LIBS="${PKG_LIBS_VERSIONED}"; fi

# Suppress opensslv3 warnings for now
if [ "$SONAME" = "3" ] || [ "$SONAME" = "4" ] ; then
#PKG_CFLAGS="$PKG_CFLAGS -DOPENSSL_SUPPRESS_DEPRECATED"
PKG_CFLAGS="$PKG_CFLAGS -DOPENSSL_API_COMPAT=0x10101000L"
fi

fi #SONAME
fi #AUTOBREW

echo "Using PKG_LIBS=$PKG_LIBS"

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

# Success
exit 0
