#!/bin/bash
# Prompt: Design2Code eval wrapper for sbatch
# ==============================================================================
# Design2Code Evaluation Script
# Evaluates dVLM model on Design2Code benchmark (screenshot -> HTML)
# ==============================================================================
set -eo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../../../.." && pwd)"

# Conda environment
EVAL_CONDA_ENV=${EVAL_CONDA_ENV:-dvlm}
if [ -f "${HOME}/.bashrc" ]; then
    source "${HOME}/.bashrc"
fi
if command -v conda >/dev/null 2>&1; then
    conda activate "${EVAL_CONDA_ENV}" || true
fi

# Configuration
MODEL_PATH=${MODEL_PATH:?"\$MODEL_PATH must be set"}
PROCESSOR_PATH=${PROCESSOR_PATH:-Qwen/Qwen2.5-VL-3B-Instruct}
USE_MDM=${USE_MDM:-0}
BLOCK_SIZE=${BLOCK_SIZE:-}
MAX_TOKENS=${MAX_TOKENS:-2048}
TESTSET_DIR=${TESTSET_DIR:-${PROJECT_ROOT}/third_party/Design2Code/testset_final}
OUTPUT_DIR=${OUTPUT_DIR:-}
SKIP_INFERENCE=${SKIP_INFERENCE:-0}

# PYTHONPATH
DVLM_DIR="${SCRIPT_DIR}/../.."
export PYTHONPATH="${DVLM_DIR}/models:${PROJECT_ROOT}/src:${PYTHONPATH:-}"

# Build args
ARGS="--model_path ${MODEL_PATH} --processor_path ${PROCESSOR_PATH} --max_tokens ${MAX_TOKENS}"
ARGS="${ARGS} --use_mdm ${USE_MDM}"
[ -n "${TESTSET_DIR}" ] && ARGS="${ARGS} --testset_dir ${TESTSET_DIR}"
[ -n "${OUTPUT_DIR}" ] && ARGS="${ARGS} --output_dir ${OUTPUT_DIR}"
[ -n "${BLOCK_SIZE}" ] && ARGS="${ARGS} --block_size ${BLOCK_SIZE}"
[ "${SKIP_INFERENCE}" = "1" ] && ARGS="${ARGS} --skip_inference"

echo "Running Design2Code evaluation..."
echo "MODEL_PATH: ${MODEL_PATH}"
echo "TESTSET_DIR: ${TESTSET_DIR}"
echo ""

python3 "${SCRIPT_DIR}/eval_design2code.py" ${ARGS}
