#!/usr/bin/env bash
# podman — GENERATED by generate-tool-shims from tools.json rule "raw-podman-in-sandbox".
# OD_PATH_SHIM_MARKER OD_TOOL_SHIM_MARKER — do not hand-edit; re-run generate-tool-shims.
# Registry hash at generation time: 1c67ba144616
set -e

name="${0##*/}"
SHIM_DIR="$(cd "$(dirname "$(readlink -f -- "${BASH_SOURCE[0]}")")" && pwd)"

GUARD_LIB="$SHIM_DIR/../lib/shim-guard.sh"
if [[ ! -r "$GUARD_LIB" ]]; then
  echo "$name: tool shim cannot read $GUARD_LIB — refusing" >&2
  exit 78
fi
# shellcheck source=../lib/shim-guard.sh
source "$GUARD_LIB"
shim_guard_enter "$name"

if ! shim_pin_read "podman"; then
  echo "$name: tool shim has no usable pinned podman (looked in: $SHIM_PIN_TRIED) — run $SHIM_DIR/install-tool-shims-real" >&2
  exit 78
fi
real="$SHIM_PIN_REAL"
shim_guard_clear "$name"

if [[ "$SHIM_REENTRY" == 1 ]]; then
  exec "$real" "$@"
fi

# Matched against "$name $*", not just "$*": the rule text (ported verbatim from
# tools.json) was written to match a full typed command line INCLUDING the binary
# name — args alone would never contain literals like "ccr" or "podman".
cmd="$name $*"
# The pattern MUST be held in a variable, not written inline in [[ =~ <literal> ]]:
# bash strips backslash escapes from an inline pattern before regcomp ever sees them,
# silently turning a boundary/whitespace class into a no-match. A variable reference
# is passed through unquoted-word-split, unmangled.
_pattern='((^|[;&|]\s*)(/usr/bin/|/bin/)?podman(-compose)?\s)'
if [[ "${AGENT_BUILD_SCOPE_ACTIVE:-}" == "1" ]] && [[ "$cmd" =~ $_pattern ]] && ! [[ "$cmd" == *"deck-podman"* || "$cmd" == *"systemd-run"* ]]; then
  cat >&2 <<'MSG'
tool-shim (raw-podman-in-sandbox): Agent Bash runs inside a bwrap user namespace whose uid_map maps a single uid, with no_new_privs set, so the setuid newuidmap helper cannot widen it. Rootless podman cannot build a pause namespace and dies with '\''unable to create a new pause process: cannot re-exec process to join the existing user namespace'\''. Its own advice is wrong and destructive here: `podman system migrate` STOPS EVERY RUNNING CONTAINER (the owner'\''s work), and a reboot fixes nothing because the sandbox is not machine state. Every raw attempt also leaks a podman-pause-*.scope and a catatonit that nothing reaps. deck-podman re-execs through the user manager where the uid range is intact, and fails closed on what it cannot forward faithfully (sandbox-private /tmp paths, interactive ttys).
Use instead: deck-podman <podman-args...>   (passthrough when unconfined, so it is always safe to use)
MSG
  exit 76
fi

exec "$real" "$@"
