#!/bin/sh
#############################################################################
#
# Description:
#
# This script can be used as a notification script for a Helios Create PDF
# printer queue. It's aim is to split a newly created PDF into single pages.
# To do this, it creates a subfolder within the queue's target folder and 
# stores the single page PDFs inside.
#
# Currently it works only with ES 3.1 (due to the format of parameters
# supplied by the EtherShare printer queue and paths to the utilities)
#
#############################################################################
#
# TODO:
# 
# Ensure, that while renaming no PDFs will be created with names longer 
# than 31 chars to avoid visibility problems for mac clients (It's a known
# limitation of AFP < 3.x that filenames must not be longer than 31 bytes)
# 
#############################################################################
#
# author: Thomas Kaiser <mailto:tk@kaiser-edv.de>
# license: GPL - http://www.gnu.org/copyleft/gpl.html
# url: http://users.phg-online.de/tk/helios/scripts/pdf-splitter.sh
# date: 9 Jan 2002 (v.0.0.1)
#
#############################################################################
#
# Changes:
# 0.0.1 initial release
#
#############################################################################
#
# Non-Warranty: 
# This script comes with absolutely no warranty. Use at your own risk!
#
#############################################################################

DT=${HELIOSDIR}/bin/dt
JOBDIR=`/usr/bin/dirname "${HELIOS_VFFILE}"`
JOBNAME=`/usr/bin/basename "${HELIOS_VFFILE}" .pdf`
# create subdirectory for single page PDFs
${DT} mkdir -m 2775 "${JOBDIR}/${JOBNAME}"
# split PDF
cd "${JOBDIR}/${JOBNAME}"
${HELIOSDIR}/bin/pdfcat -e "" "${HELIOS_VFFILE}"
# rename PDFs
for PDF in "${JOBDIR}/${JOBNAME}/"*.pdf ; do
	PAGENUMBER=`/usr/bin/basename "${PDF}" .pdf`
	${DT} mv "${PDF}" "${JOBDIR}/${JOBNAME}/${PAGENUMBER}_${JOBNAME}.pdf"
	echo "${JOBDIR}/${JOBNAME}/${PAGENUMBER}_${JOBNAME}.pdf"
done
${DT} upd "${JOBDIR}/${JOBNAME}"
# Notify the user
${HELIOSDIR}/bin/afpmsg -u ${HELIOS_JOBFOR} \
"\"${HELIOS_JOBTITLE}.pdf\" in ${HELIOS_JOBPAGES} Einzelseiten zerhackt"
exit 0

