#!/bin/sh

# Library versions
LIB_VER="8e1836f"
TLS_VER="1873d3b"

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

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

# 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
  PKG_CFLAGS="-I$INCLUDE_DIR $PKG_CFLAGS"
  PKG_LIBS="-L$LIB_DIR $PKG_LIBS"
  echo "Found INCLUDE_DIR $INCLUDE_DIR"
  echo "Found LIB_DIR $LIB_DIR"
elif [ -d "/usr/local/include/mbedtls" ]
then
  PKG_CFLAGS="-I/usr/local/include $PKG_CFLAGS"
  PKG_LIBS="-L/usr/local/lib $PKG_LIBS"
  echo "Found 'libmbedtls' $PKG_CFLAGS"
elif [ -d "/usr/include/mbedtls" ]
then
  PKG_CFLAGS="-I/usr/include $PKG_CFLAGS"
  PKG_LIBS="-L/usr/lib $PKG_LIBS"
  echo "Found 'libmbedtls' $PKG_CFLAGS"
elif [ -d "/usr/local/opt/mbedtls" ]
then
  PKG_CFLAGS="-I/usr/local/opt/mbedtls/include $PKG_CFLAGS"
  PKG_LIBS="-L/usr/local/opt/mbedtls/lib $PKG_LIBS"
  echo "Found 'libmbedtls' $PKG_CFLAGS"
fi
echo "#include <mbedtls/version.h>
int main() {
#if MBEDTLS_VERSION_MAJOR >= 2
    return 0;
#else
    void *err;
    return *err;
#endif
}" | ${CC} ${PKG_CFLAGS} -xc - -o /dev/null > /dev/null 2>&1

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

if [ $? -ne 0 ]
then
  echo "No existing 'libmbedtls' >= 2 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 inst/mbedtls-$TLS_VER.tar.xz
    if [ $? -ne 0 ]
    then
      echo "No 'xz' command found"
      exit 1
    fi
  else
    xz -dc inst/mbedtls-$TLS_VER.tar.xz | tar -xf -
  fi
  cd mbedtls-$TLS_VER
  echo "Compiling 'libmbedtls' from source ..."
  cmake -DCMAKE_INSTALL_PREFIX=../install .
  cmake --build .
  cmake --install .
  cd ..
  rm -rf mbedtls-$TLS_VER
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"
elif [ -d "/usr/local/include/nng" ]
then
  NNG_CFLAGS="-I/usr/local/include"
  NNG_LIBS="-L/usr/local/lib"
elif [ -d "/usr/include/nng" ]
then
  NNG_CFLAGS="-I/usr/include"
  NNG_LIBS="-L/usr/lib"
elif [ -d "/usr/local/opt/nng" ]
then
  NNG_CFLAGS="-I/usr/local/opt/nng/include"
  NNG_LIBS="-L/usr/local/opt/nng/lib"
fi

echo "#include <nng/nng.h>
int main() {
#if NNG_MAJOR_VERSION == 1 && NNG_MINOR_VERSION >= 5 || NNG_MAJOR_VERSION > 1
    return 0;
#else
    void *err;
    return *err;
#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 "No existing 'libnng' >= 1.5 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 inst/nng-$LIB_VER.tar.xz
    if [ $? -ne 0 ]
    then
      echo "No 'xz' command found"
      exit 1
    fi
  else
    xz -dc inst/nng-$LIB_VER.tar.xz | tar -xf -
  fi
  cd nng-$LIB_VER
  echo "Compiling 'libnng' from source ..."
  cmake -DCMAKE_INSTALL_PREFIX=../install .
  cmake --build .
  cmake --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/lib64" ]
then
  PKG_CFLAGS="-I../install/include $PKG_CFLAGS"
  PKG_LIBS="-L../install/lib64 $PKG_LIBS"
fi
if [ -d "install/lib" ]
then
  PKG_CFLAGS="-I../install/include $PKG_CFLAGS"
  PKG_LIBS="-L../install/lib $PKG_LIBS"
fi

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

# Success
exit 0
