#!/usr/bin/env bash
# Prepares android/ to be built: a scratch, patched copy of engine/ (see
# android/engine-patches/) and of android/gl4es-src/ (see
# android/gl4es-patches/) - neither vendored tree itself is ever modified -
# the Android SDL2/SDL2_mixer/fluidsynth-lite/openxr prebuilts staged into
# jniLibs, bundled assets, and android/engine.properties (engineDir/
# gl4esDir/prebuiltDir, read by android/app/build.gradle). gl4es itself is
# NOT prebuilt here - it's compiled from this scratch copy as a CMake
# subdirectory of the engine's own native build (see
# android/engine-patches/02-android-opengl-es.patch), so gl4es-patches/
# changes only need a normal rebuild, not a build-image rebuild. Always
# run inside this image (via ../run-image.sh, or directly if already
# inside it) - it reads from /opt/prebuilt/android/*, which only exists
# there.
#
# Two modes:
#   (no args)      - for build-apk.sh's own gradlew, invoked in this same
#                     container - engine.properties points at this
#                     container's own paths (e.g. /workspace/build/...).
#   --host-paths   - for building android/ natively in Android Studio on
#                     the HOST afterwards, instead of via build-apk.sh.
#                     Needs HOST_REPO_ROOT (set by run-image.sh) to know
#                     this container's bind-mounted repo root's path on
#                     the host, since a plain container-internal path
#                     like /workspace/... doesn't resolve there. Also
#                     exports /opt/prebuilt/android (host-invisible,
#                     image-only) to build/android-prebuilt so a host-side
#                     CMake configure can actually find it.
set -euo pipefail

HOST_PATHS=0
if [ "${1:-}" = "--host-paths" ]; then
    HOST_PATHS=1
fi

REPO_ROOT="$(pwd)"
ANDROID_DIR="$REPO_ROOT/android"
SCRATCH_ENGINE="$REPO_ROOT/build/android-engine"
SCRATCH_GL4ES="$REPO_ROOT/build/android-gl4es"
PREBUILT_EXPORT_DIR="$REPO_ROOT/build/android-prebuilt"

echo "== Preparing a scratch, patched copy of engine/ =="
rm -rf "$SCRATCH_ENGINE"
mkdir -p "$(dirname "$SCRATCH_ENGINE")"
cp -a "$REPO_ROOT/engine" "$SCRATCH_ENGINE"
for p in "$ANDROID_DIR"/engine-patches/*.patch; do
    patch -p1 -d "$SCRATCH_ENGINE" < "$p"
done

echo "== Preparing a scratch, patched copy of android/gl4es-src/ =="
rm -rf "$SCRATCH_GL4ES"
cp -a "$ANDROID_DIR/gl4es-src" "$SCRATCH_GL4ES"
for p in "$ANDROID_DIR"/gl4es-patches/*.patch; do
    patch -p1 -d "$SCRATCH_GL4ES" < "$p"
done

echo "== Wiring up prebuilt Android SDL2_mixer/fluidsynth-lite (BUNDLED mode, like the desktop build) =="
mkdir -p "$SCRATCH_ENGINE/build_ext/built_sdl_mixer" "$SCRATCH_ENGINE/build_ext/fluidsynth-lite"
cp -a /opt/prebuilt/android/sdl2_mixer/. "$SCRATCH_ENGINE/build_ext/built_sdl_mixer/"
mkdir -p "$SCRATCH_ENGINE/build_ext/fluidsynth-lite/src"
cp -a /opt/prebuilt/android/fluidsynth-lite/lib/. "$SCRATCH_ENGINE/build_ext/fluidsynth-lite/src/"
cp -a /opt/prebuilt/android/fluidsynth-lite/include/. "$SCRATCH_ENGINE/build_ext/fluidsynth-lite/include/"

if [ "$HOST_PATHS" -eq 1 ]; then
    : "${HOST_REPO_ROOT:?--host-paths needs HOST_REPO_ROOT set - run via ./run-image.sh, not directly}"
    echo "== Exporting prebuilt Android SDL2/SDL2_mixer/fluidsynth-lite/openxr for host-side use =="
    rm -rf "$PREBUILT_EXPORT_DIR"
    cp -a /opt/prebuilt/android "$PREBUILT_EXPORT_DIR"
    ENGINE_DIR_PROP="$HOST_REPO_ROOT/build/android-engine"
    GL4ES_DIR_PROP="$HOST_REPO_ROOT/build/android-gl4es"
    PREBUILT_DIR_PROP="$HOST_REPO_ROOT/build/android-prebuilt"
else
    ENGINE_DIR_PROP="$SCRATCH_ENGINE"
    GL4ES_DIR_PROP="$SCRATCH_GL4ES"
    PREBUILT_DIR_PROP="/opt/prebuilt/android"
fi

{
    echo "engineDir=$ENGINE_DIR_PROP"
    echo "gl4esDir=$GL4ES_DIR_PROP"
    echo "prebuiltDir=$PREBUILT_DIR_PROP"
} > "$ANDROID_DIR/engine.properties"

echo "== Staging bundled assets (shaders, soundfont, get-assets text) =="
ASSETS_DIR="$ANDROID_DIR/app/src/main/assets"
rm -rf "$ASSETS_DIR"
mkdir -p "$ASSETS_DIR/shaders" "$ASSETS_DIR/res"
# GLES ports of engine/shaders/ (see android/gles-shaders/ and
# android/engine-patches/04-android-opengl-es-render.patch) - not
# engine/shaders/ itself, which is desktop-only GLSL.
cp -a "$ANDROID_DIR/gles-shaders/." "$ASSETS_DIR/shaders/"
cp "/opt/prebuilt/soundfont/default.sf2" "$ASSETS_DIR/res/soundfont.sf2"
cp "$REPO_ROOT/res/assets/GET_ASSETS_QUEST.txt" "$ASSETS_DIR/GET_ASSETS_QUEST.txt"

echo "== Staging prebuilt Android SDL2/SDL2_mixer/fluidsynth-lite/openxr .so's into jniLibs =="
# gl4es is deliberately absent here - it's no longer prebuilt, it's compiled
# from $SCRATCH_GL4ES as part of the engine's own native build (see
# android/engine-patches/02-android-opengl-es.patch); AGP's own native-build
# packaging picks up its compiled libGL.so directly, without needing it
# staged into jniLibs by this script.
JNI_LIBS_DIR="$ANDROID_DIR/app/src/main/jniLibs/arm64-v8a"
rm -rf "$ANDROID_DIR/app/src/main/jniLibs"
mkdir -p "$JNI_LIBS_DIR"
find /opt/prebuilt/android/sdl2/lib /opt/prebuilt/android/sdl2_mixer/lib /opt/prebuilt/android/fluidsynth-lite/lib \
    /opt/prebuilt/android/openxr/lib \
    -name '*.so' -exec cp -a {} "$JNI_LIBS_DIR/" \;

echo "== Done =="
echo "engineDir=$ENGINE_DIR_PROP"
echo "gl4esDir=$GL4ES_DIR_PROP"
echo "prebuiltDir=$PREBUILT_DIR_PROP"
if [ "$HOST_PATHS" -eq 1 ]; then
    echo "android/ is ready to open and build natively in Android Studio on the host."
fi
