#!/bin/sh

# Library versions
LIB_VER="111"
TLS_VER="362"

# Initialise
PKG_CFLAGS=""
PKG_LIBS=""
MBEDTLS_CFLAGS=""
MBEDTLS_LIBS="-lmbedtls -lmbedx509 -lmbedcrypto"
NNG_CFLAGS=""
NNG_LIBS="-lnng"

# 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
export MACOSX_DEPLOYMENT_TARGET=`echo $CC | sed -En 's/.*-version-min=([0-9][0-9.]*).*/\1/p'`
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

# Force build bundled libs
if [ -z "$NANONEXT_LIBS" ]; then

# Find MbedTLS and compile if necessary
if [ "$INCLUDE_DIR" ] || [ "$LIB_DIR" ]
then
  MBEDTLS_CFLAGS="-I$INCLUDE_DIR"
  MBEDTLS_LIBS="-L$LIB_DIR $MBEDTLS_LIBS"
  echo "Found INCLUDE_DIR $INCLUDE_DIR"
  echo "Found LIB_DIR $LIB_DIR"
elif [ -d "/usr/local/include/mbedtls" ]
then
  MBEDTLS_CFLAGS="-I/usr/local/include"
  MBEDTLS_LIBS="-L/usr/local/lib $MBEDTLS_LIBS"
elif [ -d "/usr/include/mbedtls" ]
then
  MBEDTLS_CFLAGS="-I/usr/include"
  MBEDTLS_LIBS="-L/usr/lib $MBEDTLS_LIBS"
elif [ -d "/usr/local/opt/mbedtls" ]
then
  MBEDTLS_CFLAGS="-I/usr/local/opt/mbedtls/include"
  MBEDTLS_LIBS="-L/usr/local/opt/mbedtls/lib $MBEDTLS_LIBS"
fi
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

else
  echo "NANONEXT_LIBS is set... skipping detection"
  false
fi

if [ $? -ne 0 ]
then
  echo "Existing 'libmbedtls' >= 2.5.0 not found"
  echo "Detecting 'cmake'..."
  which cmake
  if [ $? -ne 0 ]
  then
    export PATH=$PATH:/Applications/CMake.app/Contents/bin
    which cmake
    if [ $? -ne 0 ]
    then
      echo "Required 'cmake' not found"
      exit 1
    fi
  fi
  echo "Detecting 'xz'..."
  which xz
  if [ $? -ne 0 ]
  then
    tar -xf src/mbedtls-$TLS_VER.tar.xz
    if [ $? -ne 0 ]
    then
      echo "No 'xz' command found"
      exit 1
    fi
  else
    xz -dc src/mbedtls-$TLS_VER.tar.xz | tar -xf -
  fi
  cd mbedtls-$TLS_VER
  echo "Compiling 'libmbedtls' from source ..."
  cmake -DCMAKE_INSTALL_PREFIX=../install -DCMAKE_INSTALL_LIBDIR=lib .
  cmake --build . --target install
  cd ..
  rm -rf mbedtls-$TLS_VER
else
  echo "Found 'libmbedtls' $MBEDTLS_CFLAGS"
  PKG_CFLAGS="$MBEDTLS_CFLAGS $PKG_CFLAGS"
  PKG_LIBS="$MBEDTLS_LIBS $PKG_LIBS"
fi

# Force build bundled libs
if [ -z "$NANONEXT_LIBS" ]; then

# Find NNG and compile if necessary
if [ "$INCLUDE_DIR" ] || [ "$LIB_DIR" ]
then
  NNG_CFLAGS="-I$INCLUDE_DIR"
  NNG_LIBS="-L$LIB_DIR $NNG_LIBS"
elif [ -d "/usr/local/include/nng" ]
then
  NNG_CFLAGS="-I/usr/local/include"
  NNG_LIBS="-L/usr/local/lib $NNG_LIBS"
elif [ -d "/usr/include/nng" ]
then
  NNG_CFLAGS="-I/usr/include"
  NNG_LIBS="-L/usr/lib $NNG_LIBS"
elif [ -d "/usr/local/opt/nng" ]
then
  NNG_CFLAGS="-I/usr/local/opt/nng/include"
  NNG_LIBS="-L/usr/local/opt/nng/lib $NNG_LIBS"
fi

echo "#include <nng/nng.h>
int main() {
#if NNG_MAJOR_VERSION < 1 || NNG_MAJOR_VERSION == 1 && NNG_MINOR_VERSION < 9
    *(void *) 0 = 0;
#endif
}" | ${CC} ${NNG_CFLAGS} -xc - -o /dev/null > /dev/null 2>&1

else
  echo "NANONEXT_LIBS is set... skipping detection"
  false
fi

if [ $? -ne 0 ]
then
  echo "Existing 'libnng' >= 1.9.0 not found"
  echo "Detecting 'cmake'..."
  which cmake
  if [ $? -ne 0 ]
  then
    export PATH=$PATH:/Applications/CMake.app/Contents/bin
    which cmake
    if [ $? -ne 0 ]
    then
      echo "Required 'cmake' not found"
      exit 1
    fi
  fi
  echo "Detecting 'xz'..."
  which xz
  if [ $? -ne 0 ]
  then
    tar -xf src/nng-$LIB_VER.tar.xz
    if [ $? -ne 0 ]
    then
      echo "No 'xz' command found"
      exit 1
    fi
  else
    xz -dc src/nng-$LIB_VER.tar.xz | tar -xf -
  fi
  cd nng-$LIB_VER
  echo "Compiling 'libnng' from source ..."
  cmake -DCMAKE_INSTALL_PREFIX=../install -DCMAKE_INSTALL_LIBDIR=lib .
  cmake --build . --target install
  cd ..
  rm -rf nng-$LIB_VER
else
  echo "Found 'libnng' $NNG_CFLAGS"
  PKG_CFLAGS="$NNG_CFLAGS $PKG_CFLAGS"
  PKG_LIBS="$NNG_LIBS $PKG_LIBS"
fi

if [ -d "install/lib" ]
then
  PKG_CFLAGS="-I../install/include $PKG_CFLAGS"
  if [ -f "install/lib/libmbedtls.b" ]
  then
    PKG_LIBS="../install/lib/libnng.b ../install/lib/libmbedtls.b ../install/lib/libmbedx509.b ../install/lib/libmbedcrypto.b $PKG_LIBS"
  else
    PKG_LIBS="../install/lib/libnng.b $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
