]> git.rmz.io Git - dotfiles.git/blobdiff - bin/crack_pdf
merge bin from shada
[dotfiles.git] / bin / crack_pdf
diff --git a/bin/crack_pdf b/bin/crack_pdf
new file mode 100755 (executable)
index 0000000..21be931
--- /dev/null
@@ -0,0 +1,62 @@
+#! /bin/bash
+usage()
+{
+cat << EOF
+usage $0 options
+
+This script will decrypt any pdf given to it. The correct password has to be passed.
+
+REQUIRES qpdf
+
+OPTIONS:
+ -h                 Shows this message
+ -p <password>      The password to be used
+ -v                 Verbose output
+EOF
+}
+
+# checks if 'qpdf' is installed
+type -P qpdf &>/dev/null || { echo "I require qpdf but it's not installed.  Aborting." >&2; exit 1; }
+
+PWD=
+
+while getopts "hp:v" OPT; do
+    case $OPT in
+        h)
+            usage
+            exit EXIT_SUCCESS
+            ;;
+        p)
+            PWD=$OPTARG
+            ;;
+        v)
+            VERBOSE=1
+            ;;
+        ?)
+            usage
+            exit 
+            ;;
+    esac
+    shift $((OPTIND-1))
+done
+
+ARGS=$@
+if [[ -z $ARGS ]] || [[ -z $PWD ]] || ! type -P qpdf &> /dev/null; then
+    usage
+    exit
+fi
+
+shopt -s nullglob       # w/o this '*.pdf' will return the string if no pdfs are in the folder
+for DST in $ARGS; do
+    if [[ -d $DST ]]; then
+        for F in $DST/*.pdf; do
+            [[ $VERBOSE ]] && echo $F
+            mv $F tmp.pdf
+            qpdf --password=$PWD --decrypt $F ${F%.pdf}_.pdf || echo "error"
+
+        done
+    elif [[ ${DST##*.} = "pdf" ]] && [[ -f $DST ]]; then
+        [[ $VERBOSE ]] && echo $DST
+        qpdf --password=$PWD --decrypt $DST ${DST%.pdf}_.pdf
+    fi
+done