#!/bin/bash

logfile=/tmp/watermarkpdf.log
watermark=/etc/cups/watermark.pdf

tempdir=$(mktemp -d)

echo $(date) "$0 $* (tempdir: $tempdir)" >> $logfile

# Command line arguments
job="$1"
user="$2"
title="$3"
numcopies="$4"
options="$5"
filename="$6"

if [ -z "$filename" ] ; then
	filename=”-”
fi

if [ $# -ge 7 ]; then
	cat $6 > $tempdir/ps.in
else
	cat > $tempdir/ps.in
fi

# convert Postscript to PDF
/usr/bin/ps2pdf $tempdir/ps.in $tempdir/pdf.in 2>>$tempdir/err

# watermarking
/usr/bin/pdftk $tempdir/pdf.in background "$watermark" output $tempdir/pdf.out 2>>$tempdir/err

# convert PDF to Postscript
/usr/bin/pdftops $tempdir/pdf.out - 2>>$tempdir/err

# clean-up
rm -rf $tempdir
