#!/usr/bin/env sh file="./%INIT_APPLICATION_NAME.tex" dst=build clear=false for arg in "$@"; do case "$arg" in "--clear") clear=true ;; --*) echo "No such option: '$arg'" exit 2 ;; *) file="$arg" ;; esac done # find all directories which are not the destination dir or inside it find . -type d -not -name "$dst" -not -path "./$dst/*" -printf '%P\n' | while IFS= read -r dir; do mkdir --parents "$dst/$dir" done test "$clear" = true && rm "$dst/${file%tex}out" latexmk -outdir="$dst" -file-line-error -pdflatex -recorder "$file"