#!/bin/sh
# rikkiti-clipd — the Rikkiti clipboard recorder.
#
# Starts two `wl-paste --watch` watchers (text + image/png) that pipe each new
# clipboard selection into rikkiti-clip-store, which appends it to the history
# store (~/.local/share/rikkiti/clipboard/). Runs from the session autostart.
#
# Idempotent: kills any previous rikkiti-clipd watchers first, so re-running it
# (or autostart firing twice) never leaves duplicate watchers. POSIX sh.
set -eu

DIR="${XDG_DATA_HOME:-$HOME/.local/share}/rikkiti/clipboard"
mkdir -p "$DIR/items"

# Kill any previous watchers from an earlier instance (match the watch commands,
# not this script's own name, so we don't suicide).
pkill -f 'wl-paste --watch rikkiti-clip-store' 2>/dev/null || true

# A Wayland display is required (wl-paste talks wlr-data-control). Bail quietly if
# there's none (e.g. started outside a session).
if [ -z "${WAYLAND_DISPLAY:-}" ]; then
	echo "rikkiti-clipd: no WAYLAND_DISPLAY — not starting" >&2
	exit 1
fi

# Text + image watchers. --watch re-runs the store command on every new
# selection, feeding the content on its stdin.
wl-paste --type text --watch rikkiti-clip-store text &
wl-paste --type image/png --watch rikkiti-clip-store image &

wait
