#!/bin/sh

# Initialise
PKG_CFLAGS=""
PKG_LIBS=""

# Helper function: detect cmake
detect_cmake() {
  if which cmake > /dev/null 2>&1; then
    return 0
  fi
  export PATH=$PATH:/Applications/CMake.app/Contents/bin
  if which cmake > /dev/null 2>&1; then
    return 0
  fi
  echo "Required 'cmake' not found"
  exit 1
}

# Helper function: find library paths
# Usage: find_lib_paths <lib_name> <default_libs>
# Sets: LIB_CFLAGS, LIB_LIBS
find_lib_paths() {
  lib_name=$1
  default_libs=$2
  LIB_CFLAGS=""
  LIB_LIBS="$default_libs"

  if [ "$INCLUDE_DIR" ] || [ "$LIB_DIR" ]; then
    LIB_CFLAGS="-I$INCLUDE_DIR"
    LIB_LIBS="-L$LIB_DIR $LIB_LIBS"
    echo "Found INCLUDE_DIR $INCLUDE_DIR"
    echo "Found LIB_DIR $LIB_DIR"
  elif [ -d "/usr/local/include/$lib_name" ]; then
    LIB_CFLAGS="-I/usr/local/include"
    LIB_LIBS="-L/usr/local/lib $LIB_LIBS"
  elif [ -d "/usr/include/$lib_name" ]; then
    LIB_CFLAGS="-I/usr/include"
    LIB_LIBS="-L/usr/lib $LIB_LIBS"
  elif [ -d "/usr/local/opt/$lib_name" ]; then
    LIB_CFLAGS="-I/usr/local/opt/$lib_name/include"
    LIB_LIBS="-L/usr/local/opt/$lib_name/lib $LIB_LIBS"
  fi
}

# Find compiler and export flags
CC=`"${R_HOME}/bin/R" CMD config CC`
CFLAGS=`"${R_HOME}/bin/R" CMD config CFLAGS`
LDFLAGS=`"${R_HOME}/bin/R" CMD config LDFLAGS`
export CC CFLAGS LDFLAGS

if [ -z "$MACOSX_DEPLOYMENT_TARGET" ]; then
  MACOSX_DEPLOYMENT_TARGET=`echo $CC | sed -En 's/.*-version-min=([0-9][0-9.]*).*/\1/p'`
  [ -n "$MACOSX_DEPLOYMENT_TARGET" ] && export MACOSX_DEPLOYMENT_TARGET
fi

# Detect -latomic linker flag for ARM architectures (Raspberry Pi etc.)
echo "#include <stdint.h>
uint64_t v;
int main() {
    return (int)__atomic_load_n(&v, __ATOMIC_ACQUIRE);
}" | ${CC} -xc - -o /dev/null > /dev/null 2>&1
if [ $? -ne 0 ]; then
  echo "Adding -latomic linker flag ..."
  PKG_LIBS="$PKG_LIBS -latomic"
fi

# Determine whether to compile bundled libraries
compile_mbedtls=0
compile_nng=0

if [ -n "$NANONEXT_LIBS" ]; then
  echo "NANONEXT_LIBS is set... building from source"
  compile_mbedtls=1
  compile_nng=1
else
  # Find MbedTLS
  find_lib_paths "mbedtls" "-lmbedtls -lmbedx509 -lmbedcrypto"
  MBEDTLS_CFLAGS="$LIB_CFLAGS"
  MBEDTLS_LIBS="$LIB_LIBS"

  echo "#include <mbedtls/version.h>
int main() {
#if MBEDTLS_VERSION_MAJOR < 2 || MBEDTLS_VERSION_MAJOR == 2 && MBEDTLS_VERSION_MINOR < 5
    *(void *) 0 = 0;
#endif
}" | ${CC} ${MBEDTLS_CFLAGS} -xc - -o /dev/null > /dev/null 2>&1
  if [ $? -ne 0 ]; then
    compile_mbedtls=1
  else
    echo "Found 'libmbedtls' $MBEDTLS_CFLAGS"
    PKG_CFLAGS="$MBEDTLS_CFLAGS $PKG_CFLAGS"
    PKG_LIBS="$MBEDTLS_LIBS $PKG_LIBS"
  fi

  # Find NNG
  find_lib_paths "nng" "-lnng"
  NNG_CFLAGS="$LIB_CFLAGS"
  NNG_LIBS="$LIB_LIBS"

  echo "#include <nng/nng.h>
int main() {
#if NNG_MAJOR_VERSION < 1 || NNG_MAJOR_VERSION == 1 && NNG_MINOR_VERSION < 11
    *(void *) 0 = 0;
#endif
}" | ${CC} ${NNG_CFLAGS} -xc - -o /dev/null > /dev/null 2>&1
  if [ $? -ne 0 ]; then
    compile_nng=1
  else
    echo "Found 'libnng' $NNG_CFLAGS"
    PKG_CFLAGS="$NNG_CFLAGS $PKG_CFLAGS"
    PKG_LIBS="$NNG_LIBS $PKG_LIBS"
  fi
fi

# Compile MbedTLS if needed
if [ $compile_mbedtls -eq 1 ]; then
  echo "Existing 'libmbedtls' >= 2.5.0 not found"
  detect_cmake
  echo "Compiling 'libmbedtls' from source ..."
  cmake -S src/mbedtls -B nano-build -DCMAKE_INSTALL_PREFIX=nano-install -DCMAKE_INSTALL_LIBDIR=lib
  cmake --build nano-build --target install
  rm -rf nano-build
fi

# Compile NNG if needed
if [ $compile_nng -eq 1 ]; then
  echo "Existing 'libnng' >= 1.11.0 not found"
  detect_cmake
  echo "Compiling 'libnng' from source ..."
  cmake -S src/nng -B nano-build -DCMAKE_INSTALL_PREFIX=nano-install -DCMAKE_INSTALL_LIBDIR=lib
  cmake --build nano-build --target install
  rm -rf nano-build
fi

# Set flags for bundled libraries
if [ -d "nano-install/lib" ]; then
  PKG_CFLAGS="-I../nano-install/include -DNNG_STATIC_LIB $PKG_CFLAGS"
  if [ -f "nano-install/lib/libmbedtls.a" ]; then
    PKG_LIBS="../nano-install/lib/libnng.a ../nano-install/lib/libmbedtls.a ../nano-install/lib/libmbedx509.a ../nano-install/lib/libmbedcrypto.a $PKG_LIBS"
  else
    PKG_LIBS="../nano-install/lib/libnng.a $PKG_LIBS"
  fi
fi

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

# Success
exit 0
