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

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
source_root="$repo_root/build/dependencies-m1/source"
install_root="$repo_root/artifacts/m1"
log_root="$repo_root/build/dependencies-m1/logs"
mkdir -p "$install_root" "$log_root"

for tool in cmake ninja meson cargo rustc cargo-capi patch; do
  command -v "$tool" >/dev/null || { echo "$tool is required" >&2; exit 2; }
done
expected_cargo_c="cargo-c 0.10.24+cargo-0.98.0"
actual_cargo_c="$(cargo-capi -V)"
if [ "$actual_cargo_c" != "$expected_cargo_c" ]; then
  echo "cargo-c version mismatch: expected '$expected_cargo_c', found '$actual_cargo_c'" >&2
  exit 2
fi

export PATH="$install_root/bin:$PATH"
export PKG_CONFIG_PATH="$install_root/lib/pkgconfig"

echo "Building Catch2"
cmake -S "$source_root/catch2" -B "$repo_root/build/dependencies-m1/catch2" \
  -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$install_root" \
  -DCATCH_BUILD_TESTING=OFF 2>&1 | tee "$log_root/catch2-configure.log"
cmake --build "$repo_root/build/dependencies-m1/catch2" --parallel 2 2>&1 | tee "$log_root/catch2-build.log"
cmake --install "$repo_root/build/dependencies-m1/catch2" 2>&1 | tee "$log_root/catch2-install.log"

echo "Building libdovi/dovi_tool"
cd "$source_root/libdovi"
if ! grep -q "dovi_rpu_set_scene_refresh_flag" dolby_vision/src/capi.rs; then
  patch -p1 --forward < "$repo_root/dependencies/patches/libdovi-scene-refresh-capi.patch"
fi
cargo build --release --locked 2>&1 | tee "$log_root/libdovi-build.log"
mkdir -p "$install_root/bin"
cp target/release/dovi_tool.exe "$install_root/bin/dovi_tool.exe"
cd dolby_vision
cargo capi install --release --locked --prefix "$install_root" \
  2>&1 | tee "$log_root/libdovi-capi-install.log"

echo "Building libplacebo"
libplacebo_utils_gen="$source_root/libplacebo/src/vulkan/utils_gen.py"
if grep -q 'registry = VkXML(ET.parse(xmlfile))' "$libplacebo_utils_gen"; then
  sed -i 's/registry = VkXML(ET.parse(xmlfile))/registry = VkXML(ET.parse(xmlfile).getroot())/' "$libplacebo_utils_gen"
fi
libplacebo_build="$repo_root/build/dependencies-m1/libplacebo"
if [ -d "$libplacebo_build" ]; then
  meson setup --reconfigure "$libplacebo_build" "$source_root/libplacebo" \
    --buildtype=release --prefix="$install_root" -Dvulkan=enabled -Dopengl=disabled -Dvulkan-registry=/ucrt64/share/vulkan/registry/vk.xml \
    2>&1 | tee "$log_root/libplacebo-configure.log"
else
  meson setup "$libplacebo_build" "$source_root/libplacebo" \
    --buildtype=release --prefix="$install_root" -Dvulkan=enabled -Dopengl=disabled -Dvulkan-registry=/ucrt64/share/vulkan/registry/vk.xml \
    2>&1 | tee "$log_root/libplacebo-configure.log"
fi
meson compile -C "$libplacebo_build" -j 2 2>&1 | tee "$log_root/libplacebo-build.log"
meson install -C "$libplacebo_build" 2>&1 | tee "$log_root/libplacebo-install.log"

echo "Building Qt Base"
cd "$source_root/qt"
qt_install_root="$(cygpath -u "$install_root/qt")"
./configure -prefix "$qt_install_root" -release -opensource -confirm-license \
  -nomake tests -nomake examples 2>&1 | tee "$log_root/qt-configure.log"
cmake --build . --parallel 8 2>&1 | tee "$log_root/qt-build.log"
cmake --install . 2>&1 | tee "$log_root/qt-install.log"

echo "Support dependency build stage complete: $install_root"
