#!/bin/sh
# Drop-in stand-in for xclip inside the sandbox. Forwards clipboard image reads
# to the host clipboard bridge over the SSH-forwarded Unix socket (see
# `container run --ssh` in src/sbx.ts); no-ops text/copy so nothing errors.
out=0
target=""
prev=""
for a in "$@"; do
  case "$a" in
    -o|-out|-output) out=1 ;;
  esac
  [ "$prev" = "-t" ] && target="$a"
  prev="$a"
done

sock="${CLIP_SOCK:-$SSH_AUTH_SOCK}"
[ -z "$sock" ] && exit 0

if [ "$out" = "1" ]; then
  case "$target" in
    TARGETS)
      # HEAD: the bridge answers from pasteboard metadata without rendering
      # or transferring the image (see clipboardProbe in src/sbx.ts).
      code=$(curl -s -o /dev/null -w '%{http_code}' -I --unix-socket "$sock" http://clip/ 2>/dev/null)
      if [ "$code" = "200" ]; then
        echo TARGETS
        echo image/png
      fi
      ;;
    image/png|image/*|"")
      curl -fsS --unix-socket "$sock" http://clip/ 2>/dev/null
      ;;
    *) : ;;
  esac
  exit 0
fi

cat >/dev/null 2>&1
exit 0
