-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpdf-decrypt
executable file
·86 lines (71 loc) · 2.04 KB
/
pdf-decrypt
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
#!/usr/bin/env bash
function pdf_decrypt() (
source "$DOROTHY/sources/bash.bash"
# =====================================
# Arguments
function help {
cat <<-EOF >/dev/stderr
ABOUT:
Take PDF <input>s that have passwords, and save copies that have the passwords removed.
USAGE:
pdf-decrypt [--password=<password>] ...<input>
EXAMPLE:
pdf-decrypt -- *.pdf
EOF
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}
# process
local item option_inputs=() option_password=''
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--password='*) option_password="${item#*=}" ;;
'--')
option_inputs+=("$@")
shift $#
break
;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*) option_inputs+=("$item") ;;
esac
done
# =====================================
# Dependencies
setup-util-qpdf --quiet
# =====================================
# Act
local index filepath filename
local filenames=()
for index in "${!option_inputs[@]}"; do
filepath="${option_inputs[index]}"
filename="$(fs-filename -- "$filepath")"
filenames[index]="$filename"
done
local password
password="$(
ask --required --password \
--question='Enter the decryption password for the PDFs' \
--question="$(echo-lines --columns -- "${filenames[@]}")" \
--default="$option_password"
)"
local outpath
for index in "${!option_inputs[@]}"; do
filepath="${option_inputs[index]}"
filename="${filenames[index]}"
outpath="$item [decrypted].pdf"
eval-helper --quiet \
--pending="$(echo-style --bold='Decrypting ' --code="$filepath" --bold=' to ' --code="$outpath")" \
--success="$(echo-style --bold+green='Decrypted ' --code="$filepath" --bold=' to ' --code="$outpath")" \
--failure="$(echo-style --bold+red='Failed to decrypt ' --code="$filepath" --bold=' to ' --code="$outpath")" \
-- qpdf -password="$password" -decrypt "$filepath" "$outpath" || :
done
)
# fire if invoked standalone
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
pdf_decrypt "$@"
fi