#!/bin/sh

export TERM=${TERM:-dumb}

mkdir -p ${SRC_DIR}/Rlibrary
export R_LIBS_USER=${SRC_DIR}/Rlibrary

while IFS='.' read -ra ADDR; do
    for i in "${ADDR[@]}"; do
        buildnum=$i
    done
done <<< "$PKG_VERSION"

echo BUILD_NUMBER=$buildnum > gradle/buildnumber.properties

# ./gradlew syncRPackages --stacktrace

if [ `uname` == Linux  ]; then
    # Monkeypatch R's Makeconfig for the sake of making kernlab work
    R_EXTRA_CPPFLAGS=$(grep -Poh "(?<=R_XTRA_CPPFLAGS = )[\ \$\(\)_\-A-Za-z0-9]+" "$PREFIX/lib/R/etc/Makeconf")
    sed -i "s/$R_EXTRA_CPPFLAGS/$R_EXTRA_CPPFLAGS -DDO_NOT_USE_CXX_HEADERS/" "$PREFIX/lib/R/etc/Makeconf"

    # at time of writing, there's a parameter error in the tests, only on Linux?  skip tests.
    ./gradlew build -x test -x h2o-r:buildPackageDocumentation
else
    # On OS X, things run OK, but only on machines with lots of memory.
    # ./gradlew syncSmalldata --stacktrace
    # ./gradlew build --stacktrace -i

    ./gradlew build -x test -x h2o-r:buildPackageDocumentation

fi

mkdir -p $PREFIX/h2o_jar
mv build/h2o.jar $PREFIX/h2o_jar/
