#!/bin/sh

# Shell script to convert a specified postscript file into a PDF document
# and place it in a location that is shared by the Samba server.
#
# Version 1.0
#
# $1 = the path to the spool file name (postscript file)
# $2 = session  user  name  (the  user name that the client wanted,
#                            not necessarily the same as the one they got)
# $3 = primary group name of $2
# $4 = the NetBIOS name of the client machine
# $5 = the IP address of the client machine
# $6 = the job name as transmitted by the client
# $7 = the size of the spooled print job (in bytes)
# $8 = Samba location of PDF file
#
# For more info http://machiel.generaal.net

DEFAULT_PATH=/usr/local/share/pdf_printer/drop_box

if test $# -lt 5
then
  if test $# -ne 1
  then
    echo "Usage: $0 <postscript file>"
    echo "     : If you want to execute this script from samba you must give at least 5 values"
    exit;
  fi
fi

if ! test -e $1
then
  echo "Error: File does not exist."
  exit
fi


if test $# -lt 5
then
  base_name="tmp"
else
  base_name=$2
fi


FILENAME=$base_name" "$(/bin/date '+%d-%m-%Y')

counter="1"

while test -e "$DEFAULT_PATH/$FILENAME.pdf";do
   counter=`expr $counter + 1`
   FILENAME=$base_name" "$(/bin/date '+%d-%m-%Y')" ($counter)"
done

ps2pdf $1 "$DEFAULT_PATH/$FILENAME.temp"

mv "$DEFAULT_PATH/$FILENAME.temp" "$DEFAULT_PATH/$FILENAME.pdf"

FILESIZE=$(/usr/bin/du -h "$DEFAULT_PATH/$FILENAME.pdf" | /usr/bin/awk '{print $1}')

if ! test $# -lt 5
then
  printf "Your printjob \"$6\" is processed.\n\nLocation\t\t= $8\nOwner\t\t= $2\nDocument name\t= $FILENAME.pdf\nDocument size\t= $FILESIZE\n\n" | smbclient -U 'PDF Printer' -M $4

  rm $1
else
  echo "PDF File is created: $DEFAULT_PATH/$FILENAME.pdf"
fi


