#!/bin/sh
# Build script for xlibre-xserver (Meson, Template B) — Xvfb-only headless build.
#
# THE crux port of the desktop initiative. Builds ONLY the Xvfb DDX (headless
# software framebuffer): no hardware/DRM/GL, no udev/logind, no xorg/xephyr/etc.
# x11vnc (Task 7a) later bridges the Xvfb display to VNC.
#
# XLibre has BSD CI but no illumos/Hammerhead target, so this port carries
# Hammerhead portability patches in patches/ (applied below).
#
# Meson option notes (verified against this tree's meson_options.txt):
#   * -Dgbm / -Dxf86-input-null from the original manifest capture do NOT
#     exist as options in 25.1.8 and are intentionally dropped (gbm is only
#     ever probed as required:false, and the null input driver isn't an
#     option here). Intent preserved: Xvfb on, all hardware/GL/DRM off.
#   * -Dsha1=libcrypto uses base OpenSSL's libcrypto (find_library('crypto')
#     resolves /usr/lib/libcrypto.so; <openssl/sha.h> in base /usr/include).
#   * libdrm/pciaccess/epoxy/gbm are all probed required:false in meson.build,
#     so the -D*=false switches don't trip a hard missing-dependency error on
#     Hammerhead (none of those libs exist here). Verified by source read.
set -e

# Resolve the port dir (holds patches/) BEFORE cd'ing into the extracted source.
PORTDIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)

cd "$SRCDIR"

# --- Hammerhead/illumos portability patches (rationale in each patch header) ---
for p in "$PORTDIR"/patches/*.patch; do
    [ -f "$p" ] || continue
    echo "==> Applying patch: $(basename "$p")"
    patch -p1 < "$p"
done

export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PREFIX/share/pkgconfig:/usr/lib/pkgconfig:$PKG_CONFIG_PATH"
# Template B illumos flags: __EXTENSIONS__ + _POSIX_PTHREAD_SEMANTICS select the
# XPG/POSIX prototypes (msg_control, *_r, etc.). coral's default CFLAGS is only
# "-O2 -pipe" and its LDFLAGS is just "-R$PREFIX/lib", so add the rest here.
export CFLAGS="${CFLAGS} -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS"
export LDFLAGS="${LDFLAGS} -L$PREFIX/lib -Wl,-R,$PREFIX/lib"

meson setup build \
    --prefix="$PREFIX" \
    --libdir=lib \
    --buildtype=release \
    -Dxvfb=true \
    -Dxorg=false -Dxephyr=false -Dxnest=false -Dxfbdev=false -Dxwin=false -Dxquartz=false \
    -Dglamor=false -Dglx=false -Dglx_dri=false \
    -Ddri1=false -Ddri2=false -Ddri3=false -Ddrm=false \
    -Dudev=false -Dudev_kms=false -Dhal=false \
    -Dsystemd_logind=false -Dsystemd_notify=false -Dseatd_libseat=false \
    -Dsuid_wrapper=false -Dxcsecurity=false -Dxselinux=false \
    -Dpciaccess=false -Dagp=false -Dint10=false -Dvgahw=false \
    -Dxf86bigfont=false -Dtests=false \
    -Ddocs=false -Ddevel-docs=false -Ddocs-pdf=false \
    -Dxf86-input-inputtest=false \
    -Ddtrace=false -Dlinux_acpi=false -Dlinux_apm=false \
    -Dsha1=libcrypto \
    -Dxkb_dir=/usr/local/share/X11/xkb \
    -Dxkb_bin_dir=/usr/local/bin \
    -Dxkb_output_dir=/var/lib/xkb

ninja -C build -j${JOBS:-1}
DESTDIR="$PKGDIR" ninja -C build install
