#!/bin/bash

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

# Check if visualize.py exists
if [[ ! -f "$SCRIPT_DIR/draw_board.py" ]]; then
    echo "Error: draw_board.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/draw_board.py" "$1" >  "$OUTPUT_FILE"
if [[ $? -ne 0 ]]; then
    echo "Error: draw_board.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
pdflatex "$OUTPUT_FILE"
if [[ $? -ne 0 ]]; then
    echo "Error: pdflatex failed" >&2
    exit 1
fi

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

echo "Script completed successfully."
exit 0
