#! /usr/bin/perl -w
#
# NCP backend for CUPS
#
# Copyright (C) 2001 Sergey Vlasov <vsu@altlinux.ru>
#
#   This program is free software; you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by the Free
#   Software Foundation; either version 2 of the License, or (at your option)
#   any later version.
#
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
#   for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

use strict;

########################################################################
# Configurable parameters
########################################################################

# nprint program name (you may want to put the full pathname here)
my $NPRINT = "nprint";

# Home directory (the .nwclient file will be searched there)
my $NCP_HOME = "/root";

########################################################################
# End of configurable parameters
########################################################################



# No arguments means show available devices
if (scalar(@ARGV) == 0) {
    print "network ncp \"Unknown\" \"NetWare Printer via NCP\"\n";
    exit 0;
}

# Check number of arguments
if (scalar(@ARGV) < 5 || scalar(@ARGV) > 6) {
    print STDERR "ERROR: ncp job user title copies options [filename]\n";
    exit 1;
}

my ($job, $user, $title, $copies, $options, $file) = @ARGV;
my $printer = $ENV{"DEVICE_URI"};

# These variables will hold the URI parts
my ($server, $queue, $nwuser, $nwpass);

# Parse the printer URI into parts
for ($printer) {

    # ncp://USERNAME:PASSWORD@SERVER/QUEUE
    m|^ncp://(.+):(.*)@(.+)/(.+)| && do {
	$nwuser = $1;
	$nwpass = $2;
	$server = $3;
	$queue = $4;
	last;
    };

    # ncp://USERNAME@SERVER/QUEUE
    m|^ncp://(.+)@(.+)/(.+)| && do {
	$nwuser = $1;
	$server = $2;
	$queue = $3;
	last;
    };

    # ncp://SERVER/QUEUE
    m|^ncp://(.+)/(.+)| && do {
	$server = $1;
	$queue = $2;
	last;
    };
}

# Check if the URI was parsed correctly
if (not defined $server or not defined $queue) {
    print STDERR "ERROR: malformed printer URI\n";
    exit 1;
}

# Unquote the URI parts (must be done after splitting)
$nwuser =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge if defined $nwuser;
$nwpass =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge if defined $nwpass;
$server =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge if defined $server;
$queue  =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/ge if defined $queue;

# Fixed part of the nprint command
my @command = ($NPRINT, "-S", $server, "-q", $queue, "-N");

# Add "-U USERNAME" if specified
if (defined $nwuser) {
    push @command, "-U";
    push @command, $nwuser;
}

# Add "-P PASSWORD" if specified
if (defined $nwpass) {
    push @command, "-P";
    push @command, $nwpass;
}

# Append the print file name or "-" to read from stdin
if (defined $file) {
    if ($file =~ /^-/) {
	# Avoid file names which look like switches
	$file = "./$file";
    }
    push @command, $file;
} else {
    push @command, "-";
}

# nprint will read $HOME/.nwclient, so need to set it
$ENV{"HOME"} = $NCP_HOME;

# all is ready, run nprint (directly, without using shell)
my $result = system { $command[0] } @command;

# if not ok, print the error message in the format required by CUPS
if ($result != 0) {
    print STDERR "ERROR: nprint exited with status $result\n";
    exit 1;
}

# normal exit
exit 0;
