#!/usr/bin/env bash
# Compile-check Vanta packages on Zo against the AOSP 17 Kotlin/Compose versions.
# Usage: tools/shadow/build.sh [Pkg ...]   (no args = every package)
set -uo pipefail
P=$(cd "$(dirname "$0")/../.." && pwd)
OUT=$(python3 "$P/tools/shadow/gen.py" "$@" | tail -1) || exit 1
cd "$OUT" || exit 1
GRADLE_VERSION=${SHADOW_GRADLE:-8.14.3}
# AGP 8.12 needs a newer Gradle than the host's; make the wrapper in an empty
# directory (the generated project cannot be configured by the old one).
WR="$P/build/shadow-wrapper-$GRADLE_VERSION"
if [ ! -x "$WR/gradlew" ]; then
  mkdir -p "$WR" && (cd "$WR" && : > settings.gradle && gradle -q wrapper --gradle-version "$GRADLE_VERSION") \
    || { echo "shadow: cannot create a Gradle $GRADLE_VERSION wrapper"; exit 1; }
fi
cp -r "$WR/gradlew" "$WR/gradle" .
tasks=()
for d in */; do d=${d%/}; [ -f "$d/build.gradle.kts" ] && tasks+=(":$d:compileDebugKotlin"); done
./gradlew -q --continue "${tasks[@]}" > build.log 2>&1
rc=$?
grep -E '^e: |error:|FAILED|What went wrong' -A3 build.log | head -400
[ $rc = 0 ] && echo "shadow: compiled ${#tasks[@]} modules" || echo "shadow: FAILED"
exit $rc
