#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")"

echo "Cleaning top-level generated build outputs..."

rm -rf \
  autom4te.cache \
  .libs \
  modules

rm -f \
  Makefile \
  Make.rules \
  config.log \
  config.status \
  config.cache \
  config.nice \
  libtool \
  stamp-h1

echo "Cleaning generated subdir Makefiles..."

rm -f \
  lib/Makefile \
  man/Makefile \
  util/Makefile \
  sutil/Makefile \
  ipxdump/Makefile \
  ipx-1.0/Makefile \
  ipx-1.0/Samples/Makefile \
  contrib/pam/Makefile \
  contrib/tcl-utils/Makefile \
  contrib/tknwmsg/Makefile \
  contrib/tests/Makefile \
  contrib/testing/Makefile \
  contrib/testing/pp/Makefile \
  intl/Makefile \
  po/Makefile \
  po/Makefile.in \
  po/POTFILES

echo "Cleaning PHP phpize build outputs, keeping contrib/php/configure..."

rm -rf \
  contrib/php/autom4te.cache \
  contrib/php/.deps \
  contrib/php/.libs \
  contrib/php/modules \
  contrib/php/libs

rm -f \
  contrib/php/Makefile \
  contrib/php/Makefile.fragments \
  contrib/php/Makefile.objects \
  contrib/php/Makevars \
  contrib/php/config.h \
  contrib/php/config.log \
  contrib/php/config.status \
  contrib/php/config.cache \
  contrib/php/config.nice \
  contrib/php/config_vars.mk \
  contrib/php/libs.mk \
  contrib/php/libtool \
  contrib/php/run-tests.php \
  contrib/php/*.lo \
  contrib/php/*.la \
  contrib/php/*.o \
  contrib/php/*.dep \
  contrib/php/*.slo

echo "Cleaning object files, binaries, generated manpages, backups..."

find . \
  \( -name '*.o' \
     -o -name '*.do' \
     -o -name '*.to' \
     -o -name '*.lo' \
     -o -name '*.la' \
     -o -name '*.a' \
     -o -name '*.so' \
     -o -name '*.so.*' \
     -o -name '*.dep' \
     -o -name '*.gcno' \
     -o -name '*.gcda' \
     -o -name '*.orig' \
     -o -name '*.rej' \
     -o -name '*~' \) \
  -delete

find man -name '*.gz' -delete 2>/dev/null || true
find ipx-1.0 -name '*.gz' -delete 2>/dev/null || true

echo "Cleaning known built binaries..."

rm -f \
  sutil/ncplogin \
  sutil/ncpmap \
  sutil/ncpmount \
  sutil/ncpumount \
  sutil/nwsfind

rm -f \
  util/ncopy \
  util/ncptest \
  util/nprint \
  util/nsend \
  util/nwauth \
  util/nwbocreate \
  util/nwbols \
  util/nwboprops \
  util/nwborm \
  util/nwbpadd \
  util/nwbpcreate \
  util/nwbprm \
  util/nwbpset \
  util/nwbpvalues \
  util/nwdir \
  util/nwdpvalues \
  util/nwfsctrl \
  util/nwfsinfo \
  util/nwfstime \
  util/nwgrant \
  util/nwlistsalvage \
  util/nwmsg \
  util/nwpasswd \
  util/nwpjmv \
  util/nwpqjob \
  util/nwpurge \
  util/nwrevoke \
  util/nwrights \
  util/nwsalvage \
  util/nwtrustee \
  util/nwtrustee2 \
  util/nwuserlist \
  util/nwvolinfo \
  util/pqlist \
  util/pqstat \
  util/pserver \
  util/slist

rm -f \
  ipxdump/ipxdump \
  ipxdump/ipxparse \
  ipx-1.0/ipx_cmd \
  ipx-1.0/ipx_configure \
  ipx-1.0/ipx_interface \
  ipx-1.0/ipx_internal_net \
  ipx-1.0/ipx_route \
  ipx-1.0/Samples/ipxrcv \
  ipx-1.0/Samples/ipxsend \
  ipx-1.0/Samples/rip \
  ipx-1.0/Samples/sap

echo "Restoring required release files if needed..."

if [ ! -f lib/libncp.vers ]; then
  if git cat-file -e HEAD:lib/libncp.vers 2>/dev/null; then
    git checkout -- lib/libncp.vers
  elif git cat-file -e v2.2.6:lib/libncp.vers 2>/dev/null; then
    git show v2.2.6:lib/libncp.vers > lib/libncp.vers
  else
    echo "Warning: lib/libncp.vers is missing and could not be restored."
  fi
fi

echo
echo "Cleanup done."
echo
echo "Remaining untracked/modified files:"
git status --short
