#!/bin/bash

# The example build file on building Android package.
set -e -x -u

OSNAME=android
if [[ -z ${NDK_ROOT+x} ]]; then NDK_ROOT=`ls -Ad $HOME/Android/Sdk/ndk/* 2>/dev/null | head -1`; fi
if [[ -z "${NDK_ROOT}" ]]
then
    echo "ERROR: cannot find where NDK was installed, using NDK_ROOT to specify it"
    exit 7
fi
if [[ -z ${ANDROID_SDK_ROOT+x} ]]; then
    export ANDROID_SDK_ROOT=$(dirname $(dirname  ${NDK_ROOT}))
fi
mkdir -p out/$OSNAME/Release && cd out/$OSNAME/Release

# ANDROID_ABI would be armeabi-v7a arm64-v8a x86 x86_64
# ANDROID_PLATFORM, the minimum level is 24, i.e., due to
#       the great change of file system permission in Android 7
cmake "$@" \
    -DCMAKE_TOOLCHAIN_FILE=${NDK_ROOT}/build/cmake/android.toolchain.cmake  \
    -DANDROID_NDK=${NDK_ROOT}                                               \
    -DANDROID_ABI=armeabi-v7a                                               \
    -DANDROID_PLATFORM=android-24                                           \
    -DOCOS_BUILD_ANDROID=ON                                                 \
    ../../.. && cmake --build . --config Release --parallel
