#!/bin/sh
# Build script for openbox (autotools, Template A).
# Deps: glib2, pango, cairo, libxml2 (BASE - /usr/lib), libX11, libXext,
# libXrender, libXrandr, libXinerama, libXcursor, libSM, libICE.
# Optionals disabled to stay minimal (no imlib2/librsvg/startup-notification
# ports exist).
set -e
cd "$SRCDIR"

# GOTCHA (Template A, standard): this release's libtool-generated `configure`
# has no solaris/illumos/hammerhead arm in its dynamic-linker detection
# `case $host_os in ...` block - unmatched OS falls through to
# `*) dynamic_linker=no ;;` which forces can_build_shared=no (static-only
# .a, no .so). Patch the case arm to add hammerhead* alongside the generic
# glibc/ELF branch (same SONAME/.so.N convention applies).
if [ -f configure ]; then
    sed -i -E 's/(kopensolaris\*-gnu)([^)]*\))/\1 | hammerhead*\2/' configure
fi

# libxml2 ships in base (/usr/lib), not under $PREFIX - make sure its .pc
# is on the search path alongside the usual $PREFIX locations.
export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PREFIX/share/pkgconfig:/usr/lib/pkgconfig:$PKG_CONFIG_PATH"
export CFLAGS="${CFLAGS} -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS -I$PREFIX/include"
export LDFLAGS="${LDFLAGS} -L$PREFIX/lib -Wl,-R,$PREFIX/lib -lsocket -lnsl"

./configure \
    --prefix="$PREFIX" \
    --build="$BUILD_TRIPLE" \
    --host="$BUILD_TRIPLE" \
    --sysconfdir="$SYSCONFDIR" \
    --disable-static \
    --disable-nls \
    --disable-imlib2 \
    --disable-librsvg \
    --disable-startup-notification

gmake -j${JOBS:-1}

# GOTCHA: openbox's automake-generated install-recursive races under a
# parallel MAKEFLAGS inherited from the environment - two subdirectory
# installs both `install-sh -c -d .../libexec` at once and the loser's
# mkdir sees EEXIST as a hard error (not the usual -p tolerant case).
# Force the install step serial regardless of inherited -j.
gmake -j1 install DESTDIR="$PKGDIR"
