aboutsummaryrefslogtreecommitdiffstats
path: root/validate.sh
blob: 5bc0224d1ff7ad8b8cc9393479b5cfcf7bffb30e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/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