#!/bin/sh

if [ "$1" = "" ]; then
	# list supported output types
	echo 'network smbc "Unknown" "Windows Printer using smbclient"'
	exit 0
fi

job="$1"
account="$2"
title="$3"
numcopies="$4"
options="$5"
filename="$6"

if [ "$filename" = "" ]; then
	filename=-
fi

# strip protocol from printer
printer=`echo "${DEVICE_URI}" | sed 's/^.*://'`

# and print using smbclient
echo "NOTICE: smbclient -N -A /etc/samba/printing.auth -c \"print ${filename}\" \"${printer}\"" 1>&2

errtxt=`smbclient -N -A /etc/samba/printing.auth -c "print ${filename}" "${printer}" 2>&1`
ret=${?}


#
# Handle errors
#   see backend(7) for error codes

# log message
if [ "$ret" = "0" ]; then
	echo "$errtxt" | sed 's/^/NOTICE: /' 1>&2
else
	echo "$errtxt" | sed 's/^/ERROR: /' 1>&2
fi

# "NT_STATUS_LOGON_FAILURE" -> CUPS_BACKEND_AUTH_REQUIRED
echo "$errtxt" | grep -i 'LOGON_FAILURE' >/dev/null && exit 2
# "Unable to open credentials file!" -> CUPS_BACKEND_AUTH_REQUIRED
echo "$errtxt" | grep -i 'credentials' >/dev/null && exit 2
# "NT_STATUS_BAD_NETWORK_NAME" -> CUPS_BACKEND_STOP
echo "$errtxt" | grep -i 'BAD_NETWORK_NAME' >/dev/null && exit 4

# something went wrong, don't know what -> CUPS_BACKEND_FAILED
[ "$ret" != "0" ] && exit 1

# success! -> CUPS_BACKEND_OK
exit 0

