#!/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" usage() { cat <<'EOF' Usage: ./render-diagnostics.sh DUMP.txt "Author Name" [theme] ./render-diagnostics.sh --all "Author Name" [theme] Examples: ./render-diagnostics.sh diagnostics/windows_system_report_PC.txt "Jane Doe" ./render-diagnostics.sh diagnostics/windows_system_report_PC.txt "Jane Doe" cic ./render-diagnostics.sh --all "Jane Doe" ./render-diagnostics.sh --all "Jane Doe" eridani The theme is optional and defaults to "default". Available themes: default, cic, eridani EOF } die() { echo "error: $*" >&2 exit 1 } render_diagnostics() { local input="$1" local author="$2" local theme="$3" [[ -f "$input" ]] || die "diagnostics dump not found: $input" local input_abs input_abs="$(realpath "$input")" [[ "$input_abs" == "$ROOT_DIR/"* ]] \ || die "diagnostics dump 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")" 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" while IFS= read -r -d '' input; do found=1 render_diagnostics "$input" "$author" "$theme" done < <(find "$DIAGNOSTICS_DIR" -type f -name '*.txt' -print0 | sort -z) if [[ "$found" -eq 0 ]]; then echo "No diagnostics .txt dumps found in $DIAGNOSTICS_DIR" return 0 fi echo "All diagnostics rendered successfully." } main() { 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 dump not found: $target" fi fi } main "$@"