#!/usr/bin/perl -w

##############################################################################
#
# Print billing management system - command-line quote generator, 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 program allows a user to see how much a print job is going to cost.
# The results are printed to standard output. The charge rates are defined
# on a per-printer basis, and can apply to either black or colour printing.
# The only command-line parameter available is an optional printer name. If
# no printer is given, defaults are used.
#
##############################################################################

use Printbill::printbill_pcfg;
use Printbill::printbill;
use POSIX 'setsid';
use File::Temp qw/ tempdir /;
use Getopt::Long;
use strict;

BEGIN { $ENV{PATH} = '/bin:/usr/bin' }

# $config is where we store the prices.

my $config = '/etc/printbill/printbillrc';
my $printer = "";
my ($file, $price, %params, @file_info, $i, @total_file_info, $dir);
my ($help, $verbose);

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

$printer = "";

GetOptions ("printer=s" => \$printer, "help" => \$help, "verbose" => \$verbose);

if (defined ($help) || $#ARGV == -1) {
	print "Usage: $0 file1 file2... fileN [--printer printer_name] [--help]\n";
	exit;
}

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

umask 0077;
$dir = tempdir (CLEANUP => 1);

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

foreach $file (@ARGV) {
	if (! -e $file) {
		print STDERR "Error: can't read $file. Aborting.\n";
		exit;
	} else {
		@file_info = printbill ($file, 0, "/tmp", %params);

		if ($file_info [0] ne "") {
			print STDERR "Error: something went wrong during postscript processing. Errors follow:\n\n$file_info[0]\n\n";
			exit -1;
		}
		
		for ($i = 0; $i < 5; $i++) {
			$total_file_info [$i] += $file_info [$i + 1];
		}
	}
}

if (defined ($verbose)) {
	print "Pages: $total_file_info[0]\n";
	print "Cyan: $total_file_info[1]\n";
	print "Magenta: $total_file_info[2]\n";
	print "Yellow: $total_file_info[3]\n";
	print "Black: $total_file_info[4]\n";
}

# Mono + pagecount is default.

if (!defined $params{'colourspace'} || $params{'colourspace'} eq 'mono') {
	$price = $total_file_info [0] * $params{'price_per_page'} +
		$total_file_info [4] * $params{'price_per_percent_black'};
} elsif ($params{'colourspace'} eq 'pagecount') {
# Pagecount only
	$price = $total_file_info [0] * $params{'price_per_page'};
} else {
# Colour + pagecount
	$price = $total_file_info [0] * $params{'price_per_page'} +
		$total_file_info [1] * $params{'price_per_percent_colour'} +
		$total_file_info [2] * $params{'price_per_percent_colour'} +
		$total_file_info [3] * $params{'price_per_percent_colour'} +
		$total_file_info [4] * $params{'price_per_percent_black'};
}
	
if ($printer eq "") {
	printf "Your print job has been costed at $params{'currency_symbol'}%.2f.\n", $price;
} else {
	printf "Your print job to printer \"$printer\" has been costed at $params{'currency_symbol'}%.2f.\n", $price;
}

exit 0;
