Added a printing of what is done.
[scripts.git] / print.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 set -e
21
22 # $Id: print.sh,v 1.19 2006-11-10 15:53:23 fleuret Exp $
23
24 if [ ! "$*" ]; then
25     echo "Need filenames to print!"
26     exit 1
27 fi
28
29 tmp=$(mktemp /tmp/print.ps.XXXXXX)
30 opts=""
31 printing_cmd="lpr"
32
33 function cleanup-before-exit () {
34     rm ${tmp}
35 }
36
37 trap cleanup-before-exit EXIT
38
39 for arg in "$@"; do
40
41     if [ "${arg}" == "-h" ] ||  [ "${arg}" == "--help" ]; then
42
43         # The help option
44
45         echo $(basename $0)" [<opt> ... <opt>] <file1> [[<opt> ... <opt>] <file2> ... ]"
46         echo
47         echo "A dumb printing utility. Options:"
48         echo
49         echo "-2pp          two pages per page"
50         echo "-4pp          four pages per page"
51         echo "-8pp          eight pages per page"
52         echo "-gv           displays the result with gv instead of printing"
53         echo "-stdout       send the result to the standard output"
54         echo "-P<printer>   uses <printer> instead of $PRINTER"
55         echo
56         echo "Contact <francois.fleuret@epfl.ch>"
57         echo "Version \$Id: print.sh,v 1.19 2006-11-10 15:53:23 fleuret Exp $"
58
59         exit 0
60
61     elif [[ ${arg} == "-"* ]]; then
62
63         # else, if it starts with '-' it is an option
64
65         opts="${opts} ${arg}"
66
67     else
68
69         # else, it is a filename to print
70
71         if [ -e "${arg}" ]; then
72
73             TYPE=$(file -L "${arg}")
74
75             noprint=""
76
77             case $TYPE in
78
79                 *"image data"*)
80                     convert "${arg}" ps:${tmp}
81                     ;;
82
83                 *"TeX DVI"*)
84                     dvips -o ${tmp} "${arg}"
85                     ;;
86
87                 *"FIG image"*)
88                     fig2dev -L ps "${arg}" > ${tmp}
89                     ;;
90
91                 *"PostScript"*)
92                     cp "${arg}" ${tmp}
93                     ;;
94
95                 *"PDF"*)
96                     pdf2ps "${arg}" ${tmp}
97                     ;;
98
99                 *" text"*)
100                     # a2ps -B -R --columns=1 -f 8 -o ${tmp} "${arg}"
101                     # if isutf8 -q ${tmp}; then
102                         # echo "Can not print uf8 text."
103                         # exit 1
104                     # else
105                     a2ps --media=A4 --user-option=lp -o ${tmp} "${arg}"
106                     # fi
107                     ;;
108
109                 *"gzip compressed"*)
110                     echo "Unzipping  "
111                     zcat "${arg}" > ${tmp}
112                     noprint=1
113                     $0 ${opts} ${tmp}
114                     ;;
115
116                 *)
117                     echo "Unable to handle type \"$TYPE\""; noprint=1
118                     ;;
119
120
121             esac
122
123             if [ ! "$noprint" ]; then
124
125                 # We apply all the filters seen up to now
126
127                 for f in ${opts}; do
128                     case $f in
129                         -2pp)
130                             tmp2=$(mktemp /tmp/filter.ps.XXXXXX)
131                             psnup -d0 -m5 -2 < ${tmp} > ${tmp2}
132                             mv ${tmp2} ${tmp}
133                             ;;
134                         -4pp)
135                             tmp2=$(mktemp /tmp/filter.ps.XXXXXX)
136                             psnup -d0 -m5 -4 < ${tmp} > ${tmp2}
137                             mv ${tmp2} ${tmp}
138                             ;;
139                         -8pp)
140                             tmp2=$(mktemp /tmp/filter.ps.XXXXXX)
141                             psnup -d0 -m5 -8 < ${tmp} > ${tmp2}
142                             mv ${tmp2} ${tmp}
143                             ;;
144                         -gv)
145                             printing_cmd="gv"
146                             ;;
147
148                         -stdout)
149                             printing_cmd="cat"
150                             ;;
151
152                         -P*)
153                             printing_cmd="lpr"
154                             PRINTER=$(echo $f | sed -e s/^-P//)
155                             if [[ -z "$PRINTER" ]]; then
156                                 echo "Error in the printer name. Use -P<printer> with no space."
157                                 lpstat -a
158                                 exit 1
159                             else
160                                 echo "Using printer \"$PRINTER\""
161                             fi
162                             ;;
163                         *)
164                             echo "Unknown option $f"
165                             exit 1;;
166                     esac
167                 done
168
169                 if [[ "$printing_cmd" == "lpr" ]]; then
170                     echo "Printing on printer '$PRINTER'"
171                     show_queue=1
172                 fi
173
174                 ${printing_cmd} ${tmp}
175
176             fi
177
178         else
179
180             echo "Can not find file ${arg}"
181
182         fi
183
184     fi
185
186 done
187
188 if [[ ${show_queue} ]]; then
189     lpq
190 fi
191
192 exit 0