#!/usr/bin/env bash
# $Id: make-archive 194 2026-10-12 11:57:48Z peter $
#
# Copyright (C) 2026 Péter Felecan
# Assisted by Claude Sonnet 5.5 (Anthropic)
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Creates the project's distribution archive (packpay-VERSION.tar.xz).
#
# This is this project's own real, PRIMARY delivery mechanism --
# Péter's own, direct, explicit confirmation, 2026-09-14 (a real
# switch away from this, to automake's own `make dist-xz`, made
# 2026-09-10/11 while fixing an unrelated, genuine
# dist_doc_DATA/EXTRA_DIST omission, was reverted the same day it
# was flagged: that switch nested every shipped file one level down,
# inside a real packpay-VERSION.VERSION/ directory, which this
# project's own real, established convention does not do). Every
# shipped file sits at the real archive's own top level -- see
# .tar-exclude for the real, authoritative list of what a real
# delivery omits.
#
# Run as the final step, after bootstrap/configure/make pdf html (to
# verify the docs build) and make maintainer-clean (to get back to a
# pristine tree) --- this script does not itself build or clean
# anything.

set -e

cd "$(dirname "$0")"

# Same caution as dudhya's own make-archive (which this was extracted
# from): a prior project built with this same toolchain lost an
# executable bit on a shipped script during an edit, unnoticed until
# a delivered archive was extracted. Check each real script is still
# executable before packaging.
scripts_to_check="bootstrap fetch-kicad-demos man2htmldoc netlist.py kicad_gen.py auto_layout.py kicad_layout.py footprint.py render.py breadboard_lib.py breadboard_config.py example_pins.py build_instructions.py ppnetlist pprender ppfootprint ppauto_layout ppbreadboard_config ppbuild_instructions ppexample_circuit1 ppexample_circuit2 pprender_footprints ppvisualize pptest_netlist pptest_breadboard_config pptest_build_instructions ppmeasure_labels ppmeasure_value_text ppparse_instructions ppresolve_components ppkicad_layout ppextract_sources pptest_regression"
for script in ${scripts_to_check}
do
	if [ -e "${script}" ] && [ ! -x "${script}" ]
	then
		echo "make-archive: ${script} is not executable, aborting" >&2
		exit 1
	fi
done

version="$(sed -n '2p' VERSION)"
archive="packpay-${version}.tar.xz"

# Write to a temp file OUTSIDE the directory being archived, then
# move it into place -- same reasoning as dudhya's own make-archive:
# writing the growing output file directly into "." (even with
# --exclude) makes the directory's own metadata change while tar is
# still mid-scan of that same directory.
tmp_archive="$(mktemp "${TMPDIR:-/tmp}/packpay-archive-XXXXXX.tar.xz")"
rm -f "${archive}"
tar cJf "${tmp_archive}" --exclude-from=.tar-exclude .

# A stray file in the tree is archived like any other: the tree IS what is shipped. Refuse any top-level entry that Makefile.am does not name
# (ADDED 2026-10-11, after audio-jack-symbols/, left in the tree by ppextract_sources, rode along in the 0.72.54 archive and only the layout
# check of the delivery noticed). configure.ac is automake's own input and is not named there. check_make_archive_refuses_strays tests this.
strays=""
for name in $(tar tf "${tmp_archive}" | sed -e 's|^\./||' -e 's|/.*||' | sort -u)
do
	[ -n "${name}" ] || continue
	[ "${name}" = "configure.ac" ] && continue
	grep -qwF -- "${name}" Makefile.am || strays="${strays} ${name}"
done
if [ -n "${strays}" ]
then
	rm -f "${tmp_archive}"
	echo "make-archive: not named in Makefile.am (a stray file in the tree?):${strays}" >&2
	exit 1
fi

mv "${tmp_archive}" "${archive}"

echo "wrote ${archive}"

# Local Variables:
# mode: shell-script
# End:
