#!/bin/sh
# Build script for x11vnc (autotools, Template A variant).
#
# GOTCHA (Task 7a, 2026-07-22): the GitHub tag tarball ships ONLY
# configure.ac/Makefile.am - no pre-generated `configure` at all (confirmed
# via `tar tf`: configure.ac + autogen.sh present, no top-level `configure`,
# no Makefile.in). Must autoreconf before the usual libtool-fix + configure
# steps; the libtool `kopensolaris*-gnu` sed fix is applied to the
# autoreconf-GENERATED configure, same as every other port.
#
# GOTCHA (Task 7a): x11vnc's own configure.ac hard PKG_CHECK_MODULES()s for
# `libvncserver >= 0.9.8` and `libvncclient >= 0.9.8` - this project split
# off the LibVNCServer repo in 2016 and is no longer a bundled/standalone
# VNC codebase (see base/libvncserver/package.toml header). Both must be
# built+installed first (base/libvncserver, this task).
#
# illumos socket libs: configure.ac already does
# AC_CHECK_LIB(nsl,gethostbyname) / AC_CHECK_LIB(socket,socket)
# unconditionally, so -lsocket/-lnsl get added to LIBS automatically by
# autoconf's own probe - no manual LDFLAGS needed for those (unlike the
# CMake-based libvncserver, which required them explicitly).
#
# No --with-jpeg option exists in this x11vnc release's configure.ac (only
# libvncserver's own jpeg support matters - already baked in via
# WITH_JPEG=ON there); the brief's "--with-jpeg=/usr/local" suggestion does
# not apply here and is intentionally omitted.
#
# GOTCHA (Task 7a): autoreconf's internal `automake --add-missing --copy
# --no-force` step reads autoconf's macro trace via `$ENV{AUTOCONF} ||
# 'true'` (automake's scan_autoconf_traces, /usr/bin/automake ~line 5308)
# - when run non-interactively over ssh AUTOCONF is unset, so automake
# silently traces with the no-op `true` command instead of real autoconf,
# sees zero AM_INIT_AUTOMAKE/AC_CONFIG_FILES hits, and fails with the
# misleading "no proper invocation of AM_INIT_AUTOMAKE was found" /
# "no Makefile.am found for any configure output" errors - even though
# configure.ac is completely correct. Fix: export AUTOCONF explicitly
# before autoreconf so it propagates to automake's sub-invocation.
set -e
cd "$SRCDIR"

export AUTOCONF=/usr/bin/autoconf
export AUTOHEADER=/usr/bin/autoheader
export ACLOCAL=/usr/bin/aclocal
export AUTOM4TE=/usr/bin/autom4te

export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PREFIX/share/pkgconfig:$PKG_CONFIG_PATH"
export CFLAGS="${CFLAGS} -I$PREFIX/include -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS"
export LDFLAGS="${LDFLAGS} -L$PREFIX/lib -R$PREFIX/lib"

test -d m4 || mkdir m4
autoreconf -v --install

# GOTCHA (Task 7a): coral's pre-build step copies the hammerhead-patched
# config.sub/config.guess into any config.sub/config.guess ALREADY present
# in $SRCDIR before build.sh runs - but this port ships NEITHER file (no
# pre-generated configure at all), so that copy is a no-op. autoreconf
# --install then generates its OWN fresh config.sub/config.guess (pulled
# from automake's install-missing machinery), which do NOT carry the
# hammerhead patch, causing `configure: error: ... OS 'hammerhead' not
# recognized`. Re-copy the patched versions here, post-autoreconf.
cp -f /usr/share/autoconf/build-aux/config.sub config.sub 2>/dev/null || true
cp -f /usr/share/autoconf/build-aux/config.guess config.guess 2>/dev/null || true

if [ -f configure ]; then
    sed -i -E 's/(kopensolaris\*-gnu)([^)]*\))/\1 | hammerhead*\2/' configure
fi

./configure \
    --prefix="$PREFIX" \
    --build="$BUILD_TRIPLE" \
    --host="$BUILD_TRIPLE" \
    --sysconfdir="$SYSCONFDIR" \
    --disable-static \
    --with-x \
    --x-includes="$PREFIX/include" \
    --x-libraries="$PREFIX/lib" \
    --without-avahi \
    --with-ssl="/usr"

gmake -j${JOBS:-1}
gmake install DESTDIR="$PKGDIR"
