aboutsummarylogtreecommitdiffstats
path: root/pdf-decrypt.bash
blob: 3bae3db6f7e8efff0fa172478e3f65652c0486fa (plain)
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
#!/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
  }
  local errno=''
  case $1 in -h|--help|''):
    echo Usage: \"$0 [file]\".
    echo Removes passwort-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 "Passwort: "
          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