#!/usr/bin/env bash
# shot — one-call HD screenshot that prints ONLY a PNG path (keeps base64 out of the
# AI's context). Installed to ~/.local/bin/shot by adom/hd-mac-bootstrap.
#
#   shot                       # whole workspace (all panels)
#   shot screen                # entire screen (desktop, other apps)
#   shot panel --name "Foo"    # one panel's content
#   shot workspace --reason "verify the flow viewer rendered"
#
# WHY: `adom-cli hydrogen screenshot ...` prints a ~300 KB base64 dataUrl to stdout.
# Piped raw into an AI conversation that's ~85k tokens of noise per capture, plus a
# second decode step. This wrapper decodes in-line and emits ~20 bytes; the AI then
# Reads the path (images are attached efficiently by the Read tool).
set -euo pipefail
scope="${1:-workspace}"
[ $# -gt 0 ] && shift
out="${SHOT_OUT:-/tmp/shot-${scope}-$(date +%s).png}"
adom-cli hydrogen screenshot "$scope" "$@" 2>/dev/null | python3 -c "
import sys, json, base64
raw = sys.stdin.read()
try:
    d = json.loads(raw)
except Exception:
    sys.stderr.write('shot: unexpected CLI output: ' + raw[:300] + '\n'); sys.exit(1)
u = d.get('dataUrl') or d.get('data_url') or ''
if not u.startswith('data:image'):
    # No image — usually the sharing gate (approval needed) or an error. Surface the
    # CLI's own JSON so the caller sees WHY without re-running anything.
    sys.stderr.write('shot: no image returned: ' + json.dumps(d)[:400] + '\n'); sys.exit(1)
open('$out', 'wb').write(base64.b64decode(u.split(',', 1)[1]))
print('$out')
"
