-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpdf-decrypt.bash
executable file
·45 lines (41 loc) · 1.08 KB
/
pdf-decrypt.bash
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
#!/bin/bash
__NAME__=pdf-decrypt
function pdf-decrypt() {
function pdf-decrypt-error-handler() {
local errno=$1
if [ $errno -gt 0 ]; then
echo "The program 'qpdf' exited with code $errno!" > /dev/stderr
echo "Press Enter to exit." > /dev/stderr
read
exit $errno
fi
}
case $1 in -h|--help|''):
echo Usage: \"$0 [file]\".
echo Removes password-protection from PDF file called [file].
exit 0
;;
esac
if [ -f "$1" ]; then
local file="$1"
local encryptionStatus="$(qpdf --show-encryption \"$file\" 2>/dev/null)"
local tempFile=""
local password=""
if [[ "$encryptionStatus" == "File is not encrypted" ]]; then
echo $encryptionStatus.
else
echo "Password: "
read password
tempFile=$(mktemp)
qpdf --password="$password" --decrypt "$file" "$tempFile" && \
mv "$tempFile" "$file"
pdf-decrypt-error-handler $?
fi
else
echo \"$1\" is not a file. > /dev/stderr
exit 1
fi
}
if [[ $0 == *${__NAME__}* ]]; then
$__NAME__ "$@"
fi