for ARCH in x64 i386; do

  if [ -e "${R_HOME}/bin/${ARCH}/R" ]; then
    CC=`"${R_HOME}/bin/${ARCH}/R" CMD config CC`
    CFLAGS=`"${R_HOME}/bin/${ARCH}/R" CMD config CFLAGS`
    LDFLAGS=`"${R_HOME}/bin/${ARCH}/R" CMD config LDFLAGS`
    export CC CFLAGS LDFLAGS

    echo "Compiling 'libmbedtls' from source for ${ARCH} ..."
    cmake -S src/mbedtls -B nano-build -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=nano-install/${ARCH}
    cmake --build nano-build --target install
    rm -rf nano-build

    echo "Compiling 'libnng' from source for ${ARCH} ..."
    cmake -S src/nng -B nano-build -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=nano-install/${ARCH}
    cmake --build nano-build --target install
    rm -rf nano-build

  else
  echo "Note: ${ARCH} not installed, skipping ..."
  fi

done

# Success
exit 0
