#!/bin/bash
set -u

BIN_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT="$(dirname "$BIN_DIR")"
ENV="$ROOT/python"

export PATH="$ENV/shim:$ENV/bin:$PATH"

chmod +x "$ENV/glibc/ld-linux-x86-64.so.2" "$ENV/shim/"* \
         "$ENV/bin/python3"* "$ENV/lib/swipl/bin/x86_64-linux/swipl" 2>/dev/null || true

if [ -z "${TPTP:-}" ]; then
  export TPTP="$ROOT/tptp-root"
fi

BENCHMARK="${1:?usage: starexec_run_default <benchmark> <output-directory> [time-limit-seconds]}"

if [[ "${2:-}" =~ ^[0-9]+([.][0-9]+)?$ ]]; then
  LIMIT="$2"
else
  LIMIT="${STAREXEC_CPU_LIMIT:-${STAREXEC_WALLCLOCK_LIMIT:-${STAREXEC_MAX_TIMEOUT:-300}}}"
fi

if [[ ! "$LIMIT" =~ ^[0-9]+([.][0-9]+)?$ ]]; then
  LIMIT=300
fi

exec sh "$ENV/shim/python3" "$ROOT/starexec_prover.py" \
  --strategy SATResetCoP \
  --sat-solver PySAT \
  --proof \
  --cpu-limit "$LIMIT" \
  "$BENCHMARK"
