#!/usr/bin/env bash
# save_clip.sh <expected-chars> <out-file> — write the clipboard to a file only if its length matches.
# copied=true from the page does not prove the system clipboard changed; characters, not bytes, are compared.
set -euo pipefail
expect="$1"; out="$2"
got=$(pbpaste | wc -m | tr -d ' ')
if [ "$got" -ge $((expect - 100)) ] && [ "$got" -le $((expect + 100)) ]; then
  mkdir -p "$(dirname "$out")"; pbpaste > "$out"; echo "ok $got chars → $out"
else
  echo "STALE: expected ~$expect chars, clipboard has $got; click the page and copy again"; exit 1
fi
