~chambln/dots

a6330adef9b91bef86314a7e296d7887eca264a8 — Gregory Chamberlain 14 days ago bea1aeb
[WIP] pw
1 files changed, 40 insertions(+), 0 deletions(-)

A sh/bin/pw
A sh/bin/pw => sh/bin/pw +40 -0
@@ 0,0 1,40 @@
#!/bin/sh -e

while getopts h OPT; do
    case $OPT in
        ?) printf 'Usage: %s [-h] ENTRY...\n' "${0##*/}" >&2; exit
    esac
done
shift $((OPTIND - 1))
set -- "${@:-*}"

IFS='|'
set -f
set +f -- $1

# Expand search patterns.
mkdir -p "${PW_DIR:=${XDG_DATA_DIR:-$HOME/.local/share}/pw}"
cd "$PW_DIR"
for pattern do
    shift
    case $pattern in
        ^*$) pattern=${pattern#^} pattern=${pattern%$} ;;
        ^*)  pattern=${pattern#^}* ;;
        *$)  pattern=*${pattern%$} ;;
        * )  pattern=*${pattern}*
    esac
    set -- "$@" $pattern.gpg
done

# Drop duplicates and unexpanded globs, and strip ‘.gpg’ suffixes.
IFS='
'
for arg do
    shift
    case "$IFS$*$IFS" in
        *"$IFS$arg$IFS"*) ;;
        *) [ -e "$arg" ] && set -- "$@" "${arg%.gpg}"
    esac
done

printf '%s\n' "$@"