#!/bin/sh
# Build script for unzip (Info-ZIP hand-rolled unix/Makefile, not
# autotools/cmake/meson). Upstream's own auto-detect step ("flags" target)
# runs a battery of trial one-line compiles with a bare $CC and, on
# Hammerhead's GCC 14 (which makes -Wimplicit-function-declaration a hard
# error by default), every one of those trial compiles fails - so the
# detector falls back to "unavailable" for fchmod/fchown/lchown/
# nl_langinfo/dirent/valloc/etc, which then generates broken substitute
# macros (e.g. redefining opendir()/readdir() in terms of fopen()/fread())
# that don't even compile. Fix: run the "flags" detection step itself with
# -Wno-implicit-function-declaration and -D__EXTENSIONS__ so the trial
# compiles succeed and the real capabilities get detected correctly, then
# build with that same flags file.
set -e
cd "$SRCDIR"

# NOTE: coral runs build.sh under zsh, not /bin/sh despite the shebang -
# zsh's nomatch aborts on a glob with no match (see project gotcha:
# never use bare globs under zsh). find+xargs is glob-match-safe.
find . -maxdepth 1 -name "*.o" -delete
rm -f flags conftest conftest.c unzip funzip unzipsfx zipgrep

gmake -f unix/Makefile flags CC="gcc -Wno-implicit-function-declaration -D__EXTENSIONS__"

eval gmake -f unix/Makefile unzips ACONF_DEP=flags `cat flags`

mkdir -p "$PKGDIR$PREFIX/bin" "$PKGDIR$PREFIX/share/man/man1"
install -m 755 unzip funzip unzipsfx "$PKGDIR$PREFIX/bin/"
[ -f unzip.1 ] && install -m 644 unzip.1 "$PKGDIR$PREFIX/share/man/man1/" || true
[ -f funzip.1 ] && install -m 644 funzip.1 "$PKGDIR$PREFIX/share/man/man1/" || true
