#!/usr/bin/env sh root="$(git rev-parse --show-toplevel)" file="$root/%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 fd . "$root" --type directory --exclude "$dst" --exclude "./$dst/*" --hidden --exec realpath --relative-to=. {} | while IFS= read -r dir; do mkdir --parents "$dst/$dir" done test "$clear" = true && rm "$dst/${file%tex}out" fd . "$root/figures" --type file --extension tex | while read -r figure; do figure_name_full="$(basename "$figure")" figure_name="${figure_name_full%.tex}" figure_hash="$(sha256sum "$figure")" figure_cache_hash="$( [ -e "$dst/figures/$figure_name/$figure_name.sha256sum_hash" ] && cat "$dst/figures/$figure_name/$figure_name.sha256sum_hash" )" echo "Compiling figure: '$figure_name'.." mkdir --parents "$dst/figures/$figure_name" if [ "$figure_hash" = "$figure_cache_hash" ]; then echo " -> Didn't change, not re-compiling." else echo "$figure_hash" >"$dst/figures/$figure_name/$figure_name.sha256sum_hash" pdflatex -output-directory="$dst" -file-line-error -jobname="figures/$figure_name/$figure_name" "$figure" || exit 1 fi done || exit 1 latexmk -outdir="$dst" -file-line-error -pdflatex -recorder "$file"