1#!/bin/bash
2# samba-print-pdf
3# This is a script which allows you to set up a virtual printer on samba
4# which will take the file (generated by a postscript filter on windows)
5# and turn it into a PDF, informing the user of where it is when it
6# is done
7#
8# (c) Buchan Milne <bgmilne@cae.co.za> 2002 
9# License: GPLv2
10# Changelog
11# v0.0.6 20030428
12#  - Allow options passed as env. variables from print command
13#  - Inline and simplify sed (use tr) clean script
14#  - Ensure file arrives in PREFIX even if TEMP is used without provided name
15#  - Changes from Joshua M. Schmidlkofer <joshua@imr-net.com> 20030425
16#    - Debugging, adjustments, and corrections.
17#    - Stupid sed sanitizing script. [probably horribly inefficient also]. 
18#    - Temp file usage cleanup.
19# v0.0.5 20020723
20#  - Add support for preset settings
21#  - Allow passing of filename provided by client as final filename
22#
23# Arguments:
24# $1 = file (usually passed with %s from samba)
25# $2 = unix prefix to where to place the file (~%u should work)
26# $3 = windows prefix to the same location (//%L/%u should work)
27# $4 = user/computer to send a notification to (%u or %m)
28# $5 = IP address of client (%I)
29# $6 = Name of destination file without extension (%J)
30# $7 = PDF setting (prepress,print,screen etc)
31#
32# If you want to customise any of the following configuration defaults, 
33# you can place them in the file /etc/samba/print-pdf.conf.
34# If you need to modify anything in this script, please provide me with your
35# changes, preferably in such a way that the changes are configurable.
36
37PS2PDF=ps2pdf13 
38OPTIONS="-dAutoFilterColorImages=false -sColorImageFilter=FlateEncode"
39#Values taken from arguments:
40INPUT=$1
41PREFIX="$2"
42WINBASE=$(echo "$3"|sed -e 's,/,\\\\,g')
43#NAME=`echo "$6"|sed -e 's/[&/:{}\\\[<>$#@*^!?=|]/-/g;s/\]/-/g'`
44NAME=`echo "$6"|tr '[:punct:]' '[-*]'`
45
46# Source config file if it exists:
47CONFFILE=/etc/samba/print-pdf.conf
48[ -e $CONFFILE ] && . $CONFFILE
49
50#Values not taken as arguments, could be set via env. vars (?) or config file
51KEEP_PS=${KEEP_PS=0}
52PERMS=${PERMS=640}
53BASEFILE=${BASEFILE=pdf-service}
54TEMP="${TEMP=$2}"
55UMASK=${UMASK=006}
56
57#Make sure that destination directory exists
58mkdir -p "$PREFIX"
59
60INFILE=$(basename $INPUT)
61
62umask $UMASK
63
64[ -n "$NAME" ] && TEMP="$PREFIX"
65
66#make a temp file to use for the output of the PDF
67OUTPUT=`mktemp -q $TEMP/$BASEFILE-XXXXXX`
68if [ $? -ne 0 ]; then
69	echo "$0: Can't create temp file $TEMP/$OUTPUT, exiting..."
70	exit 1
71fi
72if [ -n "$NAME" ]; then
73	FINALOUTPUT="$PREFIX/$NAME"
74else
75	FINALOUTPUT="$OUTPUT"
76fi
77if [ -n "$7" ]; then
78	OPTIONS="$OPTIONS -dPDFSETTINGS=/${7#pdf-}"
79else
80	OPTIONS="$OPTIONS -dPDFSETTINGS=/default"
81fi
82
83WIN_OUTPUT="$WINBASE\\"`basename "$FINALOUTPUT"`
84#mv "$INPUT" "$INPUT.ps";INPUT="$INPUT.ps"
85
86# create the pdf
87$PS2PDF $OPTIONS "$INPUT" "$OUTPUT.pdf" >/dev/null 2>&1
88mv -f "${OUTPUT}.pdf" "${FINALOUTPUT}".pdf
89
90# Generate a message to send to the user, and deal with the original file:
91MESSAGE=$(echo "Your PDF file has been created as $WIN_OUTPUT.pdf\n")
92
93
94# Cleanup
95if [ $KEEP_PS != 0 ];then
96	mv -f $INPUT "${FINALOUTPUT}".ps
97	MESSAGE=$(echo "$MESSAGE and your postscript file as $WIN_OUTPUT.ps")
98	# Fix permissions on the generated files
99	chmod $PERMS "${FINALOUTPUT}".ps "${FINALOUTPUT}".pdf
100else
101	rm -f $INPUT
102	# Fix permissions on the generated files
103	chmod $PERMS "${FINALOUTPUT}".pdf
104fi
105                                            
106#Remove empty file from mktemp:
107rm -f  $OUTPUT
108
109# Send notification to user
110echo -e $MESSAGE|smbclient -M $4 -I $5 -U "PDF Generator" >/dev/null 2>&1
111
112