#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPORT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
ROOT_DIR="$(cd "${REPORT_DIR}/.." && pwd)"

INPUT_MD="${SCRIPT_DIR}/zivro-lab2-report.md"
WITH_CODE_MD="${SCRIPT_DIR}/zivro-lab2-report-with-code.md"
OUTPUT_PDF="${SCRIPT_DIR}/zivro-lab2-report.pdf"

echo "[1/3] Генерация PNG из UML..."
python3 "${REPORT_DIR}/render_uml_png.py" \
  --input-dir "${SCRIPT_DIR}/uml" \
  --glob "*.puml"

echo "[2/3] Обновление версии отчёта с листингом кода..."
python3 "${REPORT_DIR}/append_sources_to_report.py" \
  --input "${INPUT_MD}" \
  --output "${WITH_CODE_MD}" \
  --base "${ROOT_DIR}" \
  --include "Minint/**/*.cs" \
  --include "Minint.Core/**/*.cs" \
  --include "Minint.Infrastructure/**/*.cs" \
  --include "Minint.Tests/**/*.cs"

echo "[3/3] Генерация PDF..."
pandoc "${WITH_CODE_MD}" \
  -f markdown+implicit_figures+link_attributes+tex_math_dollars \
  -o "${OUTPUT_PDF}" \
  --pdf-engine=xelatex \
  --toc \
  -V geometry:left=1.5cm,right=1.5cm,top=1.5cm,bottom=1.5cm \
  -V mainfont="DejaVu Serif" \
  -V monofont="DejaVu Sans Mono" \
  -H "${REPORT_DIR}/header.tex"

echo "Готово: ${OUTPUT_PDF}"
