#!/bin/bash

run_lualatex() {
    local jobname=$1
    local texinput=$2

    # Sequentially run the lualatex command twice
    for i in 1 2 3; do
        lualatex -jobname="$jobname" -shell-escape "$texinput"
    done
}

current_dir=$(basename "$(pwd)")

# Run each job in the background to achieve parallel execution
run_lualatex "$current_dir" "\PassOptionsToClass{handout}{beamer}\input{main.tex}"
rm -f *.aux *.log *.toc *.out *.lof *.lot *.bbl *.blg *.fls *.fdb_latexmk *.synctex.gz *.nav *.snm
run_lualatex "$current_dir-presentation" "main.tex"
rm -f *.aux *.log *.toc *.out *.lof *.lot *.bbl *.blg *.fls *.fdb_latexmk *.synctex.gz *.nav *.snm
run_lualatex "$current_dir-print-portrait" "\PassOptionsToClass{handout}{beamer}\def\printportrait{1}\input{main.tex}"
rm -f *.aux *.log *.toc *.out *.lof *.lot *.bbl *.blg *.fls *.fdb_latexmk *.synctex.gz *.nav *.snm
run_lualatex "$current_dir-print-landscape" "\PassOptionsToClass{handout}{beamer}\def\printlandscape{1}\input{main.tex}"
rm -f *.aux *.log *.toc *.out *.lof *.lot *.bbl *.blg *.fls *.fdb_latexmk *.synctex.gz *.nav *.snm

# Wait for all background jobs to complete
wait
