]> arthur.barton.de Git - netatalk.git/blob - contrib/printing/add_netatalk_printer
case-conversion slightly faster
[netatalk.git] / contrib / printing / add_netatalk_printer
1 #! /bin/sh
2 #
3 # $Id: add_netatalk_printer,v 1.7 2003-02-23 03:16:34 srittau Exp $
4 #
5 # A lovely script to add netatalk printers 
6 #
7 #ident  "@(#)netatalk   0.5     99/06/22 job@uchicago.edu"  /* Netatalk 1.4*/
8 #
9 # This File is released under the Perl Artistic Licence.
10 # See http://www.perl.org for details
11 #
12 # Or you can use it under the licence that accompanies Netatalk 1.3 =)
13 #
14 # This file is maintained by Job Bogan <job@uchicago.edu>
15 # Please contact me at this address for bug reports, problems, etc.
16 # Please do not bother the netatalk  maintainers with problems from
17 # this program.
18 #
19 #
20 #
21 # Exit Codes ...  1 for missed path or user input
22 # 2 for an aborted run (by not saying yes to a 'are you sure?'
23 # 3 for failed lpadmin commands
24 # 4 for possible security problem
25
26 # make base name for temp files.
27
28 TMP_STUFF="/tmp/`basename $0`.$$"
29 export TMP_STUFF
30 rm -rf ${TMP_STUFF}
31 mkdir -m 700 ${TMP_STUFF} || exit 4
32 trap 'rm -f $TMP_STUFF > /dev/null 2>&1' 0
33 trap 'exit' 1 2 3
34
35 # grab the pathname the script was called w/
36 # if it was run by ./add...  then make the path == `pwd`
37
38 RUNHOME=`dirname $0`
39
40 if [ "$RUNHOME" = "." ]; then
41         RUNHOME=`pwd`
42 fi
43
44 # allow for the env NETATALKHOME to override the guessed one from above
45
46 NETATALKHOME=${NETATALKHOME:-$RUNHOME}
47 export NETATALKHOME
48
49 PATH=/bin:${PATH}:${NETATALKHOME}/bin:${NETATALKHOME}/etc:${NETATALKHOME}/libexec:/usr/lib:/usr/sbin
50
51 if [ ! -x ${NETATALKHOME}/bin/pap ]; then
52         echo "OOPS:     I don't see ${NETATALKHOME}/bin/pap ,"
53         echo '  Check that it is executable or set ${NETATALKHOME}'
54         echo '  so i can find pap at ${NETATALKHOME}/bin/pap'
55         echo '[We are guessing the location of the binary from the'
56         echo 'path that was used to run this script or $NETATALKHOME]'
57         exit 1
58 fi
59
60 # get the user to tell us where to look for things, and list things there.
61
62 echo ''
63 echo "Enter Appletalk zone [default: local]: \c"
64
65 read ZONE
66
67 echo ""
68 echo "Looking for LaserWriters in Zone ${ZONE} ..."
69 ${NETATALKHOME}/bin/nbplkup ":LaserWriter@${ZONE}"
70
71 echo ""
72 echo "Enter AppleTalk printer name: \c"
73
74 read DEST
75
76 if [ "$DEST" = "" ]; then
77         echo "OOPS: you need to tell me a printer name"
78         exit 1
79 fi
80
81 echo "checking nbplkup ${DEST}:LaserWriter@${ZONE}"
82 echo ""
83 TestDEST=`${NETATALKHOME}/bin/nbplkup "${DEST}:LaserWriter@${ZONE}"`
84 echo "${TestDEST}"
85 echo ""
86
87 if [ "${TestDEST}" = "" ]; then
88     echo "I don't see that printer on the network.  You may have entered an incorrect"
89     echo "printer name - if so, exit and restart."
90     echo "[You should only enter the printer name, not the :LaserWriter portion]"
91     sleep 3
92 fi
93
94 unset TestDEST
95
96 NBPNAME="${DEST}@${ZONE}"
97
98 echo ""
99 echo ""
100
101 # scrunch all of the whitespace and / out of the appletalk name and suggest
102 # that as the unix name
103
104 SUGGEST=`echo "$DEST" | cut -d: -f1 | sed 's/ //g' | sed 's#/##g` ; export SUGGEST
105
106 # truncate the suggested name to 14 chars to conform to lp specs.
107
108 SUGGEST1=`expr ${SUGGEST} : '\(..............\)'`
109 SUGGEST=${SUGGEST1:-$SUGGEST}
110
111 echo "Enter Unix printer name - [default: ${SUGGEST}] : \c"
112 read UNIXPRINT
113 echo ''
114 if [ "${UNIXPRINT}" = "" ]; then
115         UNIXPRINT="${SUGGEST}"
116 fi
117 export UNIXPRINT
118
119 #####
120 # Here we check for legal names again. >14 chars
121 #####
122
123 UNIXPRINT1=`expr "${UNIXPRINT}" : '\(..............\)'`
124
125 # if UNIXPRINT is shorter than 14chars, UNIXPRINT1 ends up null ("")
126 # if UNIXPRINT was longer, then we get a 14char version of it.  bleah.
127
128 if [ "${UNIXPRINT1}" = "" ]; then
129         UNIXPRINT1="${UNIXPRINT}"
130 else
131 #if [ "${UNIXPRINT1}" != "${UNIXPRINT}" ]; then
132     echo "Oops, that name was too long...  Truncating to 14 chars."
133     echo "setting printer name to '${UNIXPRINT}'"
134     UNIXPRINT="${UNIXPRINT1}"
135 fi
136
137 echo "Enter a description for this printer.  [The Appletalk name will"
138 echo "be included by default, and must be included for the printing"
139 echo "filters to work.] : \c"
140 read DESC
141
142 DESC="${DESC} [${NBPNAME}]"
143
144 echo ""
145 echo "Do you want all print jobs to print out in reversed page order?"
146 echo "This is usually only desired if this printer stacks jobs"
147 echo "face up in the output bin. [N/y] \c"
148 read REV
149 case ${REV} in          [Yy]*)  REV="Netatalk-R" ;;
150                         *)      REV="Netatalk"   ;;
151 esac
152 export REV
153
154 if lpstat -p ${UNIXPRINT} >/dev/null 2>&1 ; then
155         echo '\a'
156         echo "OOPS:     There already exists a printer named '${UNIXPRINT}'"
157         echo "          here are the comments from /etc/lp/printers/${UNIXPRINT}/comment"
158         cat /etc/lp/printers/${UNIXPRINT}/comment
159         echo ""
160         echo "Do you still want to do this? This will flush "
161         echo "all pending jobs!  [Y/n]? \c"
162         read DOTHIS
163         case ${DOTHIS} in       [Yy]*)         ;;
164                                 *)      exit 2 ;;
165         esac
166
167         echo "Rejecting all new jobs for ${UNIXPRINT}"
168         reject -r "Making ${UNIXPRINT} a netatalk printer" ${UNIXPRINT}
169         echo "Disabling and Flushing the ${UNIXPRINT} queue"
170         disable -c -r "Making ${UNIXPRINT} a netatalk printer" ${UNIXPRINT}
171 fi
172
173 #####
174 # Check if we need to do silly tricks to share the printer... only on 5.5.1 or older
175 #####
176
177 if [ "`uname -r`" -lt "5.6" ]; then
178     echo ""
179     echo "Do you want to share this printer with other machines [Y/n]?\c"
180     read SHARE
181 else
182     echo "You're running SunOS 5.6 or higher... skipping 'sacadm' and 'pmadm'"
183 fi
184
185 if nistest printers.org_dir; then
186     echo "Do you want to add this printer to your NIS printer map? [y/N]"
187     echo "(You will need to share this printer with other machines)"
188     read NIS
189     case ${NIS} in      [Yy]*)    ADD_NIS=1 ;;
190                             *)              ;;
191     esac
192 fi
193
194 echo "Do you want to save the error messages from pap in"
195 echo "'/var/spool/lp/tmp/Netatalk/${UNIXPRINT}' [y/N]"
196 echo "(If you answer N, error messages will go to /dev/null.)"
197 read SAVEerr
198 case ${SAVEerr} in      [Yy]*)     SAVEerr=1 ;;
199                             *)     unset SAVEerr ;;
200 esac
201
202 echo "Making ${UNIXPRINT} print to ${DEST} via netatalk"
203
204 if [ "${SAVEerr}" = "" ]; then
205     LOCKDEV=/dev/null
206 else
207     LOCKS=/var/spool/lp/tmp/Netatalk
208     if [ ! -d ${LOCKS} ]; then
209             mkdir -m 0771       ${LOCKS}
210             chown lp    ${LOCKS}
211             chgrp lp    ${LOCKS}
212     fi
213     LOCKDEV=${LOCKS}/${UNIXPRINT}
214     touch               ${LOCKDEV}
215     chown lp:lp ${LOCKDEV}
216     chmod 600   ${LOCKDEV}
217 fi
218
219 echo ""
220
221 if [ ! -r /etc/lp/filter.table ]; then
222         echo "Setting up the existing print filters..."
223         for i in `ls /etc/lp/fd | sed 's/.fd$//'`; do
224                 lpfilter -f ${i} -F/etc/lp/fd/${i}.fd
225         done
226 fi
227 if lpfilter -f netatalk -l > /dev/null 2>&1 ; then
228         echo 'Looks like you have a Netatalk printer filter defined. Good!'
229 else
230         echo "Humm... You need a Netatalk printer filter..."
231         echo "... making you one ..."
232         cat > /etc/lp/fd/netatalk.fd << EOF
233 #ident  "@(#)netatalk.fd 0.2 97/09/04 job@uchicago.edu"      /* Netatalk 1.4 */
234
235 Input types: postscript
236 Output types: PS
237 Printer types: Netatalk
238 Printers: any
239 Filter type: fast
240 Command: ${NETATALKHOME}/libexec/ifpap 2>&1 > /dev/null
241 EOF
242         chown lp:lp /etc/lp/fd/netatalk.fd
243         chmod 664   /etc/lp/fd/netatalk.fd
244         lpfilter -f netatalk -F /etc/lp/fd/netatalk.fd
245 fi
246
247 if lpfilter -f netatalk-r -l 2>&1 > /dev/null ; then
248         echo 'Looks like you have a Reverse Netatalk printer filter defined. Good!'
249 else
250         echo "Humm... You need a Reverse Netatalk printer filter..."
251         echo "... making you one ..."
252         cat > /etc/lp/fd/netatalk-r.fd << EOF
253 #ident  "@(#)netatalk-r.fd 0.2 97/09/04 job@uchicago.edu"      /* Netatalk 1.4 */
254
255 Input types: postscript
256 Output types: PS
257 Printer types: Netatalk-R
258 Printers: any
259 Filter type: fast
260 Command: "/usr/lib/lp/postscript/postreverse | ${NETATALKHOME}/libexec/ifpap 2>&1 >/dev/null"
261 EOF
262         chown lp:lp /etc/lp/fd/netatalk-r.fd
263         chmod 664   /etc/lp/fd/netatalk-r.fd
264         lpfilter -f netatalk-r -F /etc/lp/fd/netatalk-r.fd
265 fi
266
267 if [ ! -r /usr/share/lib/terminfo/N/Netatalk ]; then
268         echo "Making a Terminfo entry for Netatalk printers"
269         cat > ${TMP_STUFF}/terminfo << EOF
270 Netatalk,
271         cols#80, lines#66,
272         cpi=null, csnm=^D, lpi=null, scs=^D, slines=^D, u9=^D,
273 EOF
274         tic ${TMP_STUFF}/terminfo
275         chown bin:bin /usr/share/lib/terminfo/N/Netatalk
276         chmod 644     /usr/share/lib/terminfo/N/Netatalk
277 fi
278
279 if [ ! -r /usr/share/lib/terminfo/N/Netatalk-R ]; then
280         echo "Making a Terminfo entry for Reversed Netatalk printers"
281         cat > ${TMP_STUFF}/terminfoR << EOF
282 Netatalk-R,
283         cols#80, lines#66,
284         cpi=null, csnm=^D, lpi=null, scs=^D, slines=^D, u9=^D,
285 EOF
286         tic "${TMP_STUFF}/terminfoR"
287         chown bin:bin /usr/share/lib/terminfo/N/Netatalk-R
288         chmod 644     /usr/share/lib/terminfo/N/Netatalk-R
289 fi
290
291 ###
292 # this is old cruft...  we should not have done this in the 1st place.
293 # we need to edit the template interface file to point to the local netatalk bins
294 #cat ${RUNHOME}/etc/netatalk.template | sed "s#DEFAULT_NETATALK_HOME#${RUNHOME}#g" \
295 #       > "/etc/lp/interfaces/${UNIXPRINT}"
296 #chown lp:lp    /etc/lp/interfaces/${UNIXPRINT}
297 #chmod 0775     /etc/lp/interfaces/${UNIXPRINT}
298 # below is the correct way.
299 ###
300
301 echo "Setting up ${UNIXPRINT} ...  Edit options later as you see fit."
302 lpadmin -p ${UNIXPRINT} -D "${DESC}" -T ${REV} \
303     -i ${RUNHOME}/netatalk.template -I PS -v ${LOCKDEV} -A none || exit 3
304 accept ${UNIXPRINT} || exit 3
305 enable ${UNIXPRINT} || exit 3
306
307 case ${SHARE} in        [Nn]*)  exit; ;;
308                         *)            ;;
309 esac
310
311 echo "Setting up network sharing for ${UNIXPRINT}"
312
313 # from p925 in the Solaris Administration Guide, Vol. II (2.5)
314
315 echo ""
316 if sacadm -l -p tcp > /dev/null; then
317    echo "already running a tcp listener"
318 else
319    echo "Defining tcp listener ..."
320    sacadm -a -p tcp -t listen -c "/usr/lib/saf/listen tcp" \
321           -v `nlsadmin -V` -n 999
322 fi
323
324 # this is extra, but a good idea...
325
326 LPD_ADDR="\\x`lpsystem -A`" ; export LPD_ADDR
327 ADDR_0=`echo $LPD_ADDR | sed -e 's/\\x00020203/\\x00020ACE/g'`; export ADDR_0
328
329 if pmadm -l -p tcp -s 0 > /dev/null; then
330    echo "<0> service is already defined."
331 else
332    echo "Defining <0>/tcp service ..."
333    pmadm -a -p tcp -s 0 -i root \
334          -m `nlsadmin -c /usr/lib/saf/nlps_server -A ${ADDR_0}` \
335          -v `nlsadmin -V`
336 fi
337
338 # again from the solaris book noted above.
339
340 if pmadm -l -p tcp -s lp > /dev/null; then
341    echo "<lp> service is already defined."
342 else
343    echo "Defining <lp>/tcp service ..."
344    pmadm -a -p tcp -s lp -i root \
345          -m `nlsadmin -o /var/spool/lp/fifos/listenS5` \
346          -v `nlsadmin -V`
347 fi
348
349 if pmadm -l -p tcp -s lpd > /dev/null; then
350    echo "<lpd> service is already defined."
351 else
352    echo "Defining <lpd>/tcp service ..."
353    pmadm -a -p tcp -s lpd -i root \
354          -m `nlsadmin -o /var/spool/lp/fifos/listenBSD -A ${LPD_ADDR}` \
355          -v `nlsadmin -V`
356 fi
357
358 if [ "${ADD_NIS}" = 1 ] ; then
359     if nistest "[printer_name=${UNIXPRINT}]printers.org_dir"; then
360         nistbladm -m    printer_host=`uname -n` \
361         "[printer_name=${UNIXPRINT}]printers.org_dir"  description="$DESC"
362     else
363         nistbladm -a    printer_name=${UNIXPRINT} \
364         description="$DESC" printer_host=`uname -n` printers.org_dir
365     fi
366 fi