#!/usr/bin/env bash

set -euo pipefail

NDK_ROOT="${ANDROID_NDK_HOME:-}"
if [[ -z "$NDK_ROOT" && -n "${ANDROID_HOME:-}" ]]; then
    NDK_ROOT="$(find "$ANDROID_HOME/ndk" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | sort | tail -1 || true)"
fi
if [[ -z "$NDK_ROOT" && -n "${ANDROID_SDK_ROOT:-}" ]]; then
    NDK_ROOT="$(find "$ANDROID_SDK_ROOT/ndk" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | sort | tail -1 || true)"
fi
OPUS_VERSION="v1.3.1"
OPUS_SRC_DIR="/tmp/opus-1.3.1"
API_LEVEL=21

declare -a ABIS=(
    "arm64-v8a"
    "armeabi-v7a"
    "x86_64"
)

bar() { printf '%s\n' '========================================================================'; }

require_cmd() {
    local cmd=$1
    if ! command -v "$cmd" >/dev/null 2>&1; then
        echo "ERROR: '$cmd' is required but not on PATH." >&2
        exit 1
    fi
}

clone_or_update_opus() {
    if [[ ! -d "$OPUS_SRC_DIR/.git" ]]; then
        echo "Cloning opus ${OPUS_VERSION} into $OPUS_SRC_DIR"
        rm -rf "$OPUS_SRC_DIR"
        git clone --branch "$OPUS_VERSION" --depth 1 https://github.com/xiph/opus.git "$OPUS_SRC_DIR"
        return
    fi

    echo "Reusing existing opus checkout at $OPUS_SRC_DIR"
    git -C "$OPUS_SRC_DIR" fetch --tags origin
    git -C "$OPUS_SRC_DIR" checkout "$OPUS_VERSION"
}

build_one() {
    local abi=$1
    local build_dir="/tmp/opus-android-${abi}-build"
    local install_dir="/tmp/opus-android-${abi}"

    echo "Building opus for ${abi} (API ${API_LEVEL})"
    rm -rf "$build_dir" "$install_dir"
    mkdir -p "$build_dir"

    # Opus 1.3.1's ARM NEON CMake path fails with recent NDK clang
    # (`celt_inner_prod_neon` implicit declaration). Keep this portable
    # baseline until we either patch Opus or upgrade the vendored version.
    cmake -S "$OPUS_SRC_DIR" -B "$build_dir" \
        -DCMAKE_TOOLCHAIN_FILE="$NDK_ROOT/build/cmake/android.toolchain.cmake" \
        -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
        -DANDROID_ABI="$abi" \
        -DANDROID_PLATFORM="android-${API_LEVEL}" \
        -DCMAKE_INSTALL_PREFIX="$install_dir" \
        -DBUILD_SHARED_LIBS=OFF \
        -DOPUS_BUILD_PROGRAMS=OFF \
        -DOPUS_BUILD_TESTING=OFF \
        -DOPUS_DISABLE_INTRINSICS=ON \
        -DOPUS_USE_NEON=OFF \
        -DCMAKE_POSITION_INDEPENDENT_CODE=ON

    cmake --build "$build_dir" --parallel
    cmake --install "$build_dir"

    if [[ ! -f "$install_dir/lib/libopus.a" ]]; then
        echo "ERROR: expected $install_dir/lib/libopus.a" >&2
        exit 1
    fi
}

main() {
    bar
    echo "Chanora Android opus build"
    echo "NDK        : $NDK_ROOT"
    echo "Opus tag   : $OPUS_VERSION"
    echo "API level  : $API_LEVEL"
    bar

    require_cmd git
    require_cmd cmake

    if [[ ! -d "$NDK_ROOT" ]]; then
        echo "ERROR: Android NDK not found at $NDK_ROOT" >&2
        exit 1
    fi

    clone_or_update_opus

    for abi_entry in "${ABIS[@]}"; do
        build_one "$abi_entry"
    done

    bar
    echo "Android opus builds complete."
    for abi_entry in "${ABIS[@]}"; do
        echo "  /tmp/opus-android-${abi_entry}/lib/libopus.a"
    done
}

main "$@"
