#!/bin/sh
#
# nxsmb: Small wrapper for the SMB backend to be able to print to CIFS or SMB ports directly.
#
# Copyright (c) 2008 by Fabian Franz.
#
#

# turn on DEBUG?
#set -x -v

# Read the config file
. $(PATH=$(cd $(dirname $0) && pwd):$PATH which nxloadconfig) --

NXREDIR_LIBRARY="$PATH_LIB/rx-etersoft/libnxredir.so.0"
COMMAND_SMB="$CUPS_BACKEND/smb"

PORT=$(echo $DEVICE_URI | cut -d/ -f3 | cut -d@ -f2 | cut -d: -f2)
PROTOCOL=$(echo $DEVICE_URI | cut -d/ -f4)
PRINTER=$(echo $DEVICE_URI | cut -d/ -f5)

if [ -z "$PRINTER" ] # old style setup
then
	echo "Warning: Not using nxredir library. The DEVICE_URI is not in the right format."
	exec "$COMMAND_SMB" "$@"
fi

REAL_PORT="445" # CIFS
DEVICE_URI=$(echo $DEVICE_URI | sed "s|:$PORT/$PROTOCOL/|:$REAL_PORT/|g")
export DEVICE_URI=$(echo $DEVICE_URI | sed "s|nxsmb://|smb://|g")

export NXSAMBA_PORT="$PORT"

exec $PATH_BIN/nxredir "$COMMAND_SMB" "$@"
