#!/bin/sh

set -e

if [ -z "${ANDROID_SDK_HOME}" ];
then echo "Please set ANDROID_SDK_HOME, exiting"; exit;
else echo "ANDROID_SDK_HOME is ${ANDROID_SDK_HOME}";
fi

if [ -z "${ANDROID_NDK_HOME}" ];
then echo "Please set ANDROID_NDK_HOME, exiting"; exit;
else echo "ANDROID_NDK_HOME is ${ANDROID_NDK_HOME}";
fi


generate_local_properties() {
	: > local.properties
	echo "sdk.dir=${ANDROID_SDK_HOME}" >> local.properties
	echo "ndk.dir=${ANDROID_NDK_HOME}" >> local.properties
}

glslang=$(realpath ../../../external/glslang/build/install/bin/glslangValidator)

prebuild() {
  ( cd ..; python3 generate-dispatch-table.py HelpersDispatchTable.h )
  ( cd ..; python3 generate-dispatch-table.py HelpersDispatchTable.cpp )
  ( cd ..; python3 glsl-to-spirv Smoke.frag Smoke.frag.h ${glslang} )
  ( cd ..; python3 glsl-to-spirv Smoke.vert Smoke.vert.h ${glslang} )
  ( cd ..; python3 glsl-to-spirv Smoke.push_constant.vert Smoke.push_constant.vert.h ${glslang} )
}

build() {
	./gradlew build
}

install() {
	adb install -r build/outputs/apk/android-debug.apk
}

run() {
	adb shell am start com.example.Smoke/android.app.NativeActivity
}

generate_local_properties
prebuild
build
install
#run
