#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" REPORT_DIR="${ROOT_DIR}/reports" BUILD_DIR="${ROOT_DIR}/build" CLEAN=0 ENTRYPOINT="${ROOT_DIR}/report.typ" usage() { cat <<'EOF' Usage: ./render.sh [--clean] PATH ./render.sh [--clean] --all Examples: ./render.sh reports/2026-09-04-clevo-handoff.yaml ./render.sh --clean --all Outputs PDFs into: ./build/ --clean removes old PDFs before rendering: all PDFs in this category with --all, or only the selected output with a single input. Other files are kept. Requirements: - typst must be available in PATH EOF } die() { echo "error: $*" >&2 exit 1 } check_dependencies() { command -v typst >/dev/null 2>&1 \ || die "'typst' was not found in PATH." } render_report() { local input="$1" [[ -f "$input" ]] \ || die "report not found: $input" case "$input" in *.yaml|*.yml) ;; *) die "unsupported report format: $input (expected .yaml or .yml)" ;; esac local filename local basename local output filename="$(basename -- "$input")" basename="${filename%.*}" local relative="$basename" if [[ "$input" == "$REPORT_DIR/"* ]]; then relative="${input#"$REPORT_DIR/"}" relative="${relative%.*}" fi [[ "$relative" != diagnostics && "$relative" != diagnostics/* ]] \ || die "reports/diagnostics is reserved for diagnostic PDF output" output="${BUILD_DIR}/${relative}.pdf" mkdir -p "$(dirname -- "$output")" if [[ "$CLEAN" -eq 1 ]]; then rm -f -- "$output" fi echo "Rendering: $input" echo " -> $output" ( cd "$ROOT_DIR" # Pass the report path relative to the repository root when possible. local report_path="$input" if [[ "$input" == "$ROOT_DIR/"* ]]; then report_path="${input#"$ROOT_DIR/"}" fi typst compile \ --input "report=${report_path}" \ "$ENTRYPOINT" \ "$output" ) echo "Done: $output" } render_all() { [[ -d "$REPORT_DIR" ]] \ || die "report directory not found: $REPORT_DIR" mkdir -p "$BUILD_DIR" if [[ "$CLEAN" -eq 1 ]]; then find "$BUILD_DIR" -path "$BUILD_DIR/diagnostics" -prune -o -type f -name '*.pdf' -exec rm -f -- {} + fi local found=0 while IFS= read -r -d '' report; do found=1 render_report "$report" done < <( find "$REPORT_DIR" \ -type f \ \( -name '*.yaml' -o -name '*.yml' \) \ -print0 | sort -z ) if [[ "$found" -eq 0 ]]; then echo "No YAML reports found in $REPORT_DIR" return 0 fi echo echo "All reports rendered successfully." } main() { local args=() for arg in "$@"; do case "$arg" in --clean) CLEAN=1 ;; -h|--help) usage; return ;; *) args+=("$arg") ;; esac done set -- "${args[@]}" check_dependencies [[ $# -eq 1 ]] || { usage exit 1 } case "$1" in --all) render_all ;; -h|--help) usage ;; *) # Accept paths relative to either: # - the current working directory # - the repository root if [[ -f "$1" ]]; then render_report "$(realpath "$1")" elif [[ -f "${ROOT_DIR}/$1" ]]; then render_report "$(realpath "${ROOT_DIR}/$1")" else die "report not found: $1" fi ;; esac } main "$@"