#!/bin/sh
# Build script for fox-toolkit / FOX 1.6.59 (autotools, Template A + C++).
# Older C++ codebase (pre-C++11 idioms) - watch illumos __EXTENSIONS__ /
# socket-header issues same as any client-stack C port, but this time also
# apply the flags to CXXFLAGS since FOX is a C++ library.
set -e
cd "$SRCDIR"

# MANDATORY libtool fix - see Template A.
if [ -f configure ]; then
    sed -i -E 's/(kopensolaris\*-gnu)([^)]*\))/\1 | hammerhead*\2/' configure
fi

# GOTCHA #1: FOX's configure.ac does `CXXFLAGS=""` unconditionally near the
# top and rebuilds it from scratch with its own hardcoded flag set - any
# CXXFLAGS/CFLAGS we export before running configure is silently discarded
# (confirmed by inspecting configure.ac: "CXXFLAGS=\"\"" then a chain of
# CXXFLAGS="${CXXFLAGS} ..." appends, never re-adding a user value).
# GOTCHA #2: within that rebuild, `XFTCFLAGS="-I/usr/include/freetype2"` is
# hardcoded (no pkg-config/freetype-config probe), so <ft2build.h> is never
# found under our $PREFIX=/usr/local.
# WORKAROUND: src/Makefile.am declares `CXXFLAGS = @CXXFLAGS@ @X_CFLAGS@`
# with no explicit CPPFLAGS reference, but automake's generated compile
# rule ALWAYS appends $(CPPFLAGS) regardless (it's a standard AC_SUBST'd
# variable automake wires in implicitly) - and configure.ac never touches
# CPPFLAGS at all. So route our include path through CPPFLAGS instead of
# CXXFLAGS/CFLAGS - it survives the configure.ac reset untouched.
# GOTCHA #3: FOX's include/xincs.h has a ready-made
# `#ifdef HAVE_SYS_FILIO_H #include <sys/filio.h> #endif` (for FIONREAD on
# Solaris/illumos) but configure.ac never actually probes for
# sys/filio.h/AC_DEFINE's HAVE_SYS_FILIO_H - it's a dead branch upstream.
# illumos DOES have <sys/filio.h> (confirmed present on Hammerhead) so just
# define the macro ourselves; FXGUISignal.cpp otherwise fails with
# "'FIONREAD' was not declared in this scope".
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PREFIX/share/pkgconfig:/usr/lib/pkgconfig:$PKG_CONFIG_PATH"
export CPPFLAGS="${CPPFLAGS} -I$PREFIX/include -I$PREFIX/include/freetype2 -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS -DHAVE_SYS_FILIO_H"
export LDFLAGS="${LDFLAGS} -L$PREFIX/lib -R$PREFIX/lib"

# No GL in this build (kernel has no DRM/KMS/GPU accel - see graphics
# capability baseline memory); tiff not ported, so disabled explicitly.
./configure \
    --prefix="$PREFIX" \
    --build="$BUILD_TRIPLE" \
    --host="$BUILD_TRIPLE" \
    --sysconfdir="$SYSCONFDIR" \
    --disable-static \
    --with-opengl=no \
    --with-xft=yes \
    --with-xrandr=yes \
    --with-xcursor=yes \
    --with-xrender=yes \
    --with-xfixes=yes \
    --with-xshm=yes \
    --disable-tiff

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