#!/bin/sh

STL_CHOSEN=$1
RELEASE_TYPE=$2
OUT_FOLDER=$3
ANDROID_FOLDER=$4
GRADLE_FLAGS=""

add_to_gradle_flags()
{
	to_add=$1
	GRADLE_FLAGS="${GRADLE_FLAGS} -P${to_add}"
}

to_lower()
{
	echo "$1" | tr '[:upper:]' '[:lower:]'
}

add_to_gradle_flags NDK_VERSION=${ANDROID_NDK_VERSION}
add_to_gradle_flags TARGET_SDK=${ANDROID_SDK_VERSION}
add_to_gradle_flags STL_TYPE=${STL_CHOSEN}
LOWER_RELEASE_TYPE=$(to_lower $RELEASE_TYPE)
# Default with gradle-wrapper, slow in docker
#./gradlew assemble${RELEASE_TYPE} ${GRADLE_FLAGS}
# Use the pre-donwloaded gradle to speed build up

(cd ${ANDROID_FOLDER} && gradle assemble${RELEASE_TYPE} ${GRADLE_FLAGS} && mkdir -p ${OUT_FOLDER} && cp app/build/outputs/apk/${LOWER_RELEASE_TYPE}/*.apk ${OUT_FOLDER})
