#!/usr/bin/env bash
# Requires Bash, curl and jq. Set ARBISCAN_API_KEY on your server.
set -euo pipefail
: "${ARBISCAN_API_KEY:?Set ARBISCAN_API_KEY}"
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
for restart in 1 2 3; do
  : > "$work/events.ndjson"
  : > "$work/cursors"
  cursor='' snapshot='' retry=0
  started=$SECONDS
  while :; do
    args=(--data-urlencode 'limit=500')
    if [[ -n "$cursor" ]]; then
      args+=(--data-urlencode "cursor=$cursor")
    else
      args+=(--data-urlencode 'sport=Football' --data-urlencode 'bookmakers=Betclic'
        --data-urlencode 'markets=h2h' --data-urlencode 'max_age_seconds=900')
    fi
    status=$(curl --silent --show-error --max-time 30 --get \
      'https://api.arbiscan.pl/v1/odds' --header "X-API-Key: $ARBISCAN_API_KEY" \
      "${args[@]}" --dump-header "$work/headers" --output "$work/page" --write-out '%{http_code}')
    code=$(jq -r '.code // empty' "$work/page" 2>/dev/null || true)
    if [[ "$status" == 409 && ( "$code" == snapshot_changed || "$code" == cursor_version_expired ) ]]; then
      break # The next attempt starts with an empty buffer.
    fi
    if [[ ( "$status" == 429 || "$status" == 503 ) && "$code" != total_quota_exceeded && $retry -lt 3 ]]; then
      delay=$(awk 'tolower($1)=="retry-after:" {gsub("\r", "", $2); print $2}' "$work/headers" | tail -1)
      # The API sends seconds. Stop on a date-valued proxy header; never retry early.
      [[ "$delay" =~ ^[0-9]+$ ]] || { echo 'Retry later; inspect Retry-After' >&2; exit 1; }
      (( SECONDS - started + delay < 230 )) || { echo 'Retry later; discard partial results' >&2; exit 1; }
      retry=$((retry + 1))
      sleep "$delay"
      continue
    fi
    [[ "$status" == 2?? ]] || { echo "HTTP $status $code; no partial output" >&2; exit 1; }
    retry=0
    current=$(jq -cS '.snapshot' "$work/page")
    [[ -z "$snapshot" || "$snapshot" == "$current" ]] || { echo 'Snapshot mismatch' >&2; exit 1; }
    snapshot=$current
    jq -c '.events[]' "$work/page" >> "$work/events.ndjson"
    if [[ $(jq -r '.has_more' "$work/page") == false ]]; then
      jq -s '.' "$work/events.ndjson"
      exit 0
    fi
    cursor=$(jq -er '.next_cursor | select(type == "string" and length > 0)' "$work/page")
    if grep -Fxq "$cursor" "$work/cursors"; then echo 'Repeated cursor' >&2; exit 1; fi
    printf '%s\n' "$cursor" >> "$work/cursors"
    (( SECONDS - started < 230 )) || { echo 'Traversal too slow' >&2; exit 1; }
  done
done
echo 'Restart budget exhausted; no partial output' >&2
exit 1
