#!/bin/sh 
#

# Use job ID to get unique filename
STRDATE=$( date +"%y%m%d%H%M%S" )
JOBCAPDIR=/var/lanxum/SPOOL/
JOBCAPTURE="$JOBCAPDIR""$STRDATE"$1.pcl
JOBINFO="$JOBCAPDIR""$STRDATE"$1.pcl.xml
ENDPRINT="$JOBCAPDIR""$STRDATE"$1.end


# Create capture file using a friendly file access mode
umask 111

# Action to take depends on the number of arguments
case $# in 
    # No arguments - report info and return success
    0) echo "direct lsp1:${JOBCAPDIR} \"Unknown\" \"Lanxum SP1800-1\""
       exit 0 
       ;; 
    # 5 arguments - read from <stdin>
    5) echo "NOTICE: ${0##*/}: Saving standard input as ${JOBCAPTURE}" >&2
       exec lpstat -o | cat > ${JOBINFO}
       exec cat > "${JOBCAPTURE}"
       echo "Print End" | cat > "${ENDPRINT}"
       ;;
    # 6 arguments - read from given file
    6) echo "NOTICE: ${0##*/}: Saving ${6} as ${JOBCAPTURE}" >&2
       exec lpstat -o | cat > ${JOBINFO}
       exec cat "${6}" > "${JOBCAPTURE}"
       echo "Print End" | cat > "${ENDPRINT}"
       ;; 
    # Otherwise, invalid invocation
    *) echo "ERROR: ${0}: Wrong number of arguments" >&2
       exit 1
       ;;
esac

# Shouldn't ever get here
echo "ERROR: ${0}: Unknown internal error" >&2
exit 1 
