5 # obtain a password from ~/pw.gpg
7 # Copyright © 2011 martin f. krafft <madduck@madduck.net>
8 # Released under the terms of the Artistic Licence 2.0
10 # Source repository: git://git.madduck.net/etc/zsh.git
13 local PWENCFILE=${HOME}/pw.gpg
16 [ -x $(command -v xclip) ] || clip=0
19 while getopts :po opt; do
21 \?) echo >&2 "E: unrecognised option: -$OPTARG"; return 1;;
26 (( OPTIND > 1 )) && shift $(( OPTIND - 1 ))
28 if [[ $clip = 1 ]]; then
29 dest='tr -d \\n | xclip -in'
34 if (( $# == 1 )); then
36 *@*) local identity="${1%@*}"
37 local resource="${1##*@}"
39 *) local resource="$1";;
46 local IFSOLD="$IFS"; IFS=" "
48 gpg --decrypt --batch --quiet "$PWENCFILE" | \
50 output=$(while read r i p o; do
52 (*${resource:l}*/${identity:+*${identity:l}}*)
53 echo "'$r' '$i' '$p' '$o'";;
57 [ -z "$output" ] && return 0
60 results=(${(f)output})
63 if (( ${#results} == 1 )); then
64 result=(${(z)${results[1]}})
66 [[ $incother = 1 ]] && output="$output ${result[4]}"
67 eval echo "$output" | eval $dest
68 [[ $clip = 1 ]] && eval echo >&2 "match for ${result[1]}, ID ${result[2]} put onto X clipboard."
70 if [[ $clip = 1 ]]; then
71 echo >&2 "E: multiple matches, hence not putting onto clipboard; use -p option."
76 output="${result[2]}@${result[1]} ${result[3]}"
77 [[ $incother = 1 ]] && output="$output ${result[4]}"