#!/usr/bin/env bash
set -euo pipefail

config_path="${VCLAW_SHARED_QUEUE_CONFIG:-${XDG_CONFIG_HOME:-$HOME/.config}/videoclaw/shared-queue.env}"
if [[ ! -f "$config_path" ]]; then
  echo "shared queue config is missing: $config_path" >&2
  exit 3
fi
# shellcheck disable=SC1090
source "$config_path"
if [[ -z "${VCLAW_SHARED_QUEUE_TOKEN:-}" ]]; then
  if ! command -v security >/dev/null 2>&1; then
    echo "VCLAW_SHARED_QUEUE_TOKEN is missing and macOS Keychain is unavailable" >&2
    exit 3
  fi
  VCLAW_SHARED_QUEUE_TOKEN="$(security find-generic-password -a default -s videoclaw-shared-queue -w)"
fi
# Callers that make many lane calls (a driver heartbeating every 45 s) should run
# this wrapper ONCE (`bash with-shared-queue.sh /usr/bin/env`) and carry the
# VCLAW_* variables forward, not re-read the Keychain per call — lane_render.py does.
export VCLAW_SHARED_QUEUE_URL VCLAW_SHARED_QUEUE_TOKEN VCLAW_MACHINE_ID
export VCLAW_LANE_ACCOUNT="${VCLAW_LANE_ACCOUNT:-main-unlimited}"
export VCLAW_SHARED_QUEUE_REQUIRED=1

if [[ "$#" -eq 0 ]]; then
  echo "usage: $0 <command> [args...]" >&2
  exit 2
fi
exec "$@"
