initial checkin
[grml-etc.git] / usr / lib / cups / backend / pdf
1 #!/bin/sh
2 # originally written by Michael Goffioul; modified & adapted by Kurt 
3 # Pfeifle as part of is "Learning Shell Scripting" efforts. (WARNING:
4 # THIS WAS MY FIRST SHELL SCRIPT!) 
5 #
6 # This script is intended to be used as a CUPS backend, to create
7 # PDFs file on-the-fly. Just create a printer using the device URI
8 # 'pdf:/path/to/dir/. When printing to this printer, a PDF file will
9 # be generated in the directory specified. The file name will either
10 # be "<jobname>-<some-other-info>.pdf" or "unknown-<printtime>.pdf", 
11 # depending wether the jobname is empty or not.
12 #
13 # Attention: CUPS backends run with root privileges -- and so does
14 # ---------- this script! Use at your own risk!
15 #
16 # To use it, simply copy this script to your backend directory, and
17 # create a printer with the correct URI. That's it.
18 #
19 # In case you don't know how to create the PDF creating " printer",
20 # here is a reminder (you need the ps2pdf.ppd' in the CUPS default
21 # 'model' directory "/usr/share/cups/model/", and it needs to be
22 # a generic PostScript-PPD such as the distiller.ppd from Adobe)::
23 #
24 #  "lpdmin -p pdfwriter -v pdf:/home/grml/PDFs -E -m ps2pdf.ppd"
25 #
26 # Copyright (C) Michael Goffioul (goffioul@imec.be) 2001
27 #           (C) Kurt Pfeifle <kpfeifle@danka.de> 2002
28
29
30 # debug off by default; comment in if you desire debugging info....
31 #DEBUG="yes"
32
33 if [ x"${DEBUG}" = x"yes" ]; then
34         set -x
35         LOGFILE="/tmp/pdf.log"
36 else
37         # debug off:
38         LOGFILE="/dev/null"
39 fi
40
41 PDFBIN=`which ps2pdf`
42 FILENAME= 
43 PRINTTIME=`date +%b%d-%H%M%S`
44
45 # first log a few things into the log file....
46 echo " "   >> ${LOGFILE}
47 echo  "####################################" >> ${LOGFILE}
48 echo  "print and log time is ${PRINTTIME}" >> ${LOGFILE}
49 echo  arg0  $0 >> ${LOGFILE}  # printer
50 echo  arg1  $1 >> ${LOGFILE}  # job
51 echo  arg2  $2 >> ${LOGFILE}  # user
52 echo  arg3  $3 >> ${LOGFILE}  # title
53 echo  arg4  $4 >> ${LOGFILE}  # options
54 echo  arg5  $5 >> ${LOGFILE}  # copies
55 echo  arg6  $6 >> ${LOGFILE}  # <filename>; empty if taken from 'stdin'
56 echo  args no. is '$#' = $#             >> ${LOGFILE}
57 echo  PRINTTIME   ${PRINTTIME}          >> ${LOGFILE}
58 echo  PATH   ${PATH}                    >> ${LOGFILE}
59 echo  SOFTWARE   ${SOFTWARE}            >> ${LOGFILE}
60 echo  USER   ${USER}                    >> ${LOGFILE}
61 echo  CHARSET   ${CHARSET}              >> ${LOGFILE}
62 echo  LANG   ${LANG}                    >> ${LOGFILE}
63 echo  TZ   ${TZ}                        >> ${LOGFILE}
64 echo  PPD   ${PPD}                      >> ${LOGFILE}
65 echo  CUPS_SERVERROOT  ${CUPS_SERVERROOT}  >> ${LOGFILE}
66 echo  RIP_MAX_CACHE   ${RIP_MAX_CACHE}  >> ${LOGFILE}
67 echo  TMPDIR   $TMPDIR                  >> ${LOGFILE}
68 echo  CONTENT_TYPE   $CONTENT_TYPE      >> ${LOGFILE}
69 echo  DEVICE_URI   ${DEVICE_URI}        >> ${LOGFILE}
70 echo  PRINTER   ${PRINTER}              >> ${LOGFILE}
71 echo  CUPS_DATADIR   ${CUPS_DATADIR}    >> ${LOGFILE}
72 echo  CUPS_FONTPATH   ${CUPS_FONTPATH}  >> ${LOGFILE}
73 echo  "Executable: ${PDFBIN}"           >> ${LOGFILE}
74 echo  "Arguments: | 1: $1 | 2: $2 | 3: $3 | 4: $4 | 5: $5 | 6: $6 |" >> ${LOGFILE}
75 echo " "                                >> ${LOGFILE}
76 echo "Executable: ${PDFBIN}"            >> ${LOGFILE}
77 echo " "                                >> ${LOGFILE}
78
79 # case of no argument, *MUST* print available URI to satisfy cupsd's
80 # startup query and test for backend availability
81 if [ $# -eq 0 ]; then
82         if [ ! -x "$PDFBIN" ]; then
83                 exit 0
84         fi
85         echo "direct pdf \"Unknown\" \"PDF Writing\""
86         exit 0
87 fi 
88
89 # case of wrong number of arguments
90 if [ $# -ne 5 -a $# -ne 6 ]; then
91         echo "Usage: pdf job-id user title copies options [file]"
92         exit 1
93 fi 
94
95 # get PDF directory from device URI, and check write status
96 PDFDIR=${DEVICE_URI#pdf:}
97 if [ ! -d "${PDFDIR}" -o ! -w "${PDFDIR}" ]; then
98         echo "ERROR: directory ${PDFDIR} not writable"
99         exit 1
100 fi 
101
102 echo "PDF directory: ${PDFDIR}" >> ${LOGFILE} 
103
104
105 # sanitize the job-title (may contain weird characters if printed fromm
106 # browser...)
107 TITLE=${3}
108
109
110 # first the spaces.....
111 TITLE=`echo ${TITLE} | tr [:blank:] _`;
112
113 [ x"${DEBUG}" = x"yes" ] && echo "removed all spaces from original name of jobfile. Name is now ${TITLE}"           >> ${LOGFILE};
114
115 # next the colons....
116 TITLE=`echo ${TITLE} | tr : _`;
117 [ x"${DEBUG}" = x"yes" ] && echo "removed all colons from original name of jobfile. Name is now ${TITLE}"           >> ${LOGFILE};
118
119 # then the slashes....
120 TITLE=`echo ${TITLE} | tr / _`;
121 [ x"${DEBUG}" = x"yes" ] && echo "removed all slashes from original name of jobfile. Name is now ${TITLE}"          >> ${LOGFILE};
122
123 # then the question-marks....
124 TITLE=`echo ${TITLE} | tr ? _`;
125 [ x"${DEBUG}" = x"yes" ] && echo "removed all question marks from original name of jobfile. Name is now ${TITLE}"   >> ${LOGFILE};
126
127 # last the backslashes....
128 TITLE=`echo ${TITLE} | tr "\134" _`;
129 [ x"${DEBUG}" = x"yes" ] && echo "removed all backslashes from original name of jobfile. Name is now ${TITLE}"      >> ${LOGFILE};
130
131 # we should now have a sanitized ${TITLE} to use with less danger of screwing things up....
132
133 SANITIZED_TITLE=${TITLE}
134
135 # generate output filename
136 OUTPUTFILENAME=
137 if [ "$3" = "" ]; then
138         OUTPUTFILENAME="${PDFDIR}/unknown-${PRINTTIME}.pdf"
139 else
140         # OUTPUTFILENAME="${PDFDIR}/${3//[^[:alnum:]]/_}.pdf"
141         # I changed this to user name, and the printtime to track down who
142         # printed the PDF and when; Samba printing possibly uses 'nobody'...
143
144         OUTPUTFILENAME="${PDFDIR}/${SANITIZED_TITLE}-${PRINTTIME}-${USER}-${2}.pdf"
145         echo "PDF file: $OUTPUTFILENAME placed in: ${PDFDIR}" >> ${LOGFILE}
146 fi 
147
148 echo "Output file name: $OUTPUTFILENAME" >> ${LOGFILE} 
149
150 # run ghostscript
151 if [ $# -eq 6 ]; then
152         $PDFBIN $6 "$OUTPUTFILENAME"
153 #>& /dev/null
154 else
155         $PDFBIN - "$OUTPUTFILENAME"  
156 #  >& /dev/null
157 fi
158
159 sleep 10
160
161 # modify ownership and permissions on the file
162 #  - world readable
163 #  - owns to user specified in argument
164 chmod a+r "$OUTPUTFILENAME"
165 if [ "$2" != "" ]; then
166         chown $2 "$OUTPUTFILENAME"
167 fi 
168
169 exit 0