#!/bin/bash
#
# Ver. @FULL_VERSION@
#
# 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 @CUPS_BACKEND_URI@ \"@CUPS_BACKEND_MODEL@\" \"@CUPS_BACKEND_INFO@\" \"@CUPS_BACKEND_ID@\""
  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="@NONGUI_DIR@/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
