summaryrefslogtreecommitdiff
path: root/grep-passwort.in
blob: b0f638d9d28629b09c27fd3c2da855f7120e6392 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash

passstore_Verzeichnis=$(
  readlink -f ~/.password-store
)

Verwendung()
{
  >&2 echo 'Nach Passwort per Regex suchen.'
  >&2 echo ''
  >&2 printf 'Verwendung: %s [OPTIONS]\n' "$(basename "$0")"
  >&2 echo \
'#HELPTEXT#               #'
  >&2 echo ''
  exit $1
}

eval set -- "$(
  getopt -o h \
    --long help \
    --long version \
    -n "$(basename "$0")" \
    -- "$@" \
    || echo "Verwendung"
)"

while true; do
  case "$1" in
    '-h'|'--help')
      Verwendung 0
    ;;
    '--version')
      >&2 echo '#VERSION#'
      exit 0
    ;;
    '--')
      shift
      break
    ;;
    *)
      >&2 printf 'FEHLER: Verstehe Option "%s" doch nicht!\n' "$1"
      exit 1
  esac
  shift
done

if [ ! -d "${passstore_Verzeichnis}" ]; then
  >&2 printf 'Verzeichnis "%s" existiert nicht - ist pass initialisiert?\n' "${passstore_Verzeichnis}"
  exit 1
fi

if [ $# -ne 0 ]; then
  >&2 printf 'FEHLER: Zu viele (%s) Argumente:\n' "$#"
  >&2 printf '"%s"\n' "$@"
  Verwendung 1
fi

count=0
while [ ${count} -ne 1 ]; do
  read -p 'suche nach: ' Suche
  Ergebnisse=''
  for Versuch in 'name:' 'iname:' 'name:*' 'iname:*' 'path:*' 'ipath:*'; do
    if [ -n "${Ergebnisse}" ]; then
      break
    fi
    Ergebnisse=$(
      find "${passstore_Verzeichnis}" \
        -type f \
        -name '*.gpg' \
        -${Versuch%%:*} "${Versuch#*:}${Suche}${Versuch#*:}" \
        -printf '%P\n'
    )
  done
  if [ -z "${Ergebnisse}" ]; then
    >&2 echo 'Ich habe nichts finden können.'
    exit 1
  fi
  count=$(printf '%s\n' "${Ergebnisse}" | wc -l)
  if [ ${count} -ne 1 ]; then
    printf 'Es gab %s Übereinstimmungen:\n' "${count}"
    printf '  %s\n' ${Ergebnisse}
  fi
done

output=$(
  pass show "${Ergebnisse%.gpg}"
)

printf '%s\n' "${output}" \
| sed '
  s/^Username: //
  t
  d
' \
| xclip -i

printf "."

read dummy

printf '%s\n' "${output}" \
| head -n1 \
| xclip -i

printf "."

read dummy

echo '' \
| xclip -i