#!/bin/sh
# Build script for xlibre-xserver-xorg (Meson, Template B) — the Xorg DDX.
#
# Sibling of base/xlibre-xserver (Xvfb-only). Same XLibre 25.1.8 source, same
# os-layer portability patches, but -Dxorg=true so it produces
# /usr/local/bin/Xorg — the server xf86-video-illumosfb (Phase-2 M2) plugs into.
#
# Patches (patches/, applied below):
#   0001 os/meson.build       - Solaris socket defines under Hammerhead sysname
#   0002 os/ospoll.c          - illumos event-ports listener re-arm fix
#   0003 os-support/meson.build - select the solaris console backend for
#                                 Hammerhead (else it falls to the no-op stub;
#                                 the solaris arm is the load-bearing
#                                 /dev/console + VT_SETDISPINFO layer)
#
# Meson option notes (verified against this tree's meson_options.txt):
#   * -Dgbm / -Dxf86-input-null are NOT options in 25.1.8 (dropped, same as the
#     Xvfb port). glamor_egl is gated on gbm_dep.found(); with no gbm on
#     Hammerhead it's skipped. Intent preserved: Xorg on; DRM/GL/udev/hal off.
#   * -Dpciaccess=false: illumosfb does no PCI probing. hw/xfree86/meson.build
#     and os-support/meson.build gate all pciaccess use on get_option('pciaccess'),
#     so bus/Pci.c and the pciaccess dependency are skipped cleanly. If the DDX
#     turns out to hard-require it, build base/libpciaccess and flip to true.
#   * -Dsha1=libcrypto uses base OpenSSL libcrypto (as the Xvfb port).
#   * -Dglx=false: glx/meson.build hard-requires the 'gl' (libGL/Mesa) dep,
#     which Hammerhead has no port of. GLX is server-side OpenGL indirect
#     rendering; the framebuffer illumosfb driver (M2) does no GL, so GLX is
#     dead weight. Disabled (matches the Xvfb port). Revisit if/when Mesa lands.
#   * linker_export_flags: on Hammerhead host_machine.system() != 'sunos', so
#     hw/xfree86/meson.build falls to the GNU-ld `-Wl,--export-dynamic` branch —
#     which is exactly what we want so Xorg modules resolve xf86* symbols back
#     into the server. (The 'sunos' branch sets empty flags for the illumos ld;
#     we use GNU ld, so we deliberately do NOT match it here.)
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 \
    -Dxorg=true -Dxvfb=true \
    -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
