-
Notifications
You must be signed in to change notification settings - Fork 1
/
dpassmenu
81 lines (67 loc) · 2.64 KB
/
dpassmenu
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
#!/bin/sh
# A simple dmenu script for ppassdb
# Help Page
helpPage() {
echo "A simple dmenu script for ppassdb
Usage: ${0##*/} [options]
Options:
--help To show this help message
--otp Generate of the an OTP code of the
encrypted text.
--filter=<PATTERN> Filters files only with a certain
pattern
--hide-gpg-extension Hides .gpg at the end on every
files
--sort Sort all files and folders together
For full documentation, refer to the README.md file at
https://github.com/jamez2128/ppassdb/blob/master/README.md"
}
# Parses all the arguments
while [ -n "$1" ] ; do
case $1 in
--otp) convertToOtpCode="--otp";;
--sort) sortOption=" " ;;
--filter=*) filter="${1#*=}" ;;
--hide-gpg-extension) hideExtension=" " ;;
--help) helpPage ; exit ;;
esac
shift
done
# This variable is defined to the name of the main script
# if you renamed the script, make sure it is the same as this variable
scriptName="ppassdb"
# Checks for dependencies
command -v "$scriptName" > /dev/null || scriptName="./$scriptName"
command -v "$scriptName" > /dev/null ||
{ notify-send "The ${scriptName##*/} script could not be found" && exit 127 ; }
command -v dmenu > /dev/null || { notify-send "dmenu is not installed" && exit 127 ; }
# Gets the full path of the script
scriptName=$(which "$scriptName")
# Exit the script if dmenu is already running
pgrep -x dmenu && exit 1
# Exit the script if it does not found any encrypted files
"$scriptName" list --notify > /dev/null || exit 1
# Gets the path of the encrypted files
ppassdbHOME="$("$scriptName" location)"
# Stops the loop until it finds a file
unset prompt
prompt="$ppassdbHOME"
while [ -d "$prompt" ] ; do
cd "$prompt" || { echo "Directory doesn't exist" ; exit 2 ; }
prompt=$(pwd)
[ -n "$filter" ] && findName="$filter" || findName="*"
findFormat=$(find . -maxdepth 1 -type d | sed "1d; s/.*\///g; s/.*$/&\//g" | sort ; find . -maxdepth 1 -name "$findName" ! -type d | sed "s/.*\///g" | sort)
[ -n "$hideExtension" ] && findFormat=$(echo "$findFormat" | sed 's/\.gpg$//g')
[ -n "$sortOption" ] && findFormat=$(echo "$findFormat" | sort)
dmenuFormat=$([ ! "$prompt" = "$ppassdbHOME" ] && echo ".." ; echo "$findFormat")
[ "$prompt" = "$ppassdbHOME" ] && dmenuPromptMessage="" || dmenuPromptMessage="${prompt#"$ppassdbHOME/"}"
prompt="$(pwd)/$(printf "%s" "$dmenuFormat" | dmenu -p "$dmenuPromptMessage")" || exit 1
done
# Exit the script if it is not a file
if [ -f "$prompt.gpg" ] ; then prompt="$prompt.gpg"
elif [ ! -f "$prompt" ] ; then exit 1
fi
echo "$prompt"
# The decryption part
"$scriptName" open --notify --clear $convertToOtpCode -- "${prompt#"$ppassdbHOME/"}" ||
{ notify-send "Decryption failed" ; exit 1 ; }