#!/usr/bin/perl

##############################################################################
#
# Print billing management system - admin tools, version 4.2.0
#
# Copyright (C) 2000, 2001, 2002, 2003 Daniel Franklin
#
# This program is distributed under the terms of the GNU General Public
# License Version 2.
#
# This utility allows users to check their own quota.
#
##############################################################################

use Printbill::PTDB_File;
use Printbill::printbill_pcfg;
use Getopt::Long;
use Locale::gettext;
use POSIX;
use Fcntl;
use strict;

setlocale (LC_MESSAGES, "");
textdomain ("printbill");

my $config = '/etc/printbill/printbillrc';
my %params = pcfg ($config);
my ($opt_user, $user, %userhash);

GetOptions ('user=s' => \$opt_user);

if (defined ($opt_user)) {
	$user = $opt_user;
} else {
	$user = getpwuid ($<);
}

tie %userhash, "Printbill::PTDB_File", "$params{'db_home'}/users/$user.db", "TRUE" or do {
	die (sprintf (gettext ("%s: Error: cannot open file %s for reading: %s.\n"), $0, "$params{'db_home'}/users/$user.db", $!));
};

if (defined ($opt_user)) {
	printf ("%.2f\n", $userhash{"quota"});
} else {
	printf gettext ("You (%s) currently have %s%.2f credit.\n"), $user, $params{'currency_symbol'}, $userhash{"quota"};
}

untie %userhash;

exit 0;
