#!/usr/bin/env bash
# Builds the GPL-3 LibRaw + librtprocess decode helper (master tier).
# OpenMP is built from pinned LLVM source so every shipped binary honors the
# same explicit macOS deployment target instead of inheriting the build host.
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
source "$ROOT/tools/macos_build_settings.sh"

TP="$ROOT/native/third_party"
BUILD_DIR="$ROOT/build/libraw-decoder"
VENDOR_BUILD_DIR="$ROOT/build/vendor"
OUT="$BUILD_DIR/filmbuddy-raw-decode-libraw"
RTPROCESS_BUILD_DIR="$VENDOR_BUILD_DIR/librtprocess-macos${FILMBUDDY_MACOS_DEPLOYMENT_TARGET}-${FILMBUDDY_BUILD_ARCH}"
LIBRAW_STAMP="$BUILD_DIR/libraw-macos${FILMBUDDY_MACOS_DEPLOYMENT_TARGET}-${FILMBUDDY_BUILD_ARCH}.stamp"
LIBOMP_PREFIX="$($ROOT/tools/build_compatible_libomp.sh)"
LIBOMP_DYLIB="$LIBOMP_PREFIX/lib/libomp.dylib"
BUILD_JOBS="$(sysctl -n hw.logicalcpu 2>/dev/null || echo 8)"
MACOS_SDK="$(xcrun --sdk macosx --show-sdk-path)"

mkdir -p "$BUILD_DIR" "$VENDOR_BUILD_DIR"

if [[ ! -f "$TP/libraw/lib/libraw_r.a" || ! -f "$LIBRAW_STAMP" ]]; then
  echo "[libraw-decoder] building LibRaw for macOS $FILMBUDDY_MACOS_DEPLOYMENT_TARGET ..."
  (
    cd "$TP/libraw"
    make -f Makefile.dist clean >/dev/null 2>&1 || true
    # Upstream's dist makefile writes into these ignored directories but does
    # not create them. A clean Corresponding Source extraction has neither.
    mkdir -p object lib
    make -f Makefile.dist \
      CC="$(xcrun -find clang)" \
      CXX="$(xcrun -find clang++)" \
      CFLAGS="-O3 -I. -w -DUSE_ZLIB -isysroot $MACOS_SDK -arch $FILMBUDDY_BUILD_ARCH -mmacosx-version-min=$FILMBUDDY_MACOS_DEPLOYMENT_TARGET" \
      library -j"$BUILD_JOBS" >/dev/null
  )
  touch "$LIBRAW_STAMP"
fi

echo "[libraw-decoder] configuring librtprocess ..."
cmake -S "$TP/librtprocess" -B "$RTPROCESS_BUILD_DIR" \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_OSX_ARCHITECTURES="$FILMBUDDY_BUILD_ARCH" \
  -DCMAKE_OSX_DEPLOYMENT_TARGET="$FILMBUDDY_MACOS_DEPLOYMENT_TARGET" \
  -DBUILD_SHARED_LIBS=OFF \
  -DOPTION_OMP=ON \
  -DOpenMP_CXX_FLAGS="-Xclang;-fopenmp;-I$LIBOMP_PREFIX/include" \
  -DOpenMP_CXX_LIB_NAMES=omp \
  -DOpenMP_omp_LIBRARY="$LIBOMP_DYLIB" >/dev/null
cmake --build "$RTPROCESS_BUILD_DIR" -j"$BUILD_JOBS" >/dev/null

RTPROCESS_ARCHIVE="$RTPROCESS_BUILD_DIR/src/librtprocess.a"
if [[ ! -f "$RTPROCESS_ARCHIVE" ]]; then
  echo "error: librtprocess archive was not produced at $RTPROCESS_ARCHIVE" >&2
  exit 1
fi

echo "[libraw-decoder] compiling helper ..."
clang++ -std=c++20 -O2 \
  -arch "$FILMBUDDY_BUILD_ARCH" \
  -mmacosx-version-min="$FILMBUDDY_MACOS_DEPLOYMENT_TARGET" \
  -I "$TP/libraw" \
  -I "$TP/librtprocess/src/include" \
  "$ROOT/native/raw-decoder/main.cpp" \
  "$TP/libraw/lib/libraw_r.a" \
  "$RTPROCESS_ARCHIVE" \
  "$LIBOMP_DYLIB" \
  -framework Accelerate -framework CoreGraphics -framework ImageIO \
  -framework CoreFoundation \
  -Wl,-rpath,@executable_path \
  -Wl,-rpath,@executable_path/lib \
  -lz -o "$OUT"

# Re-home the compatible runtime next to the helper and use @rpath so the app
# has no Homebrew or local source-tree dependency on another Mac.
echo "[libraw-decoder] relinking libomp.dylib to @rpath ..."
cp -f "$LIBOMP_DYLIB" "$BUILD_DIR/libomp.dylib"
chmod +w "$BUILD_DIR/libomp.dylib"
install_name_tool -id "@rpath/libomp.dylib" "$BUILD_DIR/libomp.dylib"
install_name_tool -change "$LIBOMP_DYLIB" "@rpath/libomp.dylib" "$OUT"

# install_name_tool invalidates existing signatures. Ad-hoc signatures keep
# local development launches valid; distribution packaging replaces them.
codesign --force --sign - "$BUILD_DIR/libomp.dylib"
codesign --force --sign - "$OUT"

filmbuddy_verify_macos_binary "$BUILD_DIR/libomp.dylib"
filmbuddy_verify_macos_binary "$OUT"

echo "[libraw-decoder] built $OUT"
echo "[libraw-decoder] built $BUILD_DIR/libomp.dylib"
