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