Misc
[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 for arg in $*; do
34
35     if [ "${arg}" == "-h" ] ||  [ "${arg}" == "--help" ]; then
36
37         # The help option
38
39         echo $(basename $0)" [<opt> ... <opt>] <file1> [[<opt> ... <opt>] <file2> ... ]"
40         echo
41         echo "A dumb printing utility. Options:"
42         echo
43         echo "-2pp          two pages per page"
44         echo "-4pp          four pages per page"
45         echo "-8pp          eight pages per page"
46         echo "-gv           displays the result with gv instead of printing"
47         echo "-stdout       send the result to the standard output"
48         echo "-P<printer>   uses <printer> instead of $PRINTER"
49         echo
50         echo "Contact <francois.fleuret@epfl.ch>"
51         echo "Version \$Id: print.sh,v 1.19 2006-11-10 15:53:23 fleuret Exp $"
52
53         \rm ${tmp}
54         exit 0
55
56     elif [[ ${arg} == "-"* ]]; then
57
58         # else, if it starts with '-' it is an option
59
60         opts="${opts} ${arg}"
61
62     else
63
64         # else, it is a filename to print
65
66         if [ -e ${arg} ]; then
67
68             TYPE=$(file -L ${arg})
69
70             noprint=""
71
72             case $TYPE in
73
74                 *"image data"*)
75                     convert ${arg} ps:${tmp}
76                     ;;
77
78                 *"TeX DVI"*)
79                     dvips -o ${tmp} ${arg}
80                     ;;
81
82                 *"FIG image"*)
83                     fig2dev -L ps ${arg} > ${tmp}
84                     ;;
85
86                 *"PostScript"*)
87                     cp ${arg} ${tmp}
88                     ;;
89
90                 *"PDF"*)
91                     pdf2ps ${arg} ${tmp}
92                     ;;
93
94                 *" text"*)
95                     # a2ps -B -R --columns=1 -f 8 -o ${tmp} ${arg}
96                     a2ps --media=A4 --user-option=lp -o ${tmp} ${arg}
97                     ;;
98
99                 *"gzip compressed"*)
100                     echo "Unzipping  "
101                     zcat ${arg} > ${tmp}
102                     noprint=1
103                     $0 ${opts} ${tmp}
104                     ;;
105
106                 *)
107                     echo "Unable to handle type \"$TYPE\""; noprint=1
108                     ;;
109
110
111             esac
112
113             if [ ! "$noprint" ]; then
114
115                 # We apply all the filters seen up to now
116
117                 for f in ${opts}; do
118                     case $f in
119                         -2pp)
120                             tmp2=$(mktemp /tmp/filter.ps.XXXXXX)
121                             psnup -d0 -m5 -2 < ${tmp} > ${tmp2}
122                             mv ${tmp2} ${tmp}
123                             ;;
124                         -4pp)
125                             tmp2=$(mktemp /tmp/filter.ps.XXXXXX)
126                             psnup -d0 -m5 -4 < ${tmp} > ${tmp2}
127                             mv ${tmp2} ${tmp}
128                             ;;
129                         -8pp)
130                             tmp2=$(mktemp /tmp/filter.ps.XXXXXX)
131                             psnup -d0 -m5 -8 < ${tmp} > ${tmp2}
132                             mv ${tmp2} ${tmp}
133                             ;;
134                         -gv)
135                             printing_cmd="gv"
136                             ;;
137
138                         -stdout)
139                             printing_cmd="cat"
140                             ;;
141
142                         -P*)
143                             printing_cmd="lpr"
144                             PRINTER=$(echo $f | sed -e s/^-P//)
145                             if [[ -z "$PRINTER" ]]; then
146                                 echo "Error in the printer name. Use -P<printer> with no space."
147                                 lpstat -a
148                                 exit 1
149                             else
150                                 echo "Using printer \"$PRINTER\""
151                             fi
152                             ;;
153                         *)
154                             echo "Unknown option $f"
155                             \rm ${tmp}
156                             exit 1;;
157                     esac
158                 done
159
160                 if [[ "$printing_cmd" == "lpr" ]]; then
161                     echo "Printing on printer '$PRINTER'"
162                     show_queue=1
163                 fi
164
165                 ${printing_cmd} ${tmp}
166
167             fi
168
169         else
170
171             echo "Can not find file ${arg}"
172
173         fi
174
175     fi
176
177 done
178
179 \rm ${tmp}
180
181 if [[ ${show_queue} ]]; then
182     lpq
183 fi
184
185 exit 0