#!/bin/bash

#compile all tex files except settings
mkdir -p output
find ./* -maxdepth 1 -mindepth 1 -type f -name "*.tex" \
     -not -name "settings.tex" \
     -not -name "SSxx Klausurvorlage.tex" \
     -not -name "klausuren-Settings-utf8.tex" \
     -print0 | \
    xargs -0 -P 4 -I [] bash -c 'cd "$(dirname "[]")"; latexmk -pdf -outdir=../output "$(basename "[]")";'

echo "finished compiling"
#cleanup compile help files
read -p "Delete complile helping files (aux, log, ...)?" -n 1 -r

if [[ $REPLY =~ ^[Yy]$ ]]; then
    extensions="acn acr alg aux bbl bcf blg -blx.bib dvi fdb_latexmk fls glg glo gls idx ilg ind ist lof log lot maf mtc mtc0 nav nlo out pdfsync ps snm synctex.gz toc vrb xdy tdo .DS_Store"

    for ext in $extensions; do
        find ./output -maxdepth 1 -mindepth 1 -type f -name "*.$ext" -delete
    done
fi
