#!/usr/bin/env bash # Shared setup; sourced by build.sh and preview.sh. set -euo pipefail repo_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" usage() { echo "Usage: $0 [--force|-f] [document.typ | report-directory]" echo "Defaults to reports/respuesta-observaciones-avance-1/main.typ. Relative paths use the current directory." echo "Existing PDFs are preserved unless --force is given. Preview watches only with --force." } force=false input="" while [[ $# -gt 0 ]]; do case "$1" in --force|-f) force=true ;; --help|-h) usage; exit 0 ;; -*) echo "Error: unknown option: $1" >&2; usage >&2; exit 2 ;; *) if [[ -n "$input" ]]; then usage >&2; exit 2; fi input="$1" ;; esac shift done input="${input:-$repo_root/reports/respuesta-observaciones-avance-1/main.typ}" if [[ -d "$input" ]]; then input="$input/main.typ"; fi if [[ ! -f "$input" || "$input" != *.typ ]]; then echo "Error: expected an existing .typ file: $input" >&2 exit 1 fi input="$(realpath -- "$input")" case "$input" in "$repo_root/template/"*) echo "Error: las plantillas no generan PDF; copie el documento base a reports/ primero." >&2 exit 1 ;; esac name="$(basename -- "$input" .typ)" if [[ "$name" == main ]]; then name="$(basename -- "$(cd -- "$(dirname -- "$input")" && pwd)")" fi output="$repo_root/pdf/$name.pdf" require_typst() { command -v typst >/dev/null 2>&1 || { echo "Error: install Typst and make it available on PATH." >&2 exit 1 } } build_pdf() { if [[ -e "$output" && "$force" == false ]]; then printf 'PDF existente: %s (use --force para regenerar)\n' "$output" return fi require_typst mkdir -p -- "$repo_root/pdf" typst compile --root "$repo_root" "$input" "$output" printf 'PDF: %s\n' "$output" }