#!/usr/bin/env bash # Validate every generated EPUB. # # 1. epubcheck — official EPUB3 conformance (always, required on PATH). # 2. kindlegen — Amazon's converter, the engine behind "Send to Kindle". # Optional but recommended: epubcheck does NOT catch the # Kindle-specific issues that cause "E999" delivery failures. # # epubcheck: brew install epubcheck # kindlegen: bundled inside Kindle Previewer 3 at # ".../Kindle Previewer 3.app/Contents/lib/fc/bin/kindlegen". # Put it on PATH or point KINDLEGEN at it to enable the Kindle check. # # Exits non-zero if any EPUB fails a check, so it can gate CI. set -uo pipefail if ! command -v epubcheck >/dev/null 2>&1; then echo "error: epubcheck not found. Install it (e.g. 'brew install epubcheck')." >&2 exit 127 fi KINDLEGEN="${KINDLEGEN:-$(command -v kindlegen 2>/dev/null || true)}" shopt -s nullglob epubs=(epubs/*.epub) if [ ${#epubs[@]} -eq 0 ]; then echo "error: no EPUBs in epubs/. Run 'zig build run' first." >&2 exit 1 fi fail=0 for f in "${epubs[@]}"; do ok=1 if ! epubcheck "$f" >/dev/null 2>&1; then ok=0 echo "FAIL (epubcheck) $f" epubcheck "$f" 2>&1 | grep -E '^(ERROR|FATAL)' | sed 's/^/ /' fi if [ -n "$KINDLEGEN" ]; then # kindlegen writes output next to the input; -o takes a bare filename. out=$("$KINDLEGEN" "$f" -o "$(basename "${f%.epub}").mobi" 2>&1) rm -f "${f%.epub}.mobi" if echo "$out" | grep -q "could not be generated"; then ok=0 echo "FAIL (kindlegen) $f" echo "$out" | grep -E '^Error|E[0-9]{4,}' | sed 's/^/ /' fi fi if [ $ok -eq 1 ]; then echo "PASS $f"; else fail=1; fi done [ -z "$KINDLEGEN" ] && echo "note: kindlegen not found; ran epubcheck only (set KINDLEGEN to also test Kindle conversion)." >&2 exit $fail