#!/usr/bin/env bash
# Fill vendor/vanta/prebuilt/ (gitignored) before apply.sh mirrors it:
#  * bootanimation.zip, always: Vanta's own (tools/bootanim), rendered here,
#    or fetched from the VantaGit release if this host cannot render it.
#  * optional APKs, only when their flag is on AND the file exists:
#      VANTA_WITH_BRIDGE=true  s10-bridge.apk   (from failure-s10, Zo side)
#      VANTA_WITH_HALO=true    ZodroidHalo.apk  (from zodroid, Zo side)
# Soong parses every android_app_import in the tree, so an import must never
# point at a file that is not there: the modules for staged APKs are written
# into prebuilt/Android.bp here, and nothing is declared for absent ones.
set -euo pipefail
P=$(cd "$(dirname "$0")/.." && pwd)
PROJ=${VANTA_PROJECTS:-$(dirname "$P")}
OUT="$P/vendor/vanta/prebuilt"
REL=${VANTA_BOOTANIM_URL:-https://vanta.failure.fail/git/vanta/vanta-bootanimation/releases/download/v0.1/bootanimation.zip}
mkdir -p "$OUT"

stored_ok() {
  python3 - "$1" <<'PY'
import sys, zipfile
try:
    z = zipfile.ZipFile(sys.argv[1])
except Exception as e:
    sys.exit(f"not a zip: {e}")
bad = [i.filename for i in z.infolist() if i.compress_type != zipfile.ZIP_STORED]
if bad: sys.exit(f"deflated members (bootanimation will not play them): {bad[:3]}")
if "desc.txt" not in z.namelist(): sys.exit("no desc.txt")
PY
}

SRC_BOOT="$P/out/bootanim/bootanimation.zip"
if [ ! -s "$SRC_BOOT" ]; then
  if ! "$P/tools/bootanim/build.sh" >/dev/null 2>&1; then
    echo "stage: cannot render the boot animation here; fetching $REL"
    mkdir -p "$(dirname "$SRC_BOOT")"
    curl -fsSL "$REL" -o "$SRC_BOOT.part" && mv "$SRC_BOOT.part" "$SRC_BOOT"
  fi
fi
[ -s "$SRC_BOOT" ] || { echo "stage: no boot animation (render failed, download failed)" >&2; exit 2; }
stored_ok "$SRC_BOOT" || { echo "stage: $SRC_BOOT is not a playable boot animation" >&2; exit 2; }
install -m 0644 "$SRC_BOOT" "$OUT/bootanimation.zip"

rm -f "$OUT/s10-bridge.apk" "$OUT/ZodroidHalo.apk"
BP="$OUT/Android.bp"
echo "// Written by tools/stage.sh for the APKs staged in this run. Do not edit." > "$BP"
if [ "${VANTA_WITH_BRIDGE:-false}" = true ]; then
  B=$(ls "$PROJ"/failure-s10/apk/dist/s10-bridge-*.apk 2>/dev/null | sort -V | tail -1 || true)
  [ -n "$B" ] || { echo "stage: VANTA_WITH_BRIDGE=true but no s10-bridge apk under $PROJ/failure-s10/apk/dist" >&2; exit 2; }
  install -m 0644 "$B" "$OUT/s10-bridge.apk"
  cat >> "$BP" <<'BPEOF'

android_app_import {
    name: "S10Bridge",
    apk: "s10-bridge.apk",
    presigned: true,
    product_specific: true,
    dex_preopt: {
        enabled: false,
    },
}
BPEOF
fi
if [ "${VANTA_WITH_HALO:-false}" = true ]; then
  H="$PROJ/zodroid/rom/apk/ZodroidHalo-release-unsigned.apk"
  [ -s "$H" ] || { echo "stage: VANTA_WITH_HALO=true but $H is missing" >&2; exit 2; }
  install -m 0644 "$H" "$OUT/ZodroidHalo.apk"
  cat >> "$BP" <<'BPEOF'

android_app_import {
    name: "ZodroidHalo",
    apk: "ZodroidHalo.apk",
    certificate: "platform",
    privileged: true,
    system_ext_specific: true,
    dex_preopt: {
        enabled: false,
    },
}
BPEOF
fi
( cd "$OUT" && sha256sum ./*.zip ./*.apk 2>/dev/null > SHA256SUMS || true )
echo "stage: ok ($(cd "$OUT" && ls | tr '\n' ' '))"
