#!/bin/sh
#
# (C) 2009 Michiel Broek for Slackware.
# (C) 2011 Michiel Broek for ArchLinux.
#
# Based on the SUSE script by Axel Schmidt and Michael Goffioul (kdeprint).
#
# License: GPL
#
# This is the CUPS PDF writer backend script.
# Download the free distiller.ppd from Adobe and install ADIST5.PPD as
# /usr/share/cups/model/distiller.ppd. 
#
# If this script is mode 0700, then it runs as root and files go into the
# users home directories if they exist.
# If this script is mode 0755, then it runs as lp and files go into the
# path defined by the URL. That path should exist and should be owned by lp.
#
# To add this printer:
# lpadmin -p PDF_2_File -v pdf2file:/mnt/users/printfiles/ -E \
#    -P /usr/share/cups/model/distiller.ppd -D "PDF to files" -L "$(hostname -f)"
# mkdir -p /mnt/users/printfiles
# chown lp:lp /mnt/users/printfiles
# chmod 755 /mnt/users/printfiles
 

PS2PDF=$( which ps2pdf )
DATE=$( date +%Y%m%d%H%M%S )
WHOAMI=$( id -un )

JOB=$1
PRTUSER=$2


# Check DEVICE DISCOVERY + test PS2PDF 
#
if [ "$JOB" = "" ]; then
    if test -f "$PS2PDF" ; then
	echo "network pdf2file:/mnt/users/printfiles \"Unknown\" \"PDF Writer (file)\" " 
	logger "pdf2file: INFO: driver information"
	exit 0  
    else
	echo "Error: $0 - ps2pdf is not available!"      
	logger "pdf2file: ERROR: ps2pdf is not available!"      
	exit 1 
    fi 
fi


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

logger "pdf2file: start job $1 for $2, $4 copies"

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


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


# Create output filename based on user name and user mode
#
if [ "$WHOAMI" == "root" -a "$PRTUSER" != "" ]; then
    PASSWD=$( grep "^$PRTUSER" /etc/passwd )
    if [ "$PASSWD" != "" ]; then
	HOMEDIR=$( echo $PASSWD | cut -d ':' -f 6 )
	PRTUID=$( echo $PASSWD | cut -d ':' -f 3 )
	PRTGID=$( echo $PASSWD | cut -d ':' -f 4 )
	mkdir -p $HOMEDIR/printfiles
	chown $PRTUID:$PRTGID $HOMEDIR/printfiles
	chmod 755 $HOMEDIR/printfiles
	FILENAME="$HOMEDIR/printfiles/${JOB}-${DATE}.pdf"
    else
	FILENAME="${PDFPATH}/${PRTUSER}-${JOB}-${DATE}.pdf"
    fi
else
    if [ "$PRTUSER" != "" ]; then
    	FILENAME="${PDFPATH}/${PRTUSER}-${JOB}-${DATE}.pdf"
    else
    	FILENAME="${PDFPATH}/pdf2file-${JOB}-${DATE}.pdf"
    fi
fi

logger "pdf2file: $FILENAME"


# Run ps2pdf (ghostscript)
#
if [ $# -eq 6 ]; then
    $PS2PDF $6 $FILENAME >& /dev/null
else
    $PS2PDF - $FILENAME >& /dev/null
fi
logger "pdf2file: written '${FILENAME}', result $?"


# If PASSWD is set then we run as root
#
if [ "$PASSWD" != "" ]; then
    # Let the user own the file
    chown $PRTUID:$PRTGID $FILENAME
    chmod 644 $FILENAME
else
    # Make file writable for the user.
    chmod 666 $FILENAME
fi

exit 0