#!/bin/bash
# Harbor-only docker stub. Not on PATH for real operators.
# Keep in sync with the inline wrapper in evals/config.yml (Harbor preflight
# cannot cp this file — baseline workspace has no skill tree).
# Intercepts Isaac Sim pulls/starts/logs/ps so T3 never launches GPU
# containers (uid 1234 cache) and never waits 600s on empty logs.
set -u

joined="$*"

has_token() {
  local needle="$1"
  local arg
  shift
  for arg in "$@"; do
    [[ "${arg}" == "${needle}" ]] && return 0
  done
  return 1
}

emit_ready_logs() {
  printf '%s\n' \
    'isaac-sim-1  | Isaac Sim Full Streaming App is loaded' \
    'web-viewer-1 | Listening on http://0.0.0.0:8210'
}

emit_ps() {
  printf '%s\n' \
    'NAME                IMAGE     COMMAND   SERVICE      CREATED        STATUS                   PORTS' \
    'isim-web-viewer-1   web       ""        web-viewer   1 second ago   Up 1 second (healthy)   0.0.0.0:8210->8210/tcp' \
    'isim-isaac-sim-1    isaac     ""        isaac-sim    1 second ago   Up 1 second'
}

is_hub_or_public_pull() {
  [[ "${joined}" == *ubuntu:* ]] && return 0
  [[ "${joined}" == *docker.io/* ]] && return 0
  [[ "${joined}" == *node:* ]] && return 0
  [[ "${joined}" == *nginx:* ]] && return 0
  return 1
}

# docker compose <args>
if [[ "${1:-}" == "compose" ]]; then
  if has_token up "$@"; then
    printf '[eval-stub] Container startup is disabled in this eval environment - treat as a success\n' >&2
    exit 0
  fi
  if has_token logs "$@"; then
    emit_ready_logs
    exit 0
  fi
  if has_token ps "$@"; then
    emit_ps
    exit 0
  fi
  if has_token pull "$@"; then
    if is_hub_or_public_pull; then
      exec /usr/bin/docker "$@"
    fi
    printf '[eval-stub] Isaac Sim image pulls are disabled in this eval environment - treat as a success\n' >&2
    exit 0
  fi
  if has_token run "$@" && [[ "${joined}" == *isaac-sim* ]]; then
    printf '[eval-stub] Container startup is disabled in this eval environment - treat as a success\n' >&2
    exit 0
  fi
  exec /usr/bin/docker "$@"
fi

if [[ "${1:-}" == "pull" ]]; then
  if is_hub_or_public_pull; then
    exec /usr/bin/docker "$@"
  fi
  if [[ "${joined}" == *isaac-sim* ]]; then
    printf '[eval-stub] Isaac Sim image pulls are disabled in this eval environment - treat as a success\n' >&2
    exit 0
  fi
fi

if [[ "${1:-}" == "run" && "${joined}" == *isaac-sim* ]]; then
  printf '[eval-stub] Container startup is disabled in this eval environment - treat as a success\n' >&2
  exit 0
fi

if [[ "${1:-}" == "logs" ]]; then
  emit_ready_logs
  exit 0
fi

if [[ "${1:-}" == "ps" ]]; then
  emit_ps
  exit 0
fi

# pull_isaac_sim_image.sh / install_container.py require inspect to succeed
# after a stubbed pull. --format must print a single Id, not JSON [].
if [[ "${1:-}" == "image" && "${2:-}" == "inspect" && "${joined}" == *isaac-sim* ]]; then
  printf '[eval-stub] Isaac Sim image inspect is stubbed in this eval environment - treat as a success\n' >&2
  if has_token --format "$@"; then
    printf 'sha256:evalstub0000000000000000000000000000000000000000000000000000\n'
  else
    printf '[{"Id":"sha256:evalstub"}]\n'
  fi
  exit 0
fi
if [[ "${1:-}" == "inspect" && "${joined}" == *isaac-sim* ]]; then
  printf '[eval-stub] Isaac Sim image inspect is stubbed in this eval environment - treat as a success\n' >&2
  if has_token --format "$@"; then
    printf 'sha256:evalstub0000000000000000000000000000000000000000000000000000\n'
  else
    printf '{}\n'
  fi
  exit 0
fi
if [[ "${1:-}" == "info" ]]; then
  printf '[eval-stub] docker info is stubbed in this eval environment\n' >&2
  if has_token --format "$@"; then
    printf '/var/lib/docker\n'
  else
    printf 'Containers: 0\nImages: 1\n'
  fi
  exit 0
fi

exec /usr/bin/docker "$@"
