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

ToolPath="/opt/XeroxOffice/prtsys"
CfgFlags=`cat ${ToolPath}/.xp_cfg`
DispFlag=`cat ${ToolPath}/.xp_disp`

## 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}

        if [ `w | awk '$3=="FROM" {print $3}'` ]; then
           Display=`w -h "$USER" | awk '$3 ~ /^:[0-9]/{print $3; exit;}'`
        else
           Display=`w -fh "$USER" | awk '$3 ~ /^:[0-9]/{print $3; exit;}'`
        fi

        if [ -n "$Display" ]; then
           export DISPLAY="$Display"
        else
           n=1000
           i=0
           while [ $n ]
           do
              content=`cat /etc/passwd | grep "$n"`
              if [ ! -n "$content" ]; then
                 break
              fi

              val=`ls -la /tmp | grep ".X$i-lock" | grep "$USER" | awk '{print $9}'`
              if [ -n "$val" ]; then
                 disp=$i
                 break
              fi

              n=`expr $n + 1`
              i=`expr $i + 1`
           done

           if [ -n "$disp" ]; then
              Display=":$disp"
              export DISPLAY="$Display"
           else
              # check whether DispFlag is set from the Installer 
              if [ ! -z "$DispFlag" ]; then 
                 export DISPLAY=`awk -F= '{print $2}' ${ToolPath}/.xp_disp`
              else
              # Set the default display value - must be explicitly added to xhost permissions
                 DISPLAY=${DISPLAY:-":0.0"}
                 export DISPLAY
              fi
           fi
        fi

        Mime=`file -b --mime-type "$*"`
        if [ -n "$Mime" ] && [ "$Mime" = "application/postscript" ]; then
           n=`head /dev/urandom | tr -dc A-Za-z0-9 | head -c 6 ; echo ''`
           TMPPRN="/tmp/PrtDrv-$n"
           touch $TMPPRN
           chmod 666 $TMPPRN
           chown lp:lp $TMPPRN
           /usr/sbin/cupsfilter -m application/vnd.cups-pdf -o scaling=65 -o fit-to-page -o position=center "$*">$TMPPRN 2>/dev/null
           ${ToolPath}/xeroxofficedriver -d${PRINTER} $Copies $UserName "$Title" $OptionList $TMPPRN 1>>/dev/null 2>>/dev/null
           _flagVal=$?
           if [ $_flagVal -eq 0 ] && [ -f $TMPPRN ]; then
              rm $TMPPRN
           fi
        else
           ${ToolPath}/xeroxofficedriver -d${PRINTER} $Copies $UserName "$Title" $OptionList $* 1>>/dev/null 2>>/dev/null
        fi

        exit 0
        ;;
esac
