#!/bin/sh
# Build script for unibilium (autotools, Template A variant + autoreconf).
#
# GOTCHA (1b-2): the release tarball ships configure.ac + a hand-written
# Makefile.in (no Makefile.am, no pre-generated `configure`) - must
# autoreconf before the usual libtool-fix + configure steps, same shape as
# base/x11vnc's autoreconf gotcha. autoreconf pulls in its OWN fresh
# config.sub/config.guess (coral's pre-build copy is a no-op here since
# neither file existed in $SRCDIR yet), so they must be re-copied
# post-autoreconf.
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 LIBTOOLIZE=/usr/bin/libtoolize

autoreconf -v --install

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

# MANDATORY libtool fix (Template A): unmatched OS in libtool's
# dynamic-linker case falls through to static-only.
if [ -f configure ]; then
    sed -i -E 's/(kopensolaris\*-gnu)([^)]*\))/\1 | hammerhead*\2/' configure
fi

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"

./configure \
    --prefix="$PREFIX" \
    --build="$BUILD_TRIPLE" \
    --host="$BUILD_TRIPLE" \
    --sysconfdir="$SYSCONFDIR" \
    --disable-static

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