#!/bin/sh
# Name: print-to-file
# Type: print-to-disk
# DPI: 600
#
#############################################################################
#
# Script that can be called to save a postscript job from stdin with mac
# lineendings into a specific directory on a Netatalk share.
# 
# It may be either called as a filter in an lpd compatible spooling system
# or directly from papd. To do so, add some lines to your papd.conf, eg.:
#
#     Print to Disk:\
#         :pr=|/usr/local/bin/print-to-file.sh:\
#         :pd=/usr/local/share/ghostscript/7.05/lib/destiller.ppd:\
#         :op=lp:
# 
# The postscript job on stdin will be saved in $TMP_FILE, some informations
# from the header will be saved in $HEADER_FILE, some information will be
# converted from MacRoman encoding to Latin1 (you will need GNU recode for
# this -- adjust the path below) and the job will be saved to a file in
# $TARGET_DIR, the name derived from the '%%Title' comment, truncated to
# $MAX_FILENAME_LENGTH and in case a file with that name already existed
# appended with a numerical suffix.
#
# To see correct filenames on the mac clients the volume containing 
# $TARGET_DIR should be set to Latin1 encoding, that means your 
# AppleVolumes file should read like
#
# 	/raid/hotfolder     codepage:maccode.iso8859-1     "Hotfolder"
#
# To get correct file type / creator code combinations you should have a
# proper configuration in your AppleVolumes.system, eg.:
#
# 	.ps	TEXT	vgrd
#
# In case, you want to test what this script does, assign the paths to fit
# your needs, set DEBUG=TRUE and pipe a postscript file, created on a mac,
# through the script, eg.:
#
#	cat test.ps | /usr/local/bin/print-to-file.sh
#
# In case, you want to feed a hotfolder with this script, you should make
# sure, that $TMP_FILE and $TARGET_DIR are located on the same filesystem:
#
# 	TMP_FILE=/raid/hotfolder/lowres_pdfs/.$$.ps
# 	TARGET_DIR=/raid/hotfolder/lowres_pdfs/in
#
#############################################################################
#
# author: Thomas Kaiser <mailto:thomas.kaiser@phg-online.de>
# license: GPL - http://www.gnu.org/copyleft/gpl.html
# url: http://users.phg-online.de/tk/netatalk/scripts/print-to-file.sh.gz
# date: 6 Jul 2002
#
#############################################################################
#
# Non-Warranty: 
# This script comes with absolutely no warranty.
#
# As there is only very limited checking in the code and nearly no error
# handling at the moment, this script will open several security holes
# (handling temporary files, etc.) Use in a production environment isn't
# recommended at all.
# 
# Use at your own risk!
#
#############################################################################
#
# define some paths
#
MAC2LATIN1="/usr/bin/recode applemac..latin1"
# SENDMAIL=/usr/sbin/sendmail
# ACHFILE=/usr/local/atalk-1.5.4/bin/achfile
TMP_FILE=/var/volumes/brennsemmel/PS-Files/.temporary-ps-$$.ps
HEADER_FILE=/tmp/$$.header
TARGET_DIR=/var/volumes/brennsemmel/PS-Files/in
MAX_FILENAME_LENGTH=29	# 31 or set to 29 if you want to create PDFs
DEBUG=FALSE
#
# some tests
#
if test "`echo -e "\060"`" = "0"
then
        EE='-e'
else
        EE=''
fi
#
# Storing the incoming job into a temporary file $TMP_FILE
#
cat >"$TMP_FILE"
#
# Extracting the PostScript header into $HEADER_FILE and extract 
# relevant informations
#
grep "^%\!PS" "$TMP_FILE" | tr "\015" "\012" | sed 's/)$//g' >"$HEADER_FILE"
RECIPIENT=`grep "^%%Routing" "$HEADER_FILE" | cut -b24-`
RAW_CREATOR=`grep "^%%Creator" "$HEADER_FILE" | cut -b13-`
CREATOR=`echo $EE "$RAW_CREATOR" | $MAC2LATIN1 | tr -d "\015"`
RAW_CREATIONDATE=`grep "^%%CreationDate" "$HEADER_FILE" | cut -b18-`
CREATIONDATE=`echo $EE "$RAW_CREATIONDATE" | $MAC2LATIN1 | tr -d "\015"`
OWNER=`grep "^%%For" "$HEADER_FILE" | cut -b9-`
PAGES=`grep "^%%Pages" "$HEADER_FILE" | cut -b10-`
RAW_TITLE=`grep "^%%Title" "$HEADER_FILE" | cut -b11-`
TITLE=`echo $EE "$RAW_TITLE" | $MAC2LATIN1 | tr "[/][:]" "_" | tr -d "\015"`
test $DEBUG = TRUE && echo $EE "\nExtracted information: \n\n\tRouting: $RECIPIENT \n\tTitle: $TITLE \n\tFor: $OWNER \n\tPages: $PAGES \n\tCreator: $CREATOR \n\tCreationDate: $CREATIONDATE\n"
#
# move PostScript file to the proper location and delete temporary files
#
SUFFIX=""
COUNTER=0
while true
do
        # TARGET_FILE="$TARGET_DIR"/"$TITLE$SUFFIX".ps
	SUFFIX_LENGTH=`echo $SUFFIX | wc -c`
	MAX_LENGTH=`expr $MAX_FILENAME_LENGTH - 2 - $SUFFIX_LENGTH`
	TARGET_FILE="$TARGET_DIR"/`echo $TITLE | cut -b-$MAX_LENGTH`"$SUFFIX".ps
	test $DEBUG = TRUE && echo $EE "file $TARGET_FILE\c"
	if [ -f "$TARGET_FILE" ] ; then
		COUNTER=`expr $COUNTER + 1`
		SUFFIX=".$COUNTER"
		test $DEBUG = TRUE && echo $EE " already exists. Skipping"
	else
		mv "$TMP_FILE" "$TARGET_FILE"
		chmod 666 "$TARGET_FILE"
		# $ACHFILE -t "TEXT" -c "vgrd" "$TARGET_FILE" 
                test $DEBUG = TRUE && echo $EE " used. Finished"
		break
	fi
done
rm "$HEADER_FILE"
exit 0

