ajhahn.de
← eeco
Shell 72 lines
#!/bin/sh
# Generates the eeco.1 Unix man page from docs/USAGE.md.
#
# Strips the cross-repo-fingerprint HTML logo header and the Prev/Next
# nav footer (both render badly in roff), then pipes the cleaned
# Markdown through go-md2man. The two strip patterns mirror the Go
# implementation at internal/guide/render.go (stripTopHTMLBlock +
# stripTrailingNavFooter) but stay independent so the slice does not
# add a frozen-surface dependency.
#
# Run from any cwd; resolves the repo root from this script's location.
# Output: dist/eeco.1. Pinned go-md2man version keeps CI byte-stable.
set -eu

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
DIST="${ROOT}/dist"
SRC="${ROOT}/docs/USAGE.md"

if [ ! -f "${SRC}" ]; then
	echo "error: ${SRC} not found" >&2
	exit 1
fi

mkdir -p "${DIST}"

# Title metadata for the `.TH` roff header. Without these inputs
# go-md2man emits an empty header so `man eeco` prints `()` for
# name(section). Mirrors gen-packaging.sh's VERSION resolution.
VERSION="${VERSION:-$(git -C "${ROOT}" describe --tags --dirty --always 2>/dev/null || echo dev)}"
MAN_DATE="${MAN_DATE:-$(date -u +%Y-%m-%d)}"

TMP_MD="$(mktemp "${DIST}/eeco.usage.XXXXXX.md")"
trap 'rm -f "${TMP_MD}"' EXIT

# Strip:
#   1. Top HTML block: from `<div align="center">` through the first
#      standalone `---` that follows it.
#   2. Trailing nav footer: a `---` followed (after optional blanks) by
#      a line matching `[← Prev: …]` or `[Next: …]`, sitting at EOF.
awk -v ver="${VERSION}" -v mdate="${MAN_DATE}" '
	BEGIN {
		# Prepend a proper H1 so go-md2man emits a .TH header with
		# name, section, date, source, and manual fields.
		printf "# eeco 1 \"%s\" \"%s\" \"User Commands\"\n\n", mdate, ver
	}
	/^<div align="center">$/ { in_top = 1; next }
	in_top && /^---$/        { in_top = 0; next }
	in_top                   { next }
	{ buf[++n] = $0 }
	END {
		last = n
		while (last > 0 && buf[last] ~ /^[[:space:]]*$/) last--
		if (last > 0 && (buf[last] ~ /Prev:/ || buf[last] ~ /Next:/)) {
			cut = last - 1
			while (cut > 0 && buf[cut] ~ /^[[:space:]]*$/) cut--
			if (cut > 0 && buf[cut] == "---") cut--
			last = cut
		}
		for (i = 1; i <= last; i++) print buf[i]
	}
' "${SRC}" > "${TMP_MD}"

# Pin go-md2man so CI is reproducible (golangci-lint precedent at
# Makefile:60-61). Run via `go run` so no separate install step is
# needed — actions/setup-go already provides the toolchain.
MD2MAN_VERSION="${MD2MAN_VERSION:-v2.0.5}"
go run "github.com/cpuguy83/go-md2man/v2@${MD2MAN_VERSION}" \
	-in "${TMP_MD}" -out "${DIST}/eeco.1"

printf 'wrote:\n  %s\n' "${DIST}/eeco.1"