#!/usr/bin/env bash
# ABOUTME: Rofi-based clipboard history manager using cliphist and wl-copy.
# ABOUTME: Lists clipboard entries and copies the selected one.

CLIPHIST_DB="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/cliphist/db"

function start_rofi() {
    local content="$1"
    echo -e "$content" | rofi -dmenu -theme /etc/xdg/rofi/themes/clipboard.rasi -mesg "󰅍  Clipboard History"
}

function main() {
    local entries
    local result

    entries=$(cliphist -db-path "$CLIPHIST_DB" list)
    result=$(start_rofi "$entries")

    if [ -n "$result" ]; then
        echo "$result" | cliphist -db-path "$CLIPHIST_DB" decode | wl-copy
    else
        exit 1
    fi
}

main
