LATEXMK := "latexmk -pdflatex=lualatex -pdf -shell-escape -interaction=nonstopmode"

build:
    #!/usr/bin/env bash
    FILE=$(find . -type f \( -path './chapters/*' -name '*.tex' ! -name 'template.tex' \) -o -name 'thesis.tex' | sed 's|^\./||' | sort | fzf)
    DIR=$(dirname "$FILE")
    BASE=$(basename "$FILE" .tex)
    kitty -e sh -c "cd '$DIR' && {{ LATEXMK }} -outdir=build -pvc -view=none '$BASE.tex'" > /dev/null 2>&1 &
    while [ ! -f "$DIR/build/$BASE.pdf" ]; do sleep 0.5; done
    if command -v inotifywait > /dev/null; then
        inotifywait -qq -e close_write "$DIR/build/$BASE.pdf"
    else
        sleep 2
    fi
    zathura "$DIR/build/$BASE.pdf" > /dev/null 2>&1 &

clean:
    find . -type d \( -name build -o -name svg-inkscape \) -exec rm -rf {} +
