#!/usr/bin/env -S bash -x
# Exit on first error and cleanup.
set -e
trap 'kill $(pgrep -g $$ | grep -v $$) > /dev/null 2> /dev/null || :' EXIT
rm -rvf $(cat .gitignore)

# Run the example
export SOURCE_DATE_EPOCH="315532800"
export REPREP_LATEX="lualatex"
stepup boot -w -n 1 & # > current_stdout.txt &

# Get the graph after completion of the pending steps.
stepup wait
stepup graph current_graph

# Reproducibility test
rm paper.aux paper.log
mv paper.pdf paper1.pdf
stepup watch-delete paper.pdf
stepup run
stepup join
stepup make-inventory -o reproducibility_inventory.txt paper.pdf paper1.pdf

# Wait for background processes, if any.
wait

# Check files that are expected to be present and/or missing.
[[ -f plan.py ]] || exit 1
[[ -f paper.pdf ]] || exit 1
[[ -f paper.log ]] || exit 1
[[ -f paper.aux ]] || exit 1
[[ -f paper1.pdf ]] || exit 1
[[ -f reproducibility_inventory.txt ]] || exit 1
stepup check-inventory paper-inventory.txt
