#!/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-lab4-report.md"
WITH_CODE_MD="${SCRIPT_DIR}/zivro-lab4-report-with-code.md"
OUTPUT_PDF="${SCRIPT_DIR}/zivro-lab4-report.pdf"

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

echo "[2/2] Генерация 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}"
