-
Notifications
You must be signed in to change notification settings - Fork 1
/
kmprb_one
399 lines (349 loc) · 12.3 KB
/
kmprb_one
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/bin/bash
#####################################################
## kmprb_one ##
## Usage: kmprb_one [-rt] ##
## Print and delete all one page print jobs ##
## ##
## -rt means reformat and print all one page ##
## text files with dprint before printing all ##
## formatted one page files (PostScript or PDF)##
## ##
## Copyleft (c) 24 Jul 2015 Joseph J. Pollock ##
## JPmicrosystems - josephj at main.nc.us ##
## Last Modifed 03/23/2018 ##
## ##
## 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 ##
#####################################################
#### debug
## fake lp for testing w/o a printer
##source ${HOME}/pgm/bash_functions/lp ## debug - DRYRUN
function quit () {
## clean up loose ends and exit
##
rm -f ${tmpfile}
exit $rc
}
function user_abort () {
## clean up loose ends from user abort
## If we get clobbered, there should be a message
## But it probably shouldn't be a gui message
##
echo
echo " ${script_name} - Aborted by User"
echo
rc=1
quit
}
function lp_queue_id () {
# lp_queue_id - extract print queue number
# from message issued by lp
# returns 0 if successful, 1 if failed
# queue number is returned in lpq_id as a string queue-jobnumber
local usage
usage="Usage is ${FUNCNAME} {lp-print-message}"
##echo "lpq received [$@]" > /dev/stderr
if [[ -z "${4}" ]] # The 4th word returned by lp
then # should be printer-q_id
echo "${usage}" > /dev/stderr # if it's null, then foobar
unset lpq_id # echo a usage message
return 1 # and return an error
fi
##echo "lpq_id [${4}]" > /dev/stderr
lpq_id=${4} # extract q_id
return 0
}
function job_in_print_queue () {
## Return 0 if print queue contains job number $1
## query the print queue using lpstat
## pipe the outout into grep to find just the line containing the job number
## $1. Use command substitution (``) and quoting to get that line into a string
## which is then tested for non-empty (-n). If the print job exists, we'll get
## the line and the string will be non-empty. Otherwise, the string will be empty
## Can deal with multiple print queues if $1 contains Queuename-jobnumber
##echo "[$(lpstat | grep "$1")]" > /dev/stderr
if [ -n "$(lpstat | grep "${1}")" ]
then
return 0
fi
return 1
}
function wait_until_printjob_done () {
## Wait print job to finish
## Uses the job_in_print_queue function (defined above) to see if job number $1
## is still in the print queue. If it is, wait and look again until
## it's not found (presumably, finished spooling to printer)
while job_in_print_queue "${1}"
do
##echo found it > /dev/stderr ## debug
sleep 2 ## adjust for the speed of your printing process
done
##echo it\'s gone > /dev/stderr ## debug
return 0
}
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}"]" ## debug
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 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 ##
##################################################################
script_error=9 ## when non-file related errors happen
debug_out="/dev/stderr" ## debug - so debug output always has somewhere to go
##debug_out="$HOME/temp/debug_output_kmprb_one.txt" ## debug
##exec 5> "$debug_out" ## debug
##BASH_XTRACEFD="5" ## debug Calling from desktop
##source $HOME/bin/bash_trace ## debug - TRACE
dsptime_short=5 ## yad timeout normal messages
dsptime_error=30 ## yad timeout error messages
yopt='--center --on-top --no-markup'
script_name="$(basename $0)" ## path stripped name of this script
mypath
script_path="${my_path}" ## make sure we can find other scripts
yyes="OK"
yno="Cancel"
config="${HOME}/.duplexpr"
trap 'user_abort' 2 ## Call user_abort() if user presses ctrl-c
## If called from kmprb, then DUPLEX_GUI == 1 already
## Otherwise, it was called standalone
if (( ! DUPLEX_GUI ))
then
export DUPLEX_LAST_BATCH=1 ## tell krmpq it's at the end of the line - so no quit option
export DUPLEX_GUI=1 ## Tell called scripts that they're nested in the duplex system
fi
## Access the duplexpr configuration file
if [[ ! -r "${config}" ]]
then
yad ${yopt} --title="${script_name}" --info --button=gtk-ok:0\
--text="Missing config file ${config}" --width=160 --timeout="${dsptime_error}"
rc="${script_error}"
quit
fi
source "${config}"
if (( $? ))
then
yad ${yopt} --info --button=gtk-ok:0\
--text="Bad config file ${config}" --width=160 --timeout="${dsptime_error}"
rc="${script_error}"
quit
fi
reformat=0
if [[ "$1" == "-rt" ]]
then
reformat=1 ## reformat text files with dprint
shift
if [[ ! -r "${HOME}/.enscriptrc" ]]
then
yad ${yopt} --title="${script_name}" --info --button=gtk-ok:0\
--text="Missing enscript configuration file ${HOME}/.enscriptrc
Needed when reformatting text (-rt) is specified" --timeout="${dsptime_error}"
rc="${script_error}"
quit
fi
fi
cd "${pq}"
if (( $? ))
then
yad ${yopt} --title="${script_name}" --info --button=gtk-ok:0\
--text="Can't access Print Queue [$pq]" --width=160 --timeout="${dsptime_error}"
rc="${script_error}"
quit
fi
## Create temp file for mprb -i output
tmpfile=$(/bin/mktemp -q ${tp}/${script_name}.XXXXXX) # create temp file
rc=$?
if (( $rc )) # if create tempfile failed
then
yad ${yopt} --title="${script_name}" --info --button=gtk-ok:0\
--text="Can't create temp file Error [${rc}]
Check permissions for ${tp}
Aborting..." --width=160 --timeout="${dsptime_error}"
rc="${script_error}"
quit
fi
## Use mprb -i to generate a list of all print jobs and the number of pages per job
## Get rid of non-job lines - don't have a [ in them
## Delete everything up to and including the [
## Delete '] -'
## Get rid of everything from ' pages' to the end of the line
## Making sure to avoid matching within the file name
## Now, if the line ends with ' 1', it's a one page job
## Get rid of any non-one page jobs
## Get rid of the ' 1' at the end of the line, so all that's left is the file name of a one page job
rc=0
"${script_path}/mprb" -i * | sed -r \
-e '/.*\[/!d' \
-e 's/.*\[//' \
-e 's/\] -//' \
-e 's/ pages[ ]+[0-9]+ sheets$//' \
-e '/ 1$/!d' \
-e 's/[ ]+1$//' > "${tmpfile}"
## Get the list of one page jobs into an array without allowing bash substitutions
readarray -t files < "${tmpfile}"
## If there aren't any one page jobs, then we're done. Say so if we weren't called from
## another duplex script like kmprb
if (( ! ${#files[*]} ))
then
(( ! DUPLEX_GUI )) && yad ${yopt} --title "${script_name}" --info --button=gtk-ok:0\
--text=" No 1 Page Jobs in Print Queue" --width=211 --timeout="${dsptime_short}"
rc=0
quit
fi
##echo -e "files ${files[@]}" ## debug
##for (( I=0; I<${#files[*]}; I++ )) ## debug
##do ## debug
## echo -e "[${I}] [${files[${I}]}]" ## debug
##done ## debug
jobs="${#files[*]}"
message="$(for file in "${files[@]}"
do
printf "%s\n" "${file}"
done | \
sed -r -e 's/$/ - 1 Pages/g' -e 's/^/\t/')\n\n ${jobs} Pages to print\n
Select ${yyes} or Press Ctrl+Enter to Print ALL one page jobs\n\nSelect ${yno} to Skip Printing\n\n"
echo -e "${message}" | yad ${yopt} --title "Preview Print Queue [$pq]" --tail \
--button=gtk-ok:0 --button=gtk-cancel:1 \
--width=500 --height=500 --text-info
rc=$?
if (( $rc ))
then
rc=0 ## It's OK if the user cancelled the print
quit
fi
if (( reformat ))
then
unset print_files
unset text_files
for file in "${files[@]}"
do
##echo "file [${file}]" ## debug
get_print_file_type "${file}"
rc=$?
if (( $rc ))
then
yad ${yopt} --title "${script_name}" --info --button=gtk-ok:0\
--text="Can't get print file type of [${file}] "
quit
fi
##echo "file [${file}] TYPE [${print_file_type}]" ## debug
case ${print_file_type} in
1 | 3) ## PostScript or PDF is OK as is - add to list
print_files=("${print_files[@]}" "$file")
continue
;;
2) ## Text - convert, print now, but don't add to list
##echo -e "found one page text file [${file}]" ## debug
text_files=("${text_files[@]}" "${file}") ## but save it for deleting with krmpq
##(( ++fake_print_job_number )) ## used for testing with fake lp
"${script_path}/dprint " "${file}" ## debug
rc=$?
if (( $rc ))
then
yad ${yopt} --title "${script_name}" --info --button=gtk-ok:0\
--text="dprint failed to print [${file}] "
quit
fi
;;
*) ## Should never happen
yad ${yopt} --title "${script_name}" --error --button=gtk-ok:0 --width=200 --height=100 --no-markup\
--text "[${file}] can't be processed\n\tAborting [${rc}]"
rc=1
quit
;;
esac
done
files=("${print_files[@]}")
fi
## Print the jobs and save the job number
##count="${#files[@]}" ## debug
##echo "count [${count}]" ## debug
if (( ${#files[@]} ))
then
##echo "non-reformatted files"
##echo "files ${files[@]}" ## debug
##for (( I=0; I<${#files[*]}; I++ )) ## debug
##do ## debug
## echo "[${I}] [${files[${I}]}]" ## debug
##done ## debug
##(( ++fake_print_job_number )) ## used for testing with fake lp
myjob="$(lp "${files[@]}")"
rc=$?
##echo "myjob rc [${myjob}] [${rc}]"
if (( rc )) # if lp failed
then
yad ${yopt} --title "${script_name}" --error --button=gtk-ok:0 --width=200 --height=100 --no-markup\
--text "lp failed to print [${files[@]}\n\tAborting [${rc}]" --timeout="${dsptime_error}"
quit
fi
lp_queue_id ${myjob} ## Get id of last job sent to printer
rc=$?
if (( $rc )) ## Internal Error - Failed to get print queue id
then
yad ${yopt} --title "${script_name}" --error --button=gtk-ok:0 --width=200 --height=100 --no-markup\
--text "Internal Error - Failed to get print queue id of [${myjob}]
Programmer malfunction - please report bug" --timeout="${dsptime_error}"
quit
fi
wait_until_printjob_done ${lpq_id} ## Wait for it to be gone
fi
## Optionally erase the files which were just printed
"${script_path}/krmpq" "${text_files[@]}" "${files[@]}"
rc=$?
quit