reportist/render-diagnostic.sh

113 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
# Render a legacy WinDiagnostics capture with the preserved diagnostic layout.
set -euo pipefail
root=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
usage() {
cat >&2 <<EOF
Usage: $0 INPUT --output PDF --author NAME --logo FILE [--theme NAME] [--source-name NAME] [--force]
EOF
exit 2
}
fail() {
echo "error: $*" >&2
exit 1
}
require_value() {
(($# >= 2)) || usage
}
input=${1:-}
[[ -n "$input" ]] || usage
shift
output=
author=
logo=
theme=default
source_name=
force=false
while (($#)); do
case $1 in
--output)
require_value "$@"
output=$2
shift 2
;;
--author)
require_value "$@"
author=$2
shift 2
;;
--logo)
require_value "$@"
logo=$2
shift 2
;;
--theme)
require_value "$@"
theme=$2
shift 2
;;
--source-name)
require_value "$@"
source_name=$2
shift 2
;;
--force)
force=true
shift
;;
*)
usage
;;
esac
done
for value in "$output" "$author" "$logo"; do
[[ -n "$value" ]] || usage
done
command -v typst >/dev/null 2>&1 || fail "Typst is required"
[[ -f "$input" ]] || fail "$input: diagnostic capture does not exist"
[[ -f "$logo" ]] || fail "$logo: logo does not exist"
input=$(realpath "$input")
logo=$(realpath "$logo")
source_name=${source_name:-$(basename "$input")}
output_parent=$(dirname "$output")
mkdir -p "$output_parent"
output=$(cd "$output_parent" && pwd -P)/$(basename "$output")
[[ "$input" != "$output" ]] || fail "input and output paths must differ"
[[ ! -e "$output" || "$force" == true ]] || fail "$output: output exists; use --force to replace it"
stage=$(mktemp -d "${TMPDIR:-/tmp}/reportist-diagnostic.XXXXXXXX")
trap 'rm -rf "$stage"' EXIT
mkdir -p "$stage/assets"
cp -R "$root/templates/diagnostics/." "$stage/"
cp "$input" "$stage/diagnostic.json"
cp "$logo" "$stage/assets/beffroi.png"
typst compile \
--root "$stage" \
--input diagnostics=diagnostic.json \
--input "diagnostics-source=$source_name" \
--input "author=$author" \
--input "theme=$theme" \
"$stage/diagnostics.typ" \
"$stage/diagnostic.pdf" || fail "diagnostic rendering failed"
if [[ "$force" == true ]]; then
mv -f "$stage/diagnostic.pdf" "$output"
else
mv "$stage/diagnostic.pdf" "$output"
fi
echo "Rendered diagnostic PDF: $output"