#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
SOURCE_DIR="$ROOT_DIR/source/libass-android"
NATIVE_DIR="$SOURCE_DIR/lib_ass/src/main/cpp/libass-cmake/src"

for command in java ninja autoconf automake autoreconf make pkg-config; do
  command -v "$command" >/dev/null || { echo "Missing build tool: $command" >&2; exit 1; }
done
if command -v glibtoolize >/dev/null 2>&1; then
  export LIBTOOLIZE=glibtoolize
elif command -v libtoolize >/dev/null 2>&1; then
  export LIBTOOLIZE=libtoolize
else
  echo "Missing libtoolize/glibtoolize" >&2
  exit 1
fi
command -v autopoint >/dev/null || { echo "Missing build tool: autopoint" >&2; exit 1; }

(cd "$NATIVE_DIR/unibreak" && ./autogen.sh && make distclean)
(cd "$NATIVE_DIR/fribidi" && ./autogen.sh && make distclean)
(cd "$NATIVE_DIR/fontconfig" && ./autogen.sh && make distclean)
(cd "$NATIVE_DIR/expat/expat" && ./buildconf.sh)
(cd "$NATIVE_DIR/ass" && ./autogen.sh)
(cd "$SOURCE_DIR" && ./gradlew :lib_ass:assembleRelease :lib_ass_kt:assembleRelease :lib_ass_media:assembleRelease)

echo "AARs are under source/libass-android/*/build/outputs/aar/"
