#!/bin/bash

##==============================================================================
#$ PDF        - Generic PDF (acroread) based filter script     Updated: 06/28/05
##------------------------------------------------------------------------------
##
## OPTIONS RECOGNIZED: ( all must be preceded by a "-" )                 
##                                                                       
## Any valid acroread command line argument                                 
##                                                                       
##==============================================================================

# The basic way this script works is that it parses any
# arguments and assumes those to be acroread arguments.  Then we route 
# STDIN through acroread giving acroread the modified list of arguments
# then the formatted output from acroread appears on our STDOUT.  We are 
# basically just translating the PDF to Postscript using acrobat reader.

# The following two lines determine the PDF -> Print coverter to use and 
# any required arguments it needs to do the conversion non-interactively.
#
#
# Start of lines added by joe 06-28-2005
# figure out what UNIX we're running under
UNAME=$(uname)
if [ "$UNAME" == "Darwin" ]
then
 pdfFilter="cat"
 requiredArgs="-"
else
 pdfFilter="acroread"
 requiredArgs="-toPostScript"
fi
#
# End of lines added by joe 06-28-2005
#

# Note: the following lines (if enabled) will allow the PDF files to flow
#       through directly to the lp/lpr printing system.  On linux, this 
#       will allow the linux printing subsystem handle the PDF print conversion.
#
#       But, be aware that if your linux printer driver is not written properly,
#       it can cause some very strange printed output.  That is why the script
#       defaults to using acroread.  This always seems to do a proper conversion.

# pdfFilter="tee"
# requiredArgs="/dev/null"

# set up redirection of stderr

if [ -z "$LOGFILE" ]
then
    UMASK=`umask`; umask 00             # save and set the umask
    log=/tmp/PDF_filter.last
    rm -rf $log
    echo "" > $log
    umask $UMASK                        # restore original umask                
else
    log=$LOGFILE
fi

exec 2>>$log

# do some logging

echo "" >> $log
echo "==========[ Starting filter ]================================================" >> $log
echo "`grep ^[#][$] $0|cut -b4-`" >> $log
echo "=============================================================================" >> $log
echo "" >> $log
echo "Filter Script Pathname: $0" >> $log
echo "Command line arguments: $*" >> $log
echo "" >> $log

# Initialize option variables to default values

# set up some defaults

#
# Can we find the acroread program?
#

if [ -z "`type $pdfFilter 2>/dev/null`" ]
then
    echo "*** Error, '$pdfFilter' is not installed or in the current PATH!" >> $log
    echo "==========[ Exiting filter ]================================================" >> $log
    exit 1
fi

#
# Determine which options have been invoked
#

count=$#

while [ $count -gt 0 ]
do
    case "$1" in

        #
        # Help
        #
        -[?]|--help)
                grep ^[#][#$] $0|cut -b3-
                exit 0
                ;;

        #
        # Version
        #
        --version)
                grep ^[#][$] $0|cut -b4-
                exit 0
                ;;

        #
        # Revision History
        #
        --history)
                grep ^[#][+] $0|cut -b4-
                exit 0
                ;;

        #
        # Catch any other left over arguments.  We will send them to acroread.
        #
	*)        #catch any non-processed arguments
                  args="$args $1"
                  ;;
    esac
    
    shift
    count=$(($count - 1))
done

echo "Current settings to use when formatting, "def" means Default." >> $log
echo "Extra Arguments: $args"          >> $log
echo ""                                >> $log

#
# Now add any arguments we didn't parse out.  They should already be raw acroread arguments.
#

# Don't append Appx fed in FORM options.  This was already handled when Appx
# created the PDF file.  

# arglist="$requiredArgs $args"

arglist="$requiredArgs"


#
# Log what we did
#

echo "$pdfFilter cmd: $pdfFilter $arglist" >>$log
echo "" >> $log

# Now pass the print file data on through acroread

$pdfFilter $arglist

# all done

echo "==========[ Exiting filter ]================================================" >> $log

exit 0

#+###########################################################################
#+ Modification History
#+
#+ DATE      BY   Description
#+ --------  ---  ---------------------------------------------------------
#+ 05/23/03  PHB  Initial Creation
#+ 05/23/03  PHB  Now ignoring FORM options.  They we used when the PDF was created.
#+ 05/23/03  PHB  Also, added comments to explain how to bypass acroread on linux if someone wants to  
#+ 06/28/05  joe  Added Darwin (mac os) specific test setting variables UNAME pdfFilter and requiredArgs based on uname results
#+###########################################################################
