#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" DIAGNOSTICS_DIR="${ROOT_DIR}/diagnostics" BUILD_DIR="${ROOT_DIR}/build/diagnostics" CLEAN=0 usage() { cat <<'EOF_USAGE' Usage: ./render-diagnostics.sh [--clean] REPORT.json "Author Name" [theme] ./render-diagnostics.sh [--clean] --all "Author Name" [theme] Examples: ./render-diagnostics.sh diagnostics/windows_system_report_PC.json "Jane Doe" ./render-diagnostics.sh diagnostics/windows_system_report_PC.json "Jane Doe" cic ./render-diagnostics.sh --all "Jane Doe" ./render-diagnostics.sh --all "Jane Doe" eridani --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. The theme is optional and defaults to "default". Available themes: default, cic, eridani EOF_USAGE } die() { echo "error: $*" >&2 exit 1 } render_diagnostics() { local input="$1" local author="$2" local theme="$3" [[ -f "$input" ]] || die "diagnostics JSON not found: $input" [[ "$input" == *.json ]] || die "diagnostics input must be a .json file: $input" local input_abs input_abs="$(realpath "$input")" [[ "$input_abs" == "$ROOT_DIR/"* ]] \ || die "diagnostics JSON must be inside the repository: $input" local input_typst="${input_abs#"$ROOT_DIR/"}" local relative_output if [[ "$input_abs" == "$DIAGNOSTICS_DIR/"* ]]; then relative_output="${input_abs#"$DIAGNOSTICS_DIR/"}" else relative_output="$(basename -- "$input_abs")" fi relative_output="${relative_output%.*}.pdf" local output="${BUILD_DIR}/${relative_output}" mkdir -p "$(dirname -- "$output")" if [[ "$CLEAN" -eq 1 ]]; then rm -f -- "$output" fi echo "Rendering: $input_typst" echo " -> ${output#"$ROOT_DIR/"}" ( cd "$ROOT_DIR" typst compile \ --input "diagnostics=${input_typst}" \ --input "author=${author}" \ --input "theme=${theme}" \ diagnostics.typ \ "$output" ) } render_all() { local author="$1" local theme="$2" local found=0 [[ -d "$DIAGNOSTICS_DIR" ]] \ || die "diagnostics directory not found: $DIAGNOSTICS_DIR" if [[ "$CLEAN" -eq 1 && -d "$BUILD_DIR" ]]; then find "$BUILD_DIR" -type f -name '*.pdf' -exec rm -f -- {} + fi while IFS= read -r -d '' input; do found=1 render_diagnostics "$input" "$author" "$theme" done < <(find "$DIAGNOSTICS_DIR" -type f -name '*.json' -print0 | sort -z) if [[ "$found" -eq 0 ]]; then echo "No diagnostics .json reports found in $DIAGNOSTICS_DIR" return 0 fi echo "All diagnostics 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[@]}" command -v typst >/dev/null 2>&1 || die "typst was not found" [[ $# -ge 2 && $# -le 3 ]] || { usage; exit 1; } local target="$1" local author="$2" local theme="${3:-default}" [[ -n "$author" ]] \ || die "author is required; pass it as the second argument" if [[ "$target" == "--all" ]]; then render_all "$author" "$theme" else if [[ -f "$target" ]]; then render_diagnostics "$target" "$author" "$theme" elif [[ -f "${ROOT_DIR}/$target" ]]; then render_diagnostics "${ROOT_DIR}/$target" "$author" "$theme" else die "diagnostics JSON not found: $target" fi fi } main "$@"