#!/bin/sh
########################################################################################################################
#	
#	Name:		pdf-writer
#
#	Summary:	A simple PDF Writer for CUPS 
#
#	Copyright: 	(c) 2006  Axel Schmidt,  SUSE LINUX GmbH, Nuernberg, Germany
#			(c) 2001  Michael Goffioul (kdeprint <at> swing <dot> be)
#
#			Thanks to Michael Goffioul. pdf-writer was developed based
#			on his "pdfdistiller" from 2001 to provide a seamless integration
#			into a Linux Desktop Enviroment using CUPS.
#
#			This program is free software; you can redistribute it and/or
#			modify it under the terms of the GNU General Public License
#			as published by the Free Software Foundation; either version 2
#			of the License, or (at your option) any later version.
#
#			This program is distributed in the hope that it will be useful,
#			but WITHOUT ANY WARRANTY; without even the implied warranty of
#			MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#			GNU General Public License for more details.
#
#			You should have received a copy of the GNU General Public License
#			along with this program; if not, write to the Free Software
#			Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
########################################################################################################################
#
#	Description:	CUPS BACKEND to generate PDF files from PS.
#	
#			Backends are a special type of filter which is used to send print data to and discover 
#			different devices on the system.	
#	
#       		Backends must support DEVICE DISCOVERY 
#       		- When run with no arguments, the backend should list the devices and schemes it supports or 
#       		- is advertising to stdout. The output consists of zero or more lines consisting of any of the
#			- following forms:
#	
#    			device-class scheme "Unknown" "device-info"
#    			device-class device-uri "device-make-and-model" "device-info"
#    			device-class device-uri "device-make-and-model" "device-info" "device-id"
#
#	Input:		Every backend should accept 5 or 6 command-line arguments:
#
#			job user title copies options [filename]
#
#    			job - The numeric job ID for the job being printed
#    			user - The string from the originating-user-name attribute
#    			title - The string from the job-name attribute
#    			copies - The numeric value from the number-copies attribute
#    			options - String representations of the job template attributes, separated by spaces.
#    			filename - The request file
#
#	Output:		Created PDF files will be located at DEVICE_URI for USERMODE="off" or if USERMODE="on"
#			PDF will be written to "/home/<user name>/PDF".
#			Printer test pages are always written to DEVICE_URI. 
#
#	Prerequisite:	USERMODE="on" needs "AuthType Basic" and "RunAsUser No" configured in /etc/cups/cups.conf
#			ps2pdf from ghostscript-library rpm must be installed.
#
#	Exit Codes:	0 (CUPS_BACKEND_OK)
#    				The print file was successfully transmitted to the device or remote server. 		
#			1 (CUPS_BACKEND_FAILED)
#    				The print file was not successfully transmitted to the device or remote server.
#
########################################################################################################################

PS2PDF=`which ps2pdf`
DATE=`date +%Y-%m-%d-%H:%M.%S`

# USERMODE="on" changes PDFPATH to "$HOME/<user-name>/PDF" 
#
USERMODE="off"
DEFGROUP="users"
HOME="/home"
JOB=$1
PRTUSER=$2

# Create user based file name
#
create_user_fname()
{
	if [ "$PRTUSER" != "" ]; then
		FILENAME="$PDFPATH/$PRTUSER-$DATE.pdf"
		logger "pdf-writer: \"$PRTUSER-$DATE.pdf\" was placed in: $PDFPATH"  
	else
		FILENAME="$PDFPATH/$DATE.pdf"
		logger "pdf-writer: \"$DATE.pdf\" was placed in: $PDFPATH"  
	fi
}


# Check DEVICE DISCOVERY + test PS2PDF 
#
if [ "$JOB" = "" ]; then
	if test -f "$PS2PDF" ; then
		echo "network pdf-writer:/export/share/pdf \"unkown\" \"pdf writer\" " 
		logger "pdf-writer: INFO: USERMODE=\"$USERMODE\""
		if [ "$USERMODE" = "on" ]; then
			logger "pdf-writer: CUPS user must be set to root with USERMODE=\"on\"!"
		fi
		exit 0	
        else
  		echo "Error: $0 - ps2pdf is not available!"      
  		logger "pdf-writer: ERROR: ps2pdf is not available!"      
        	exit 1 
	fi 
fi

logger "pdf-writer started: $1 $2 $3 $4 $5 $6"

# Check number of command line arguments
#
if [ $# -ne 5 -a $# -ne 6 ]; then
	echo "Usage: $0 job-id user title copies options [file]"
	logger "pdf-printer: CRITICAL: Printer stopped!"
	exit 1
fi


# get PDF-WRITER directory from device URI
#
PDFPATH=${DEVICE_URI#pdf-writer:}


# Create output filename based on user name and user mode
#
if [ "$USERMODE" = "on" -a "$PRTUSER" != "" ]; then
   	mkdir -p $HOME/$PRTUSER/PDF
	chown $PRTUSER $HOME/$PRTUSER/PDF

# check if cups run as lp or root
# user lp may not run chown 	
#
	if [ $? -eq 1 ]; then
	  	create_user_fname
	else
		chgrp $DEFGROUP /home/$PRTUSER/PDF
   		PDFPATH=$HOME/$PRTUSER/PDF
   		FILENAME="$PDFPATH/$DATE.pdf"
		logger "pdf-writer: \"$DATE.pdf\" was placed in: $PDFPATH"  
	fi
else
	create_user_fname
fi

# Check write status 
#
if [ ! -d "$PDFPATH" -o ! -w "$PDFPATH" ]; then
	logger "pdf-writer: ERROR: directory $PDFPATH not writeable"
	exit 1
fi

# Run ps2pdf (ghostscript)
if [ $# -eq 6 ]; then
	$PS2PDF $6 $FILENAME >& /dev/null
else
	$PS2PDF - $FILENAME >& /dev/null
fi

# Chown + set permissions for the user 
# Note: this will no work if CUPS runs as lp
if [ "$PRTUSER" != "" ]; then
	chmod 640 $FILENAME
	chown $PRTUSER:$DEFGROUP $FILENAME
fi
exit 0
