#!/bin/sh
# summary: Manage private clipboard history.
# starchaser clipboard: what the clipboard history (Super+V) keeps.
#
#   starchaser clipboard store    keep one copy (data on stdin), unless a password manager marked it secret
#   starchaser clipboard prune    forget copies made more than an hour ago
#
# The work is done by clip-store.sh (this account's own copy in ~/.config/hypr/scripts, else Starchaser's in
# config/hypr/scripts), which hyprland.lua's wl-paste watchers run directly. This name is kept for anything that
# calls it by name, and for the clipboard picker, which can't find Starchaser's folder by itself.
usage() {
    echo "Usage: starchaser clipboard store|prune|list|clear|pin ID|unpin ID|pause|resume|status|history on|off"
    echo
    echo "store: keep one copy in the clipboard history (data on stdin), unless a password manager marked it secret."
    echo "history on|off: keep a clipboard history (on, the default: copies are forgotten after 1 hour) or keep nothing."
    echo "prune: forget copies made more than 1 hour ago."
}
impl="$HOME/.config/hypr/scripts/clip-store.sh"
[ -x "$impl" ] || { self=$(readlink -f "$0" 2>/dev/null) && impl="${self%/*}/../config/hypr/scripts/clip-store.sh"; }
for a in "$@"; do
    case "$a" in -h|--help|help) usage; exit 0 ;; esac
done
case "${1:-}" in
    list|clear|pin|unpin|pause|resume|status|history)
        self=$(readlink -f "$0")
        exec python3 "${self%/*}/starchaser_clipboard.py" "$@" ;;
esac
if [ "$#" -gt 1 ]; then
    echo "starchaser clipboard: unexpected argument '$2'." >&2
    usage >&2
    exit 2
fi
case "${1:-}" in
    store) if [ -x "$impl" ]; then exec "$impl"; fi
           # no raw helper text (a missing cliphist or a cache it can't write says one plain line)
           if ! command -v cliphist >/dev/null 2>&1; then
               echo "starchaser clipboard: the clipboard history helper (cliphist) isn't installed." >&2
               exit 1
           fi
           if ! cliphist store 2>/dev/null; then
               echo "starchaser clipboard: couldn't save to the clipboard history. Check that you can write to ~/.cache." >&2
               exit 1
           fi
           [ ! -t 1 ] || echo "Kept one copy in the clipboard history." ;;
    prune) if [ -x "$impl" ]; then exec "$impl" prune; fi
           exit 0 ;;
    "") usage >&2; exit 2 ;;
    -h|--help|help) usage ;;
    *) echo "starchaser clipboard: unknown command '${1:-}'. Run 'starchaser clipboard --help' for more help." >&2; usage >&2; exit 2 ;;
esac
