#!/bin/sh
###############################################################
##                                                           ##
##  Copyright (C) 2010 by Xerox Corp.  All rights reserved.  ##
##                                                           ##
###############################################################

ToolPath="/opt/Xerox/prtsys"
CfgFlags=`cat ${ToolPath}/.xp_cfg`

## Determine configuration
for flag in ${CfgFlags}
do
    case $flag in
	LPRng | V5 | AIX | BSD | CUPS)
	    flConfig=$flag
	    ;;
    esac
done

## Main section of the script
case ${flConfig:-V5} in
    AIX) ## Native AIX support
		cd ${ToolPath}
		${ToolPath}/xeroxdriver -f AIX $* 1>>/dev/null 2>>/dev/null
        exit 0;
	;;

    V5) ## Solaris and HP-UX native lpd subsystems
        PATH=/usr/sbin:/usr/bin:/bin
        PRINTER=`/usr/bin/basename $0`
        XPrintRequest_id=`echo "-o JobID=${1}"`
        UserName=`echo -u${2} | sed -e"s/@.*//"`
        USER=$2
		if [ "_$3" != "_" ];
		then
        	Title=`echo -t${3} | sed -e "s/###[ 0-9A-z#].*//"`
		fi
        OptList=$5
        export PATH PRINTER XPrintRequest_id UserName Title XPrintCopies OptList
        shift 5

        HOSTOS=`uname -s`
        case $HOSTOS in
			HP-UX)
            	if [ "_$OptList" != "_" ]; then
					OptionList=`echo $OptList | sed -e "s/ -oBSD[A-z0-9].*//g" -e"s/ / -o/g" -e"s/^/ -o/"`
				fi
	            export OptionList
			;;

			SunOS)
            	LD_LIBRARY_PATH=${ToolPath}/v5lib:/lib:/usr/lib
            	if [ "_$OptList" != "_" ]; then
	            	OptionList=`echo $OptList | sed -e "s/flist=['A-z0-9].*//g" -e"s/ / -o/g" -e"s/^/ -o/"`
            	fi
            	export LD_LIBRARY_PATH OptionList
			;;
		esac

        # Set the default display value - must be explicitly added to xhost permissions
        DISPLAY=${DISPLAY:-":0.0"}
        export DISPLAY
        cd ${ToolPath}
        ${ToolPath}/xeroxdriver -d${PRINTER} $XPrintCopies $UserName "$Title" $OptionList $* 1>>/dev/null 2>>/dev/null
        exit 0
        ;;

    CUPS) ## Common Unix Printing System support
        PATH=/usr/sbin:/usr/bin:/bin
        RequestID=`echo "$1" | sed -e "s/^/-o JobID=/g"`
        UserName=`echo "$2" | sed -e "s/^/-u /g" -e "s/\"//g"`
        USER=$2
        Title=`echo "$3" | sed -e "s/^/-t/g" -e "s/\"//g"`
		if [ "_${5}" != "_" ]; then
	        OptionList=`echo "$5" | sed -e "s/ / -o /g" -e "s/^/-o /" -e "s/\"//g"`
	    fi
        shift 5

        cd ${ToolPath}

        # Set the default display value - must be explicitly added to xhost permissions
        DISPLAY=${DISPLAY:-":0.0"}
        export DISPLAY
        ${ToolPath}/xeroxdriver -d${PRINTER} $Copies $UserName "$Title" $OptionList $* 1>>/dev/null 2>>/dev/null
        exit 0
        ;;
esac
