]> git.rmz.io Git - dotfiles.git/blob - bin/crack_pdf
nvim: track lazy-lock
[dotfiles.git] / bin / crack_pdf
1 #! /bin/bash
2 usage()
3 {
4 cat << EOF
5 usage $0 options
6
7 This script will decrypt any pdf given to it. The correct password has to be passed.
8
9 REQUIRES qpdf
10
11 OPTIONS:
12 -h Shows this message
13 -p <password> The password to be used
14 -v Verbose output
15 EOF
16 }
17
18 # checks if 'qpdf' is installed
19 type -P qpdf &>/dev/null || { echo "I require qpdf but it's not installed. Aborting." >&2; exit 1; }
20
21 PWD=
22
23 while getopts "hp:v" OPT; do
24 case $OPT in
25 h)
26 usage
27 exit EXIT_SUCCESS
28 ;;
29 p)
30 PWD=$OPTARG
31 ;;
32 v)
33 VERBOSE=1
34 ;;
35 ?)
36 usage
37 exit
38 ;;
39 esac
40 shift $((OPTIND-1))
41 done
42
43 ARGS=$@
44 if [[ -z $ARGS ]] || [[ -z $PWD ]] || ! type -P qpdf &> /dev/null; then
45 usage
46 exit
47 fi
48
49 shopt -s nullglob # w/o this '*.pdf' will return the string if no pdfs are in the folder
50 for DST in $ARGS; do
51 if [[ -d $DST ]]; then
52 for F in $DST/*.pdf; do
53 [[ $VERBOSE ]] && echo $F
54 mv $F tmp.pdf
55 qpdf --password=$PWD --decrypt $F ${F%.pdf}_.pdf || echo "error"
56
57 done
58 elif [[ ${DST##*.} = "pdf" ]] && [[ -f $DST ]]; then
59 [[ $VERBOSE ]] && echo $DST
60 qpdf --password=$PWD --decrypt $DST ${DST%.pdf}_.pdf
61 fi
62 done