-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfmarks
executable file
·99 lines (89 loc) · 3.18 KB
/
pdfmarks
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env bash
source "common"
show_help() {
printf "usage: $PROGRAM_NAME [-h] [-v] [-s=SEP] FILE [COMMAND]\n"
printf " -h\t\tshow this help screen\n"
printf " -v\t\tincrease verbosity\n"
printf " -s=SEP\tseparate arguments of COMMAND using the characters of SEP instead of space\n"
printf "\n"
printf "Renumbers the pages of FILE according to COMMAND, where COMMAND is of the format:\n"
printf " [ <physical_page> <logical_page> [<page_style>] [<page_label>] [<physical_page_end:logical_page_step>] ]*\n"
printf "where\n"
printf " <physical_page> is a physical page number;\n"
printf " <logical_page> is a logical page number;\n"
printf " [<page_style>] is one of 'D' (decimal Arabic), 'R' (upper-case Roman), 'r' (lower-case Roman), 'A' (upper-case letters), 'a' (lower-case letters), or '-' for unspecified;\n"
printf " [<page_label>] is a label for prefixing page numbers.\n"
printf " [<physical_page_end:logical_page_step>] steps logical page numbers by <logical_page_step> from physical page <physical_page> until <physical_page_end> (instead of by one).\n"
printf "Arguments in COMMAND may alternatively be delimited by the '#' charater, rather than by spaces (the ' ' character).\n"
printf "If the COMMAND argument is not specified, then it is read as a single line from standard input.\n"
}
verbosity=0
command_arg_separator=' '
OPTIND=1
while getopts "h?vs=" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
v)
((verbosity+=1))
;;
s)
command_arg_separator="$OPTARG"
;;
esac
done
shift $(( OPTIND - 1 ))
[[ "$1" == "--" ]] && shift
input_file="$1"
numbering_command="$2"
if [[ -z "$input_file" ]]; then
failwith "FILE must be specified"
exit 10
fi
if [[ -z "$numbering_command" ]]; then
read -r numbering_command
if [[ -z "$numbering_command" ]]; then
failwith "COMMAND must be specified"
exit 11
fi
fi
trap_exit_on
output_file="$(mktemp "/tmp/$(basename "$input_file").XXXX")"
numbering_tokens=""
delim=','
while IFS="$command_arg_separator" read -r -d "$delim" p1 p2 p3 p4 p5; do
physical_page="$p1"
logical_page="$p2"
page_style="null"
page_label="null"
if [[ -n "$p3" && "$p3" != "-" ]]; then
page_style="/$p3"
fi
if [[ -n "$p4" && "$p4" != "-" ]]; then
page_label="($p4)"
fi
if [[ -n "$p5" && "$p5" != "-" ]]; then
IFS=":" read -r physical_page_end logical_page_step <<< "$p5"
while (( $physical_page <= $physical_page_end )); do
numbering_tokens+=" [ $physical_page $logical_page $page_style $page_label ]"
(( physical_page += 1 ))
(( logical_page += logical_page_step ))
done
continue
fi
numbering_tokens+=" [ $physical_page $logical_page $page_style $page_label ]"
done <<< "$numbering_command$delim"
echo "${numbering_tokens[@]}" >&2
# exit
(( $verbosity )) && echo "Writing output to temporary file \`$output_file\`..."
gs_args=""
gs $gs_args -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="$output_file" "$input_file" "pdfmarks.ps" << EOF
/Numberings [$numbering_tokens] def
do_renumbering
EOF
exit_code=$?; echo; (( $exit_code == 0 )) || exit $exit_code
(( $verbosity )) && echo "Moving temporary file \`$output_file\` back to \`$input_file\`."
mv -f "$output_file" "$input_file" &&
echo "Successfully renumbered pages of \`$input_file\`."