#!/usr/bin/env bash
# Build Photoyam's LibRaw WebAssembly module (camera-RAW decoding).
#
#   Output: src/vendor/libraw/libraw.mjs   Emscripten ES-module loader (glue)
#           src/vendor/libraw/libraw.wasm  LibRaw + zlib + libjpeg + libraw_glue.cpp (beside this script)
#
# This script and libraw_glue.cpp are published with the site at
# https://photoyam.com/source/libraw/README.txt (the "source + build script" the LGPL asks for).
#
# LibRaw is dual-licensed LGPL-2.1 / CDDL-1.0; we distribute it under the LGPL-2.1.
# The .wasm is a separate file loaded at run time (only when a RAW file is opened),
# so anyone can rebuild it with this script, a different LibRaw version or their
# own changes, and drop the result in place of the served file — the
# WebAssembly equivalent of relinking a shared library (LGPL-2.1 section 6).
#
# Requirements: bash, git, and the Emscripten SDK on PATH (emcc). Tested with
# Emscripten 4.0.10 on Windows (Git Bash) and Linux:
#   git clone https://github.com/emscripten-core/emsdk.git && cd emsdk
#   ./emsdk install 4.0.10 && ./emsdk activate 4.0.10 && source ./emsdk_env.sh
# Then, from the repository root:
#   bash public/source/libraw/build.sh
# Outside the Photoyam repository, set OUT=<dir> to choose where the files go.
#
# No threads (no SharedArrayBuffer → works without cross-origin isolation, which
# the site cannot enable without breaking third-party embeds), no SIMD (older
# Safari), native wasm exceptions (LibRaw throws internally; Safari 15.2+).
# Not compiled in: the GPL demosaic packs, RawSpeed, the Adobe DNG SDK, LCMS.
set -euo pipefail

LIBRAW_TAG="${LIBRAW_TAG:-0.22.1}"
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$HERE/../../.." && pwd)"
WORK="${LIBRAW_WORK:-$ROOT/.libraw-build}"
OUT="${OUT:-$ROOT/src/vendor/libraw}"
SRC="$WORK/LibRaw-$LIBRAW_TAG"

command -v emcc >/dev/null || { echo "emcc not found: activate the Emscripten SDK first" >&2; exit 1; }
mkdir -p "$WORK" "$OUT"
if [ ! -d "$SRC" ]; then
  git clone --branch "$LIBRAW_TAG" --depth 1 https://github.com/LibRaw/LibRaw.git "$SRC"
fi

# The library sources exactly as LibRaw's own Makefile.am lists them.
SOURCES=$(tr -d '\r' < "$SRC/Makefile.am" | sed -n '/^lib_libraw_a_SOURCES/,/^$/p' | tr -s ' \t\\' '\n' | grep '\.cpp$' | sed "s#^#$SRC/#")

DEFS="-DLIBRAW_NOTHREADS -DUSE_ZLIB -DUSE_JPEG -DUSE_JPEG8 -DNO_LCMS"
CFLAGS="-O3 -fwasm-exceptions -sUSE_ZLIB=1 -sUSE_LIBJPEG=1 -w -I$SRC"

OBJ="$WORK/obj-$LIBRAW_TAG"
mkdir -p "$OBJ"
OBJS=()
for s in $SOURCES "$HERE/libraw_glue.cpp"; do
  o="$OBJ/$(echo "${s#$ROOT/}" | tr '/\:' '___').o"
  if [ ! -f "$o" ] || [ "$s" -nt "$o" ]; then
    echo "  CXX $(basename "$s")"
    emcc $CFLAGS $DEFS -c "$s" -o "$o" &
    # keep up to 8 compiles in flight
    while [ "$(jobs -rp | wc -l)" -ge 8 ]; do wait -n; done
  fi
  OBJS+=("$o")
done
wait

echo "  LINK libraw.mjs + libraw.wasm"
emcc -O3 -fwasm-exceptions -sUSE_ZLIB=1 -sUSE_LIBJPEG=1 \
  -sMODULARIZE=1 -sEXPORT_ES6=1 -sEXPORT_NAME=createLibRaw \
  -sENVIRONMENT=web,worker,node \
  -sALLOW_MEMORY_GROWTH=1 -sINITIAL_MEMORY=64MB -sMAXIMUM_MEMORY=4GB \
  -sSTACK_SIZE=1MB \
  -sEXPORTED_FUNCTIONS=_malloc,_free \
  -sEXPORTED_RUNTIME_METHODS=HEAPU8,HEAPU16,HEAPF64,UTF8ToString \
  -sFILESYSTEM=0 -sINCOMING_MODULE_JS_API=locateFile,wasmBinary,instantiateWasm \
  "${OBJS[@]}" -o "$OUT/libraw.mjs"

ls -l "$OUT"
echo "LibRaw $LIBRAW_TAG built into $OUT"
