-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpr
385 lines (332 loc) · 9.83 KB
/
mpr
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/bin/bash
###################################################
## mpr ##
## Mutifile duplex printing, one at a time for a ##
## dedicated printer on a workstation ##
## Calling sequence: ##
## mpr [-i] <print-file> ... ##
## ##
## Uses default print strategy ##
## ##
## Copyright (c) 27 Sep 2015 Joseph J. Pollock ##
## JPmicrosystems - josephj at main.nc.us ##
## Last Modified 07/24/2017 ##
## ##
## This program is free software; you can ##
## redistribute it and/or modify it under the ##
## terms of the GNU General Public License as ##
## published by the Free Software Foundation; ##
## either version 2 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope ##
## that it will be useful, but WITHOUT ANY ##
## WARRANTY; without even the implied warranty ##
## of MERCHANTABILITY or FITNESS FOR A ##
## PARTICULAR PURPOSE. See the GNU General ##
## Public License for more details. ##
## ##
## You should have received a copy of the ##
## GNU General Public License along with this ##
## program; if not, write to ##
## the Free Software Foundation, Inc. ##
## 59 Temple Place - Suite 330 ##
## Boston, MA 02111-1307, USA ##
###################################################
function quit () {
## clean up loose ends and exit
##
rm -f ${tmpfile}
exit $rc
}
function user_abort () {
## clean up loose ends from user abort
##
echo
echo " Aborted by User"
echo
rc=1
quit
}
function get_print_file_type () {
## What type of printable? file is $1
## Return 0 on success
## 1 on failure
## Return print_file_type
## 0 if file does not exist
## or can't be read
## 1 if file is PostScript
## 2 if file is text
## 3 if file is PDF
## 255 if other
local str
if [[ ! -r "${1}" ]] # if the file can't be read
then
print_file_type=0
return 1
fi
str=$(file -Lb "${1}")
## echo "str=["${str}"]"
case "${str}" in
*"PostScript"*"text"*)
print_file_type=1
return 0
;;
*"text"*)
print_file_type=2
return 0
;;
*"PDF"*)
print_file_type=3
return 0
;;
*)
print_file_type=255
return 1
;;
esac
}
function page_count () {
unset page_ct
if [[ ! -r "${1}" ]] # if the file can't be read
then
return 1
fi
get_print_file_type "${1}"
case "${print_file_type}" in
1) ## PostScript
ps_page_ct "${1}"
return $?
;;
3) ## PDF
pdf_page_ct "${1}"
return $?
;;
*) ## Anything else fails
return 1
;;
esac
}
function non_numeric () {
## Test string for non_numeric
## For use with (( )) (opposite of [ ])
local var
##echo "Testing [${1}]" ## debug
[[ -z "${1}" ]] && return 1
var="$(echo "${1}" | sed -re 's/[0-9][0-9]*//')"
[[ -z "${var}" ]]
return $?
}
function pdf_page_ct () {
local magic page_ct0 rc
if [[ ! -r "${1}" ]]
then
return 1
fi
magic="$(head -1 "${1}" | cut -d '-' -f 1)"
if [[ $magic != "%PDF" ]]
then
return 1
fi
page_ct0=$(pdfinfo "${1}" 2>/dev/null | grep 'Pages:' | awk '{print $2}')
rc=$?
(( rc )) && return ${rc}
if (( page_ct0 ))
then
page_ct=${page_ct0}
return 0
fi
return 1
}
function ps_page_ct () {
## extract page count from postscript file
## Return 0 on success
## 1 on failure
## Number of pages in page_ct (unset if not found)
## This is totally seat of the pants - no guarantees
## First, try to find it at the tail of the file.
## If that fails, try at the head
## If that fails, count the nummber of showpage commands and cross your fingers
## This method is not guaranteed to work because one showpage could be in a function
## called repeatedly, etc.
local args line page_ct0
##echo starting page count
unset page_ct page_ct0 ## Clear the return result in case an error occurs
line=$(tail -30 "${1}" | grep "%%Pages:") ## Find the line containing %%Pages: nn
##echo Line = "[${line}]" > /dev/stderr
args=(${line}) ## Put it into an array to extract the second field - number of pages
if [[ "${args[0]}" == "%%Pages:" ]]
then
page_ct0=$(( ${args[1]} ))
if (( page_ct0 ))
then
page_ct="${page_ct0}"
return 0
fi
fi
## If at first you don't succeed, try, try again
line=$(head -30 "${1}" | grep "%%Pages:") ## Find the line containing %%Pages: nn
##echo Line = "[${line}]" > /dev/stderr
args=(${line}) ## Put it into an array to extract the second field - number of pages
if [[ "${args[0]}" == "%%Pages:" ]]
then
page_ct0=$(( ${args[1]} ))
if (( page_ct0 ))
then
page_ct="${page_ct0}"
return 0
fi
fi
## The third time is the charm
page_ct0="$(grep -c 'showpage' "${1}" 2>/dev/null)"
##page_ct0="$(grep -c 'HiResBoundingBox' "${1}" 2>/dev/null)"
if (( ! $? )) && (( page_ct0 ))
then
page_ct="${page_ct0}"
return 0
fi
return 1
}
function ynq () {
# Yes/No Question
# Usage: ynq <prompt>
# set C style return code
# Returns 1 for yes and 0 for no - for use with (( ))
# Enter, by itself, means Yes
local prompt="$*"
local reply
read -n 1 -p "$prompt" reply
[[ -n ${reply} ]] && echo ## Just got one character, so add a newline
case "$reply" in
[yY1] | "" )
return 1
;;
* )
return 0
;;
esac
}
function mypath () {
## Get the real path of the calling script
## From: https://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within/246128#246128
my_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
}
##################################################################
## Main Program ##
##################################################################
##source $HOME/bin/bash_trace ## debug - TRACE
trap 'user_abort' 2 ## Call user_abort() if user presses ctrl-c
script_error=9 ## error code for script/system errors
script_name=$(basename ${0}) ## path stripped name of this script
mypath
script_path="${my_path}" ## make sure we can find other scripts
if (( $? )) # having an identity crisis
then
echo "Can't find path to scripts"
exit "${script_error}"
fi
if (( ! $# )) || [[ "$1" == '*' ]]
then
echo "Tell me what to print.
I don't mean to be difficult, but every now and again,
I need a good argument or two!
"
exit 0
fi
## enscript parameters
## In moderate point sizes (12+), the header font may need to be proportional
## To show the whole header
## A non-proportional font for the body works best for structured text
body_font="Luxi-Mono" ## font for enscript for non-postscript files
header_font="Times-Roman" ## font for enscript for non-postscript files
font_size=14 ## point size for enscript
margins="50:50:50:50" ## 100ths of inch top, bottom, left, right for enscript
## Create temp file for enscript output
tp="/tmp" # temp file path
tmpfile=$(/bin/mktemp -q $tp/${script_name}.XXXXXX) # create temp file
if (( $? )) # if create tempfile failed
then
echo "Can't create temp file"
echo "Check permissions for ${tp}\n\nAborting..."
exit "${script_error}"
fi
total_pages=0 ## Total pages printed
total_sheets=0 ## Total sheets printed
run=1 ## if true (1) - actually print jobs
if (( $# )) && [[ "$1" == "-i" ]]
then
run=0 ## just report pages to be printed and quit
shift
fi
dplx="${script_path}/dplx"
##dplx="${HOME}/pgm/duplex_proj/devel/current/dplx" ## debug test version
echo "Duplex printing $@"
i=0
for file in "$@"
do
if [[ ! -r "${file}" ]]
then
echo "File not found"
echo "Skipping [${file}]"
continue
fi
get_print_file_type "${file}"
rc=$?
if (( rc ))
then
echo "Unsupported file type"
echo "Skipping [${file}]"
continue
fi
fn="${file}"
if (( print_file_type == 2 )) ## If it's text
then ## Convert it to postscript
enscript -B -Z --silent --word-wrap --margins=$margins -f${body_font}@${font_size} -F${header_font}@${font_size} -o ${tmpfile} "${file}"
fn=${tmpfile}
fi
page_count "${fn}" ## Get Number of pages to print
if (( $? ))
then
##echo "#### Debug page_ct = [${page_ct}]"
echo "page_count() failed"
echo "Skipping [${file}]"
continue
fi
(( i++ ))
(( sheets = page_ct / 2 ))
(( page_ct % 2 )) && (( sheets++ ))
printf "Printing %2d of %2d - [%s] - %2d pages %2d sheets\n" "${i}" "$#" "${file}" "$page_ct" "${sheets}"
if (( run ))
then
$dplx "$file"
rc=$?
if (( rc ))
then
echo "${dplx##*/} failed to print [$file] with error code [$rc]"
if (( rc == script_error )) ## if something worse than just a print file error occurred
then ## quit processing print jobs
break
fi
fi
if (( ! rc ))
then
(( total_pages += page_ct ))
(( total_sheets += sheets ))
fi
ynq "When Printer stops, remove ALL printed pages, then Press y or Enter to continue "
rc=$?
if (( ! rc )) ## User wants to stop printing
then
echo "${script_name} Quitting at user request"
rc=0 ## User quitting is not an error
break
fi
fi
done
if (( total_pages ))
then
echo
echo " *** Printed ${total_pages} pages"
echo
fi
quit