#!/bin/bash
#
# Ver. 0.7.1
#
# Boomaga provides a virtual printer for CUPS. 
# This can be used for print preview or for print booklets.
#


###########################################################
# Output "device discovery" information on stdout:
# http://www.cups.org/documentation.php/doc-1.6/man-backend.html
# device-class device-uri "device-make-and-model" "device-info" "device-id"
if test "$#" = "0";
then
  echo "file boomaga:/ \"Boomaga printer\" \"Boomaga\" \"MFG:Generic;CMD:PJL,PDF;MDL:Boomaga printer;CLS:PRINTER;DES:Virtual boomaga printer;DRV:DPDF,R1,M0;\""
  exit 0
fi

jobID=$1
user=$2
title=$3
count=$4
options=$5
inputFile=$6

# Escape possible double quotes (by using a bash-specific pattern substitution
# in parameter expansion) to construct valid cmd string.
title="${title//\"/\\\\\\\"}"

cmd="/usr/lib/boomaga/boomagabackend  ${jobID} \\\"${title}\\\" ${count} \\\"${options}\\\""

echo "DEBUG: [Boomaga] Start $cmd" >&2

if [ -n "${inputFile}" ];
then
    cat "${inputFile}" | su -c "/bin/bash -c \"${cmd} \"" ${user}
else
    su -c "/bin/bash -c \"${cmd} \"" ${user}
fi
