export NEXMARK_HOME=$(pwd)
echo "============================================================================"
echo "                    Nexmark Benchmark Execution Tool"
echo "============================================================================"
echo ""
echo "Environment Configuration:"
echo "  NEXMARK_HOME: ${NEXMARK_HOME}"
echo ""

# Load environment variables
if [ -f "${NEXMARK_HOME}/nexmark_env.sh" ]; then
    source "${NEXMARK_HOME}/nexmark_env.sh"
    echo "  ✓ Environment configuration loaded successfully"
else
    echo "  ✗ Error: Environment configuration file not found"
    echo "    Expected location: ${NEXMARK_HOME}/nexmark_env.sh"
    exit 1
fi

echo ""

# Configure benchmark parameters
RUN_QUERY_CMD="${NEXMARK_HOME}/bin/run_query_vvp.sh"
CONF_FILE="${NEXMARK_HOME}/conf/vvp-flink-conf.yaml"
SDK_ENV_KEY="sdk-env"
SDK_ENV_VALUE="vvp-dev"
PARALLELISM=8

# Display execution parameters
echo "Execution Parameters:"
echo "  Configuration File:   ${CONF_FILE}"
echo "  Queries:              ${QUERIES}"
echo "  Parallelism:          ${PARALLELISM}"
echo "  Flink Version:        ${FLINK_VERSION}"
echo "  Workspace:            ${WORK_SPACE}"
echo "  Namespace:            ${NAMESPACE}"
echo "  Deployment Target:    default-queue"
echo ""

# Execute benchmark
echo "============================================================================"
echo "Starting Nexmark Benchmark Execution..."
echo "============================================================================"
echo ""

$RUN_QUERY_CMD \
 --accessId "$AK" \
 --accessSecret "$SK" \
 --endPoint "$END_POINT" \
 --flinkVersion "$FLINK_VERSION" \
 --flinkConf "$CONF_FILE" \
 --queries "$QUERIES" \
 --sdkEnvValue "$SDK_ENV_VALUE" \
 --sdkEnvKey "$SDK_ENV_KEY" \
 --namespace "$NAMESPACE" \
 --workspace "$WORK_SPACE" \
 --deploymentTarget "default-queue" \
 --nexmarkJar "$NEXMARK_JAR" \
 --parallelism "$PARALLELISM"

# Check exit status
EXIT_CODE=$?
echo ""
echo "============================================================================"
if [ $EXIT_CODE -eq 0 ]; then
    echo "✓ Benchmark execution completed successfully"
else
    echo "✗ Benchmark execution failed with exit code: ${EXIT_CODE}"
fi
echo "============================================================================"

exit $EXIT_CODE