#!/bin/bash
set -e

INPUT_DIR="/workspace/input"
OUTPUT_DIR="/workspace/output"
INPUT_JSON="${INPUT_DIR}/task.json"
OUTPUT_JSON="${OUTPUT_DIR}/result.json"

# Allow overrides via env vars
INPUT_JSON="${COREMARK_INPUT:-$INPUT_JSON}"
OUTPUT_JSON="${COREMARK_OUTPUT:-$OUTPUT_JSON}"

mkdir -p "$(dirname "$OUTPUT_JSON")"

echo "=========================================="
echo "CoreMark-PRO CPU Performance Benchmark"
echo "=========================================="
echo ""

# Set build directory
BUILD_DIR="/workspace/coremark-pro/builds/linux64/gcc64/bin"

# Check if binaries exist
if [ ! -f "$BUILD_DIR/core.exe" ]; then
    echo "ERROR: CoreMark-PRO binaries not found in $BUILD_DIR"
    exit 1
fi

# Run the benchmark
exec python3 /workspace/coremark/coremark_perf.py \
    --input "$INPUT_JSON" \
    --output "$OUTPUT_JSON" \
    --build-dir "$BUILD_DIR"
