#!/bin/sh
# Anticonf (tm) script originally by Jeroen Ooms (2015) from rlib/xml2
# Modified by James Eapen (2024)
# 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'

version_greater_equal() {
    printf '%s\n%s\n' "$2" "$1" | sort --check=quiet --version-sort
}

checkGCCVersion() {
    version=$($1 -dumpfullversion)
    if version_greater_equal "$version" "$2"; then
      echo "     $CC version $version meets minimum requirements!"
    else
      echo "------------------------- ANTICONF ERROR ---------------------------"
      echo "Configuration failed because $1 version $version does not meet the minimum version requirement $2 "
      echo "--------------------------------------------------------------------"
      exit 1;
    fi
}

checkHtsVersion() {
    version=$(pkg-config --modversion htslib)

    if version_greater_equal "$version" "$1"; then
      echo "     htslib version $version meets minimum requirements!"
    else
      echo "------------------------- ANTICONF ERROR ---------------------------"
      echo "Configuration failed because htslib version $version does not meet the minimum version requirement $1"
      echo "--------------------------------------------------------------------"
      exit 1;
    fi
}

echo "*** Configuring for package installation..."

# Linux or MacOS
PLATFORM="$(uname)"

# Library settings
PKG_CONFIG_NAME="htslib"
PKG_DEB_NAME="libhts-dev"
PKG_RPM_NAME="htslib-devel"
PKG_CONDA_NAME="htslib"
PKG_NIX_NAME="htslib"
PKG_BREW_NAME="htslib"
PKG_TEST_HEADER="<htslib/hts.h>"
PKG_LIBS="-lhts"

# 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
  echo "**** Trying pkg-config to use system htslib headers..."
  pkg-config --version >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    PKGCONFIG_CFLAGS=$(pkg-config --cflags $PKG_CONFIG_NAME 2> /dev/null)
    PKGCONFIG_LIBS=$(pkg-config --libs $PKG_CONFIG_NAME 2> /dev/null)
  fi

  if [ -z "$PKGCONFIG_CFLAGS" ] && [ -z "$PKGCONFIG_LIBS" ]; then
    echo "     Falling back to Rhtslib"
    RHTSLIB_CFLAGS=$("${R_HOME}/bin${R_ARCH_BIN}/Rscript" -e 'Rhtslib::pkgconfig("PKG_CPPFLAGS")')
    RHTSLIB_LIBS=$("${R_HOME}/bin${R_ARCH_BIN}/Rscript" -e 'Rhtslib::pkgconfig("PKG_LIBS")')
  fi

  if [ "$PKGCONFIG_CFLAGS" ] || [ "$PKGCONFIG_LIBS" ]; then
    echo "     Found pkg-config cflags and libs for ${PKG_CONFIG_NAME}!"
    PKG_CFLAGS=${PKGCONFIG_CFLAGS}
    PKG_LIBS=${PKGCONFIG_LIBS}
  elif [ "$RHTSLIB_CFLAGS" ] || [ "$RHTSLIB_LIBS" ]; then
    echo "     Using Rhtslib cflags and libs for ${PKG_CONFIG_NAME}!"
    PKG_CFLAGS="${RHTSLIB_CFLAGS}"
    PKG_LIBS="${RHTSLIB_LIBS}"
  else
      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 " * nixpkgs: $PKG_NIX_NAME (NixOS)"
      echo " * brew: $PKG_BREW_NAME (MacOS, linux)"
      echo " * bioconda: $PKG_CONDA_NAME (conda, miniconda, mamba, pixi)"
      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 "--------------------------------------------------------------------"
      exit 1;
  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`

if $(echo $CC | grep -q "clang"); then
    echo "**** Using clang"
    $CC --version | sed 's/^/     /'
else
    echo "**** Checking GCC version"
    checkGCCVersion "$CC" 9.4
fi

if [ -z "$RHTSLIB_CFLAGS" ]; then
    echo "**** Checking htslib version"
    checkHtsVersion 1.17
fi

# For debugging
echo "**** Compilation flags"
echo "     Using PKG_CFLAGS=$PKG_CFLAGS"
echo "     Using PKG_LIBS=$PKG_LIBS"

# Test configuration
if [ -z $RHTSLIB_CFLAGS ]; then
    echo "#include $PKG_TEST_HEADER" | ${CC} ${CPPFLAGS} ${PKG_CFLAGS} ${CFLAGS} -E -xc - >/dev/null 2>&1 || R_CONFIG_ERROR=1;
fi

if [ $R_CONFIG_ERROR ]; then
  echo "------------------------- ANTICONF ERROR ---------------------------"
  echo "Could not test config by compiling $PKG_TEST_HEADER with this command:"
  echo "echo #include $PKG_TEST_HEADER | ${CC} ${CPPFLAGS} ${PKG_CFLAGS} ${CFLAGS} -E -xc -"
  echo "--------------------------------------------------------------------"
  exit 1;
fi

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

# Notes
echo "**** Checking if htslib was compiled with libdeflate"

if [ "$PLATFORM" = "Darwin" ]; then
    LDD="otool -L"
else
    LDD="ldd"
fi

if [ -n "$RHTSLIB_LIBS" ]; then
    echo "     NOTE: iscream queries may be slower with Rhtslib than with htslib compiled with libdeflate"
    echo "     See <https://huishenlab.github.io/iscream/articles/htslib.html> for more information"
elif htsloc=$(which tabix) || htsloc=$(ldconfig -p | grep libhts.so | tail -n1 | cut -f 4 -d ' '); then
    if $LDD "$htsloc" | grep -q 'libdeflate'; then
        echo "     htslib compiled with libdeflate!"
    else
        echo "     NOTE: htslib not compiled with libdeflate - iscream querys may be slower without libdeflate"
        echo "     See <https://huishenlab.github.io/iscream/articles/htslib.html> for more information"
    fi
else
    echo "     Could not check for libdeflate - paths to tabix and libhts.so could not be found"
fi

# Success
echo "**** Success!"
exit 0

