summarylogtreecommitdiffstats
path: root/pdf-reverse.sh
blob: a8c24e2f51d0f5bfc8cf8ce3b697ccdf86e6902a (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
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
#!/bin/bash

### Author: Anonymous.

extendedhelp="### $0
# 
# Takes a pdf file, and reverses it's page order.
# 
###"

output_file_default='/dev/stdout'

errmsg() {
  if [ "$#" -ge "1" ]; then
    msg="$1"
  else
    msg="$0: Unspecified error."
  fi
  
  echo "${msg}" >> /dev/stderr
}

exiterror() {
  if [ "$#" -ge "1" ]; then
    msg="$1"
  else
    msg="$0: Unspecified error."
  fi
  
  if [ "$#" -ge "2" ]; then
    exitcode="$2"
  else
    exitcode=2
  fi
  
  errmsg "${msg}"
  exit "${exitcode}"
}

printusage() {
  echo "Usage:"
  echo "  $0 <pdf_input_file> [<pdf_output_file> [<additional_pdftk_options>]]"
  echo "(<pdf_output_file> defaults to '${output_file_default}')"
  echo "For example, specify 'verbose' as an additional pdftk-option to get verbose output."
}

printextendedhelp() {
  echo "${extendedhelp}"
}

if [ "$#" -lt "1" ]; then
  errmsg "$0: Error: Too few arguments specified."
  errmsg ""
  errmsg "$(printusage)"
  errmsg ""
  errmsg "=== Extended information on this software: ==="
  errmsg ""
  errmsg "$(printextendedhelp)"
  errmsg ""
  exiterror "$0: Aborting, since too few arguments specified." 1
fi

input_file="$1"

shift

if [ "$#" -ge "1" ]; then
  output_file="$1"
  shift
else
  output_file="${output_file_default}"
fi

pdftk "${input_file}" cat end-1 output "${output_file}" "$@"
exitcode_pdftk="$?"

if [ "${exitcode_pdftk}" -ne 0 ]; then
  errmsg "$0: Error: The run of 'pdftk' produced an error. See above error message from the call to 'pdftk' for details."
fi
exit "${exitcode_pdftk}"