#!/bin/sh
#
# This script is intended to be used as a CUPS backend, to create
# PDF file on-the-fly. Just create a printer using the device uri
# pdf:/path/to/dir/. When printing to this printer, a PDF file
# will be generated in the directory specified. The file name will
# be either "<jobname>.pdf" or "unknown.pdf", depending wether the
# jobname is empty or not.
#
# Usage:
#  1. Copy this script to your backend directory (/usr/lib/cups/backend)
#     and name it "pdf".
#  2. Create a printer in CUPS and use the URI "pdf:/tmp".
#  3. Optional: Install either mime-construct or mimeit (part of
#     metamail) to have the PDF files emailed instead of dumped to dir.
#
# Copyright (C) Michael Goffioul (goffioul@imec.be) 2001
LOGFILE=/tmp/pdf.log
PDFBIN=`which ps2pdf`
FILENAME= 
# this is borrowed from printpdf script for the filename
PRINTTIME=`date +%b%d-%H%M%S`

echo "Executable: $PDFBIN" > $LOGFILE
echo "Arguments: |$1|$2|$3|$4|$5|$6|" >> $LOGFILE 
echo $# $PRINTTIME >> $LOGFILE

# case of no argument, prints available URIs
if [ $# -eq 0 ]; then
	if [ ! -x "$PDFBIN" ]; then
		exit 0
	fi
	echo "direct pdf \"Unknown\" \"PDF Writing\""
	exit 0
fi 

# case of wrong number of arguments
if [ $# -ne 5 -a $# -ne 6 ]; then
	echo "Usage: pdf job-id user title copies options [file]"
	exit 1
fi 

# get PDF directory from device URI, and check write status
PDFDIR=${DEVICE_URI#pdf:}
if [ ! -d "$PDFDIR" -o ! -w "$PDFDIR" ]; then
	echo "ERROR: directory $PDFDIR not writable"
	exit 1
fi 

echo "PDF directory: $PDFDIR" >> $LOGFILE 

user=$2
# Avoid email to non-user accounts
case "$user" in
    nobody|guest) user=;;
esac

# generate output filename
OUTPUTFILENAME=

if [ "$user" = "" ]; then
	OUTPUTFILENAME="$PDFDIR/unknown-$PRINTTIME.pdf"
else
	# OUTPUTFILENAME="$PDFDIR/${3//[^[:alnum:]]/_}.pdf"
	# I changed this to user name, and the printtime to track down who
	# printed the PDF and when, samba printing just uses nobody

	OUTPUTFILENAME="$PDFDIR/$user-$PRINTTIME.pdf"
	echo "PDF file: $OUTPUTFILENAME placed in: $PDFDIR" >> $LOGFILE
fi 

echo "Output file name: $OUTPUTFILENAME" >> $LOGFILE 

# run ghostscript
if [ $# -eq 6 ]; then
	$PDFBIN $6 $OUTPUTFILENAME 
#>& /dev/null
else
	$PDFBIN - $OUTPUTFILENAME >& /dev/null
fi

# If the printername resolves to a .xml file and XenuxPDF is installed run it
XENUXPDF="/usr/local/XenuxPDF"
CONFIG="$XENUXPDF/$PRINTER.xml"
if [[ -e "$XENUXPDF/xenuxpdf.py" && -e $CONFIG ]]; then
	echo "Calling XenuxPDF" >> $LOGFILE
	PYTHONBIN=`which python`
	$PYTHONBIN "$XENUXPDF/xenuxpdf.py" $CONFIG $OUTPUTFILENAME >> $LOGFILE 2>&1
fi

# modify ownership and permissions on the file
#  - world readable
#  - owns to user specified in argument
chmod a+r $OUTPUTFILENAME

if [ "$user" != "" ]; then
	if [ -x /usr/bin/mime-construct ]; then
		echo "Send as pdf to $user using mime-construct" >> $LOGFILE 
		/usr/bin/mime-construct --to "$user" --subject "PDF print" --type application/pdf --attachment `basename $OUTPUTFILENAME` --file $OUTPUTFILENAME \
			&& rm $OUTPUTFILENAME
	elif [ -x /usr/bin/mimeit ]; then
		echo "Send as pdf to $user using mimeit" >> $LOGFILE 
		cat $OUTPUTFILENAME | /usr/bin/mimeit application/pdf $user "PDF print" \
			&& rm $OUTPUTFILENAME
	else	
		#The following ensures that PDF has filerights so everybody of fsadmin group can delete it.
		chown $user:fsadmin $OUTPUTFILENAME
	fi
fi 

exit 0