#!/usr/bin/env sh

shopt -s nullglob globstar

# Define the password store directory
prefix=${PASSWORD_STORE_DIR-~/.password-store}

# Find all password files and strip the prefix and .gpg extension
password_files=("$prefix"/**/*.gpg)
password_files=("${password_files[@]#"$prefix"/}")
password_files=("${password_files[@]%.gpg}")

# Use rofi to display the list and capture the user's selection
password=$(printf '%s\n' "${password_files[@]}" | rofi -dmenu -i -p "Select Password:")

# If a password was selected, copy it to the clipboard using 'pass -c'
if [[ -n $password ]]; then
    # The -c flag automatically copies the password to the clipboard
    pass show -c "$password"
fi
