#!/usr/bin/env bash

## Edit a password file using the password manager `pass`, but instead of
## entering the password name as usual, select the password file using fzf.
##
## Usage: pass-edit
##
## Dependencies: fzf, GNU sed, pass

set -euo pipefail
shopt -s inherit_errexit

die() { echo "${0##*/}: $1" >&2; exit 1; }

[[ " $* " =~ ' --help ' ]] && sed -n 's/^## \?//p' "$0" && exit
(( $# )) && die 'invalid argument'

# Run `pass edit`, but not in a pipeline (using xargs) because doing so will
# result in a GnuPG error if the private key hasn't already been decrypted.
pw_file=$(cd "${PASSWORD_STORE_DIR:-~/.password-store}"; fzf)
pass edit "${pw_file%.gpg}"
