#!/usr/bin/env bash

shopt -s nullglob globstar

if (( ROFI_RETV == 0 ))
then
    echo -en "\0prompt\x1fPassword for\n"
    prefix=${PASSWORD_STORE_DIR-~/.password-store}
    password_files=( "$prefix"/**/*.gpg )
    password_files=( "${password_files[@]#"$prefix"/}" )
    password_files=( "${password_files[@]%.gpg}" )

    printf '%s\n' "${password_files[@]}"
else
    typeit=0
    (( ROFI_RETV == 2 )) && typeit=1
    password="$1"
    echo "$@" >&2
    echo "$ROFI_RETV" >&2

    [[ -n $password ]] || exit

    if (( typeit == 0 ))
    then
        coproc pass show -c "$password" &>/dev/null
    else
        coproc ( pass show "$password" | { read -r pass; printf %s "$pass"; } |
            xdotool type --clearmodifiers --file - )
    fi
fi
