Files
northlands-guild-packs/scripts/rebuild-specialist-pdfs.sh
rick 5950809a56 scripts: add assemble-packs.py and rebuild-specialist-pdfs.sh
assemble-packs.py: Reliable pack assembly with cross-flavor singleton lookup,
name aliasing (Oil of X → X (oil of), etc.), curly apostrophe normalization,
and built-in QA verification (page counts + singles/SVG count check).

rebuild-specialist-pdfs.sh: Full rebuild of DM + Player PDFs with price tags
in filenames. Note: bash pack assembly has scoping bugs; use assemble-packs.py
for pack assembly instead.

Made-with: Cursor
2026-04-16 20:03:17 +02:00

269 lines
7.7 KiB
Bash
Executable File

#!/bin/bash
# Rebuild all Specialist PDFs: DM, Player Singles (with price tags), Packs, and All-Packs.
# Run from repo root. Requires inkscape and pdfunite on PATH.
set -euo pipefail
TIER="0c Specialist"
ITEMS_MD="$TIER/ITEMS.md"
if [ ! -f "$ITEMS_MD" ]; then
echo "ERROR: $ITEMS_MD not found. Run from repo root." >&2
exit 1
fi
# Build a lookup: SVG basename → price string from ITEMS.md
# Parses lines like: - **Price**: 550s or - **Price**: 26g
declare -A PRICES
current_title=""
while IFS= read -r line; do
if [[ "$line" =~ ^###\ ]]; then
current_title="${line#\#\#\# }"
elif [[ "$line" =~ ^\-\ \*\*Price\*\*:\ (.+)$ ]]; then
price="${BASH_REMATCH[1]}"
if [ -n "$current_title" ]; then
PRICES["$current_title"]="$price"
fi
fi
done < "$ITEMS_MD"
echo "Loaded ${#PRICES[@]} prices from $ITEMS_MD"
for flavor in Defense Magic Offense Recovery Stealth Strike Utility; do
svgdir="$TIER/$flavor"
[ -d "$svgdir" ] || continue
echo ""
echo "=== $flavor ==="
mkdir -p "$svgdir/PDFs DM" "$svgdir/PDFs Players/Singles"
count=0
for svg in "$svgdir"/*.svg; do
[ -f "$svg" ] || continue
name=$(basename "$svg" .svg)
[[ "$name" == Template* ]] && continue
price="${PRICES[$name]:-}"
if [ -z "$price" ]; then
echo " WARN: No price for '$name', using name only"
dm_out="$svgdir/PDFs DM/$name.pdf"
player_out="$svgdir/PDFs Players/Singles/$name.pdf"
else
dm_out="$svgdir/PDFs DM/$name ($price).pdf"
player_out="$svgdir/PDFs Players/Singles/$name ($price).pdf"
fi
# DM PDF: Inkscape exports multi-page SVG as separate page PDFs
inkscape "$svg" --export-type=pdf --export-filename="/tmp/dm-batch-" --export-page=all 2>/dev/null
if [ -f "/tmp/dm-batch-_p1.pdf" ] && [ -f "/tmp/dm-batch-_p2.pdf" ]; then
pdfunite "/tmp/dm-batch-_p1.pdf" "/tmp/dm-batch-_p2.pdf" "$dm_out"
rm -f /tmp/dm-batch-_p*.pdf
else
echo " WARN: DM PDF export failed for $name"
fi
# Player PDF
python3 scripts/export-player-pdf.py "$svg" -o "$player_out" 2>/dev/null
count=$((count+1))
done
echo " Exported $count DM + Player PDFs"
done
# Assemble packs from PACKS.md
echo ""
echo "=== Assembling Packs ==="
current_flavor=""
current_pack_num=0
current_pack_name=""
current_pack_items=()
find_single_pdf() {
# Search all flavor directories for a matching player single PDF.
# Handles: exact match, "Oil of X" → "X (oil of)", "Potion of X" → "X (potion of)",
# and known PACKS.md → SVG name aliases.
local item="$1"
local search_names=("$item")
# "Oil of X" → "X (oil of)"
if [[ "$item" =~ ^Oil\ of\ (.+)$ ]]; then
search_names+=("${BASH_REMATCH[1]} (oil of)")
fi
# "Potion of X" → "X (potion of)"
if [[ "$item" =~ ^Potion\ of\ (.+)$ ]]; then
search_names+=("${BASH_REMATCH[1]} (potion of)")
fi
# Known PACKS.md aliases that differ from SVG card names
declare -A ALIASES=(
["Shieldfoam Ampoule"]="Shieldfoam Vial"
["Frostbite Trap Resin"]="Frostbite Trap"
["Rest-Easing Draught"]="Resting Draught"
["Elixir of Fire Breath"]="Elixir of Lesser Fire Breath"
["Mire-Walk Oil"]="Mirewalk Oil"
["Acid Flask"]="Acid Flask (Improved)"
["Rune of Warding"]="Rune of Warding (Absorb)"
["Healing Poultice"]="Healing Poultice"
["Bandage-Seal"]="Bandage-Seal"
["Lanternfish Capsule"]="Lanternfish Capsule"
)
if [ -n "${ALIASES[$item]:-}" ]; then
search_names+=("${ALIASES[$item]}")
fi
for flavor_dir in "$TIER"/*/PDFs\ Players/Singles; do
[ -d "$flavor_dir" ] || continue
for try_name in "${search_names[@]}"; do
for candidate in "$flavor_dir"/*.pdf; do
[ -f "$candidate" ] || continue
local cbase=$(basename "$candidate" .pdf)
if [[ "$cbase" == "$try_name" ]] || [[ "$cbase" == "$try_name ("* ]]; then
echo "$candidate"
return 0
fi
done
done
done
return 1
}
assemble_pack() {
if [ ${#current_pack_items[@]} -eq 0 ]; then return; fi
local packs_dir="$TIER/$current_flavor/PDFs Players/Packs"
mkdir -p "$packs_dir"
local padnum
printf -v padnum "%02d" "$current_pack_num"
local pack_pdf="$packs_dir/${padnum} Specialist ${current_flavor} - ${current_pack_name}.pdf"
local pdf_list=()
local missing=()
for item in "${current_pack_items[@]}"; do
local found
found=$(find_single_pdf "$item")
if [ -n "$found" ]; then
pdf_list+=("$found")
else
missing+=("$item")
fi
done
if [ ${#missing[@]} -gt 0 ]; then
for m in "${missing[@]}"; do
echo " WARN: Pack '$current_pack_name' missing '$m'"
done
fi
if [ ${#pdf_list[@]} -ge 2 ]; then
pdfunite "${pdf_list[@]}" "$pack_pdf"
echo " Pack: $pack_pdf (${#pdf_list[@]} cards)"
elif [ ${#pdf_list[@]} -eq 1 ]; then
cp "${pdf_list[0]}" "$pack_pdf"
echo " Pack: $pack_pdf (1 card)"
fi
}
PACKS_MD="$TIER/PACKS.md"
if [ ! -f "$PACKS_MD" ]; then
echo "ERROR: $PACKS_MD not found" >&2
exit 1
fi
while IFS= read -r line; do
# Flavor header: ## Defense
if [[ "$line" =~ ^##\ ]] && [[ ! "$line" =~ ^###\ ]]; then
# Flush previous pack
assemble_pack
current_pack_items=()
current_flavor="${line#\#\# }"
current_pack_num=0
fi
# Pack header: ### Pack N — "Name"
if [[ "$line" =~ ^###\ Pack ]]; then
# Flush previous pack
assemble_pack
current_pack_items=()
current_pack_num=$((current_pack_num+1))
if [[ "$line" =~ \"([^\"]+)\" ]]; then
current_pack_name="${BASH_REMATCH[1]}"
else
current_pack_name="Pack $current_pack_num"
fi
fi
# Table row: | Item Name | Rarity | Price |
if [[ "$line" =~ ^\|\ [A-Z] ]]; then
item_name=$(echo "$line" | awk -F'|' '{print $2}' | sed 's/^ *//;s/ *$//')
if [ -n "$item_name" ] && [[ "$item_name" != "Item" ]]; then
current_pack_items+=("$item_name")
fi
fi
done < "$PACKS_MD"
# Flush last pack
assemble_pack
# Grand all-packs PDF
echo ""
echo "=== Building All Specialist Player Packs.pdf ==="
all_packs=()
for flavor in Defense Magic Offense Recovery Stealth Strike Utility; do
packs_dir="$TIER/$flavor/PDFs Players/Packs"
[ -d "$packs_dir" ] || continue
for p in "$packs_dir"/*.pdf; do
[ -f "$p" ] && all_packs+=("$p")
done
done
if [ ${#all_packs[@]} -gt 0 ]; then
pdfunite "${all_packs[@]}" "$TIER/All Specialist Player Packs.pdf"
echo " Created: $TIER/All Specialist Player Packs.pdf (${#all_packs[@]} packs)"
else
echo " WARN: No packs found to assemble"
fi
# QA: verify pack page counts (each card = 1 page, so pages = card count)
echo ""
echo "=== QA: Pack page counts ==="
qa_fail=0
for flavor in Defense Magic Offense Recovery Stealth Strike Utility; do
packs_dir="$TIER/$flavor/PDFs Players/Packs"
[ -d "$packs_dir" ] || continue
for p in "$packs_dir"/*.pdf; do
[ -f "$p" ] || continue
pages=$(pdfinfo "$p" 2>/dev/null | awk '/^Pages:/{print $2}')
pname=$(basename "$p")
if [ -z "$pages" ] || [ "$pages" -lt 4 ]; then
echo " FAIL: $pname — $pages pages (expected 4-6)"
qa_fail=1
else
echo " OK: $pname — $pages pages"
fi
done
done
echo ""
echo "=== QA: Singles count per flavor ==="
for flavor in Defense Magic Offense Recovery Stealth Strike Utility; do
singles_dir="$TIER/$flavor/PDFs Players/Singles"
[ -d "$singles_dir" ] || continue
count=$(ls "$singles_dir"/*.pdf 2>/dev/null | wc -l)
svg_count=$(ls "$TIER/$flavor"/*.svg 2>/dev/null | grep -cv Template || true)
if [ "$count" -ne "$svg_count" ]; then
echo " WARN: $flavor — $count PDFs vs $svg_count SVGs"
else
echo " OK: $flavor — $count PDFs, $svg_count SVGs"
fi
done
if [ "$qa_fail" -eq 1 ]; then
echo ""
echo "WARNING: Some packs have fewer pages than expected. Check warnings above."
fi
echo ""
echo "Done!"