#!/bin/bash

# Set script directory
SCRIPT_DIR="$(dirname "$0")"

# Check if visualize.py exists
if [[ ! -f "$SCRIPT_DIR/visualize.py" ]]; then
    echo "Error: visualize.py not found in $SCRIPT_DIR" >&2
    exit 1
fi

tmptexdir=$(mktemp -d)  # Create a unique temporary directory
OUTPUT_FILE="$tmptexdir/testcase.tex"

# Run visualize.py
python3 "$SCRIPT_DIR/visualize.py" "$1" "$2" >  "$OUTPUT_FILE"
if [[ $? -ne 0 ]]; then
    echo "Error: visualize.py failed" >&2
    exit 1
fi

# Check if testcase.tex exists
if [[ ! -f "$OUTPUT_FILE" ]]; then
    echo "Error: texfile not found in $SCRIPT_DIR" >&2
    exit 1
fi

# Run pdflatex
lualatex "$OUTPUT_FILE"
if [[ $? -ne 0 ]]; then
    echo "Error: lualatex failed" >&2
    exit 1
fi

# Clean up testcase.tex
rm -r "$tmptexdir"

echo "Script completed successfully."
exit 0
