#!/bin/bash
#
# This script is Free Software (as in beer)
# You may do whatever you want with it as long as you keep
# the author's credits:
#
# Martin Rode, Programmfabrik GmbH, www.programmfabrik.de
#
# The script takes one argument from the samba server
#
#
# Call Samba like this:
#
# [pdfprinter] 
#         print command = /smbhome/pdfprinter/pdfscript %s
#         path = /smbhome/pdfprinter
#         printable = yes         
#         comment = Treiber: Minolta Color PageWorks/Pro PS
#
#
 

# find the file name
FILE=$(egrep "^%%Title:" "$1" | head -n 1 | sed -e 's/^%%Title: //g' | sed -e 's/^(//g' | sed -e 's/\(.*\)\..*$/\1/g' | tr -d "\r\n")
FILE=`echo -n -e $FILE`

# remove crap from the beginning and end of the ps file
POS_S=`egrep -n "^%\!PS-Adobe" "$1" | cut -d : -f 1 `
POS_E=`egrep -n "^%%EOF" "$1" | cut -d : -f 1`
sed 1,$[$POS_S - 1]d "$1" | head -n $[$POS_E - $POS_S + 1] > "${FILE}.ps" 

# now we can remove the smb file
rm -f "$1"

#make a pdf
gs -q -dCompatibilityLevel=1.3 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="${FILE}.pdf" -c save pop -f "${FILE}.ps"

#remove the ps
rm -f "${FILE}.ps"
