stupa-pdf-api/scripts/build-pdfs.sh

207 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
# STUPA PDF API - Build PDFs from LaTeX sources
# This script compiles the LaTeX templates into PDF forms
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Project directories
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
BACKEND_DIR="${PROJECT_ROOT}/backend"
ASSETS_DIR="${BACKEND_DIR}/assets"
# LaTeX template directories
LATEX_TEMPLATES_DIR="${BACKEND_DIR}/latex-templates"
LATEX_QSM_DIR="${BACKEND_DIR}/latex-qsm"
LATEX_VSM_DIR="${BACKEND_DIR}/latex-vsm"
# Function to check if required tools are installed
check_requirements() {
echo -e "${BLUE}Checking requirements...${NC}"
local missing=()
# Check for git
if ! command -v git &> /dev/null; then
missing+=("git")
fi
# Check for xelatex
if ! command -v xelatex &> /dev/null; then
missing+=("xelatex (texlive-xetex)")
fi
# Check for latexmk
if ! command -v latexmk &> /dev/null; then
missing+=("latexmk")
fi
if [ ${#missing[@]} -ne 0 ]; then
echo -e "${RED}Missing required tools: ${missing[*]}${NC}"
echo "Please install them first:"
echo " sudo apt-get install git texlive-full texlive-xetex texlive-lang-german texlive-fonts-extra latexmk"
exit 1
fi
echo -e "${GREEN}All requirements satisfied!${NC}"
}
# Function to setup git worktrees
setup_worktrees() {
echo -e "${BLUE}Setting up LaTeX template worktrees...${NC}"
cd "${LATEX_TEMPLATES_DIR}"
# Remove existing worktrees if they exist
if [ -d "${LATEX_QSM_DIR}" ]; then
echo -e "${YELLOW}Removing existing QSM worktree...${NC}"
git worktree remove --force "${LATEX_QSM_DIR}" 2>/dev/null || true
fi
if [ -d "${LATEX_VSM_DIR}" ]; then
echo -e "${YELLOW}Removing existing VSM worktree...${NC}"
git worktree remove --force "${LATEX_VSM_DIR}" 2>/dev/null || true
fi
# Create new worktrees
echo -e "${BLUE}Creating QSM worktree...${NC}"
git worktree add "${LATEX_QSM_DIR}" v1.2/QSM
echo -e "${BLUE}Creating VSM worktree...${NC}"
git worktree add "${LATEX_VSM_DIR}" v1.2/VSM
echo -e "${GREEN}Worktrees created successfully!${NC}"
}
# Function to build a PDF from LaTeX
build_pdf() {
local template_name=$1
local source_dir=$2
local output_name=$3
echo -e "${BLUE}Building ${template_name} PDF...${NC}"
# Change to source directory
cd "${source_dir}"
# Clean previous builds
rm -f *.aux *.log *.out *.fdb_latexmk *.fls *.synctex.gz *.pdf
# Build PDF using latexmk with xelatex
echo -e "${YELLOW}Running latexmk with xelatex...${NC}"
if latexmk -xelatex -interaction=nonstopmode -halt-on-error Main.tex; then
echo -e "${GREEN}Build successful!${NC}"
# Copy to assets directory
if [ -f "Main.pdf" ]; then
cp "Main.pdf" "${ASSETS_DIR}/${output_name}"
echo -e "${GREEN}Copied to ${ASSETS_DIR}/${output_name}${NC}"
else
echo -e "${RED}Error: Main.pdf not found after build${NC}"
return 1
fi
else
echo -e "${RED}Build failed for ${template_name}${NC}"
return 1
fi
# Clean up build artifacts
echo -e "${YELLOW}Cleaning up...${NC}"
rm -f *.aux *.log *.out *.fdb_latexmk *.fls *.synctex.gz
}
# Function to verify PDFs
verify_pdfs() {
echo -e "${BLUE}Verifying generated PDFs...${NC}"
local all_good=true
# Check QSM PDF
if [ -f "${ASSETS_DIR}/qsm.pdf" ]; then
local size=$(stat -c%s "${ASSETS_DIR}/qsm.pdf" 2>/dev/null || stat -f%z "${ASSETS_DIR}/qsm.pdf" 2>/dev/null)
if [ "$size" -gt 10000 ]; then
echo -e "${GREEN}✓ qsm.pdf ($(($size / 1024)) KB)${NC}"
else
echo -e "${RED}✗ qsm.pdf is too small (${size} bytes)${NC}"
all_good=false
fi
else
echo -e "${RED}✗ qsm.pdf not found${NC}"
all_good=false
fi
# Check VSM PDF
if [ -f "${ASSETS_DIR}/vsm.pdf" ]; then
local size=$(stat -c%s "${ASSETS_DIR}/vsm.pdf" 2>/dev/null || stat -f%z "${ASSETS_DIR}/vsm.pdf" 2>/dev/null)
if [ "$size" -gt 10000 ]; then
echo -e "${GREEN}✓ vsm.pdf ($(($size / 1024)) KB)${NC}"
else
echo -e "${RED}✗ vsm.pdf is too small (${size} bytes)${NC}"
all_good=false
fi
else
echo -e "${RED}✗ vsm.pdf not found${NC}"
all_good=false
fi
if [ "$all_good" = true ]; then
echo -e "${GREEN}All PDFs verified successfully!${NC}"
return 0
else
echo -e "${RED}PDF verification failed!${NC}"
return 1
fi
}
# Main execution
main() {
echo -e "${GREEN}STUPA PDF Builder${NC}"
echo "=================="
echo ""
# Check requirements
check_requirements
# Create assets directory if it doesn't exist
mkdir -p "${ASSETS_DIR}"
# Setup worktrees if needed
if [ ! -d "${LATEX_QSM_DIR}" ] || [ ! -d "${LATEX_VSM_DIR}" ]; then
setup_worktrees
fi
# Build QSM PDF
if ! build_pdf "QSM" "${LATEX_QSM_DIR}" "qsm.pdf"; then
echo -e "${RED}Failed to build QSM PDF${NC}"
exit 1
fi
# Build VSM PDF
if ! build_pdf "VSM" "${LATEX_VSM_DIR}" "vsm.pdf"; then
echo -e "${RED}Failed to build VSM PDF${NC}"
exit 1
fi
# Verify results
echo ""
verify_pdfs
echo ""
echo -e "${GREEN}PDF build complete!${NC}"
echo ""
echo "Generated PDFs:"
echo " - ${ASSETS_DIR}/qsm.pdf"
echo " - ${ASSETS_DIR}/vsm.pdf"
}
# Run main function
main "$@"