#!/usr/bin/env bash
# Host build accelerators for native codecs (libvpx / NDK). Source, do not execute.
#
# ccache (default: auto-detect):
#   ANDROIDCAST_CCACHE=auto|1|0
#   CCACHE_DIR          — default: <repo>/build/.ccache
#
# distcc (off by default; experimental for libvpx configure/make only, not ndk-build):
#   ANDROIDCAST_USE_DISTCC=1
#   DISTCC_HOSTS=localhost/8 192.168.1.10/16
#
set -euo pipefail

[[ -n "${ANDROIDCAST_NATIVE_CACHE_SOURCED:-}" ]] && return 0
ANDROIDCAST_NATIVE_CACHE_SOURCED=1

androidcast_project_root() {
  if [[ -n "${ANDROIDCAST_ROOT:-}" ]]; then
    printf '%s\n' "$ANDROIDCAST_ROOT"
    return 0
  fi
  local here
  here="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  printf '%s\n' "$here"
}

androidcast_ccache_wanted() {
  case "${ANDROIDCAST_CCACHE:-auto}" in
    0 | false | FALSE | no | NO | off | OFF) return 1 ;;
    1 | true | TRUE | yes | YES | on | ON) return 0 ;;
    auto | AUTO | "")
      command -v ccache >/dev/null 2>&1
      ;;
    *) return 0 ;;
  esac
}

androidcast_ndk_clang_basename() {
  case "${1:-}" in
    arm64-v8a) echo aarch64-linux-android29-clang ;;
    armeabi-v7a) echo armv7a-linux-androideabi29-clang ;;
    x86_64) echo x86_64-linux-android29-clang ;;
    x86) echo i686-linux-android29-clang ;;
    *) echo "" ;;
  esac
}

androidcast_ndk_clangxx_basename() {
  case "${1:-}" in
    arm64-v8a) echo aarch64-linux-android29-clang++ ;;
    armeabi-v7a) echo armv7a-linux-androideabi29-clang++ ;;
    x86_64) echo x86_64-linux-android29-clang++ ;;
    x86) echo i686-linux-android29-clang++ ;;
    *) echo "" ;;
  esac
}

# Configure ccache + optional distcc; set CC/CXX/NDK_CCACHE for libvpx + ndk-build.
# Args: project_root abi ndk_root ndk_host_bin
androidcast_apply_native_build_accelerators() {
  local root="${1:?root}"
  local abi="${2:?abi}"
  local ndk_bin="${4:?ndk_host_bin}"
  local clang clangxx
  clang="$(androidcast_ndk_clang_basename "$abi")"
  clangxx="$(androidcast_ndk_clangxx_basename "$abi")"
  if [[ -z "$clang" || ! -x "$ndk_bin/$clang" ]]; then
    echo "WARN: NDK clang not found for $abi under $ndk_bin" >&2
    return 0
  fi

  local prefix=""
  local ccache_on=false
  local distcc_on=false

  if androidcast_ccache_wanted && command -v ccache >/dev/null 2>&1; then
    ccache_on=true
    export CCACHE_DIR="${CCACHE_DIR:-$root/build/.ccache}"
    mkdir -p "$CCACHE_DIR"
    export CCACHE_BASEDIR="$root"
    export CCACHE_COMPRESS="${CCACHE_COMPRESS:-1}"
    export CCACHE_COMPILERCHECK="${CCACHE_COMPILERCHECK:-content}"
    export CCACHE_MAXSIZE="${CCACHE_MAXSIZE:-5G}"
    export NDK_CCACHE="${NDK_CCACHE:-ccache}"
    prefix="ccache "
    echo "ccache: ON (dir=$CCACHE_DIR, NDK_CCACHE=$NDK_CCACHE)"
    ccache -s 2>/dev/null | sed 's/^/  /' || true
  elif androidcast_ccache_wanted; then
    echo "ccache: requested but not installed (install ccache or ANDROIDCAST_CCACHE=0)"
  else
    echo "ccache: off (ANDROIDCAST_CCACHE=${ANDROIDCAST_CCACHE:-auto})"
  fi

  case "${ANDROIDCAST_USE_DISTCC:-0}" in
    1 | true | TRUE | yes | YES | on | ON)
      if command -v distcc >/dev/null 2>&1 && [[ -n "${DISTCC_HOSTS:-}" ]]; then
        distcc_on=true
        export DISTCC_VERBOSE="${DISTCC_VERBOSE:-0}"
        prefix="distcc ${prefix}"
        echo "distcc: ON (experimental, libvpx make/configure only; hosts=$DISTCC_HOSTS)"
      elif command -v distcc >/dev/null 2>&1; then
        echo "distcc: ANDROIDCAST_USE_DISTCC=1 but DISTCC_HOSTS empty — skipped" >&2
      else
        echo "distcc: requested but distcc not installed — skipped" >&2
      fi
      ;;
    *)
      echo "distcc: off (set ANDROIDCAST_USE_DISTCC=1 and DISTCC_HOSTS to try)"
      ;;
  esac

  export CC="${prefix}${ndk_bin}/${clang}"
  if [[ -n "$clangxx" && -x "$ndk_bin/$clangxx" ]]; then
    export CXX="${prefix}${ndk_bin}/${clangxx}"
  else
    export CXX="${prefix}${ndk_bin}/${clang}"
  fi
  export AR="${ndk_bin}/llvm-ar"
  export RANLIB="${ndk_bin}/llvm-ranlib"

  # libvpx configure reads these when cross-compiling.
  export AS="$CC"
  export LD="${ndk_bin}/ld.lld"
}
