#!/usr/bin/perl -w

##############################################################################
#
# Print billing management system - bottom-level queue filter, version 4.1.2
#
# Copyright (C) 2000, 2001, 2002, 2003 Daniel Franklin
#
# This program is distributed under the terms of the GNU General Public
# License Version 2.
#
# This filter is used as the final component in the two-stage print queue
# scheme. It accepts any job print job from the special user
# $params{"printbilld_user"} (`lp' on Debian).
#
##############################################################################

use Printbill::printbill_pcfg;
use Getopt::Long;
use Sys::Syslog qw(:DEFAULT setlogsock);
use strict;

# $config is where we store the prices.

my $config = "/etc/printbill/printbillrc";
my $user = "";
my $JSUCC = 0;
my $JREMOVE = 3;
my %params;

setlogsock('unix') or die "$0: cannot set log type: $!\n";
openlog ($0, 'cons', 'lpr');

# Parse standard command-line options sent to us by lprng

if ($] >= 5.005) {
	Getopt::Long::Configure ("pass_through");
	Getopt::Long::Configure ("bundling");
} else {
	Getopt::Long::config ("pass_through");
	Getopt::Long::config ("bundling");
}

GetOptions ("n=s" => \$user);

%params = pcfg ($config, "");

die_cleanup ($JREMOVE, "$0: problems parsing configuration file\n") if (! scalar (%params));

# At this point we have all configuration options and the command-line
# arguments which have been passed to us by lprng.

if ($user eq $params{'printbilld_user'}) {
	syslog ('info', "Secondary queue has accepted job")
		or die "$0: could not write to syslog: $!\n";

	exit $JSUCC;
} else {
	syslog ('info', "Secondary queue has rejected job - $user not $params{'printbilld_user'}")
		or die "$0: could not write to syslog: $!\n";

	exit $JREMOVE;
}
