#!/usr/bin/perl -w

######################################################################
#
# Check the tcp quotas for the user(s) so faar.
# 
#
# 1996-09-15 turbo:  begun
# 1996-09-24 turbo:  added cost to the list function
#
# $Header: /usr/local/src/cvsroot/tcpquota/chktcpquota,v 1.24 1998/03/31 12:16:03 turbo Exp $
#
# $Log: chktcpquota,v $
# Revision 1.24  1998/03/31 12:16:03  turbo
# Make sure we search and opens the correct config file,
# set by the variables '{lib|conf}_dir' at the top...
#
# Revision 1.23  1998/03/14 23:02:57  turbo
# If we can't open a connection to the mSQL database, say so and include the
# information to _WHICH_ server we can not connect to...
#
# Revision 1.22  1997/12/03 10:15:23  turbo
# * Moved the 'list_dbase()' to the library file, it was (practicaly) used
#   by the 'addtcpquota' script to...
#
# Revision 1.21  1997/11/26 21:29:54  turbo
# Removed a lot of config file variables, that could possibly confuse a new
# user/admin of the package... We asume that whoever chooses to install the
# package, use our default... If not, they can go in and change the stuff
# themselvs!!
#
# Revision 1.20  1997/11/26 19:53:27  turbo
# Moved the function 'list_logg()' to the library file, it was needed
# elsewhere to...
#
# Revision 1.19  1997/11/06 13:18:59  turbo
# Fixed a complaint, that when running without param's or with the param
# '--user' (which both outputs the users own quota), and the user did not exist
# in the database (because he/she have not payed for any TCPQuota), the program
# said that '<user> does not exists in database' which was taken as an system
# error...
#
# Revision 1.18  1997/11/04 14:53:32  turbo
# We should require './lib/tcpquota.pl' instead of 'lib/tcpquota.pl'...
#
# Revision 1.17  1997/10/19 19:24:46  turbo
# Tell the user why the user can not access the net if MIN_QUOTA eq 0, and he/she
# is bellow that...
#
# Revision 1.16  1997/10/19 18:50:20  turbo
# If called without arguments, output your own quota...
#
# Revision 1.15  1997/10/19 18:24:31  turbo
# The script 'chktcpquota' did almost the same thing as the script 'tcpquota',
# the only difference was that 'tcpquota' check your own quota, while 'chktcpquota'
# could check ALL users quota... Implemented the function to only check your
# own quota in 'chktcpquota'... Could be a little nicer though...
#
# Revision 1.14  1997/10/18 00:55:51  turbo
# * When listing the quota/log database, output a little header with username
#   of the user printing the list with the date.
# * Sort the list alphabeticly...
# * Make sure local variables stays local, I should really double check that
#   in the other programs to...
#
# Revision 1.13  1997/10/16 17:46:52  turbo
# If we don't have any params, give the correct help...
#
# Revision 1.12  1997/10/16 17:26:42  turbo
# When getting a users tcptab entry, no need to do it in a while, we have
# already selected the correct line above... This way, if a user does not
# exists, we can say so...
#
# Revision 1.11  1997/10/16 17:06:21  turbo
# If the logging table does not exists, exit cleanly...
#
# Revision 1.10  1997/10/16 16:50:52  turbo
# Added support for printing the log... The new function 'list_logg()' does
# this...
#
# Revision 1.9  1997/10/16 16:38:09  turbo
# If the first character is a '-', the user owes money, if not, the user have
# quota to use...
#
# Revision 1.8  1997/10/16 15:46:32  turbo
# Moved the function 'calculate_cost()' to the library file, no need to have
# it in all files...
#
# Revision 1.7  1997/10/12 17:47:52  turbo
# * Removed the fucked up header lines...
# * Perl complained about a uninitialized variable... Set it to zero if it is
#   empty...
#
# Revision 1.6  1997/10/02 11:56:39  turbo
# No debugging please...
#
# Revision 1.5  1997/09/26 15:54:53  turbo
# * Forgot the '$DEBUG' variable... Perl complained...
# * Better looking output in the user/cost/quota listing...
# * Use the config files 'RATE_QUOTA' instead of the local 'RATE_PRICE' variable.
#
# Revision 1.4  1997/08/17 17:20:40  turbo
# * Moved some functions to the library file.
# * Deleted some variables, they exists in the config file.
# * Changed and deleted some site specific entries, and made them less site
#   specific...
#
#

# Include some magic...
use Msql;
use POSIX;
package main;
#use Getopt::Long;

$lib_dir  = "./lib";
$conf_dir = "./confs";

require "$lib_dir/tcpquota.pl";

$DEBUG = 0;
$ALL   = 0;  # Not all...
$LOG   = 0;  # Not logging...
$OWN   = 0;  # Not our own quota...

# Who are running the program?
$USER = $ENV{'USER'};

# Does we have any arguments?
if( $ARGV[0] ) {
    # Ohh yes.... Process it...

    foreach $arg (@ARGV) {
	# Help?
	if( ($arg eq 'help') || ($arg eq '--help') || ($arg eq '-h') ) {
	    help();
	}

	if( ($arg eq 'user') || ($arg eq '--user') || ($arg eq '-u') ) {
	    $OWN = 1;
	}

	if( ($arg eq 'all') || ($arg eq '--all') || ($arg eq '-a') ) {
	    $ALL = 1;
	}

	if( ($arg eq 'log') || ($arg eq '--log') || ($arg eq '-l') ) {
	    $LOG = 1;
	}
    }
} else {
    $OWN = 1;
}

if( ! $ALL && !$LOG && !$OWN ) {
    if( $ARGV[0] ) {
	$user = $ARGV[0];
    }
}
if( $OWN ) {
    $user = $USER;
}

######################################################################
#
# configuration parameters
#

$PROG="chktcpquota";
$CF_FILE="tcpquota.cf";
$CF_FILE="tcpquota.debug.cf" if (defined $ENV{DEBUG} or $DEBUG);
%cf=(); # config array.
&readconfig("$conf_dir/$CF_FILE",$PROG);

######################################################################
#
# initializing stuff
#

# Open up the msql connection...
$dbh = Msql->connect( $cf{'SERVER'}, "tcpquota")
    || die( "Can't connect to msqld at '$cf{'SERVER'}'." );

######################################################################
#
# main loop
#
$date = get_timestring();
if( $ALL == 0 && $LOG == 0 ) {
    &read_dbase( $user );
} elsif( $LOG == 1 ) {
    # Print out a little header...
    printf("List printed by $USER $date\n\n");

    &list_logg("logging");
} else {
    # Print out a little header...
    printf("List printed by $USER $date\n\n");
    printf("User name      Quota         Money\n");

    &list_dbase("tcptab");
}
exit(0);

######################################################################
#
# read_dbase( $user )
#
# Check if the user is in the database...
#
sub read_dbase {
    local($user) = @_;
    my($name, $sec, $cost, $query, $sth);

    # Query the database.
    $query = "select * from tcptab where name like '$user'";
    $sth = $dbh->query($query) || die( "Error when query..." );

    ($name,$sec) = $sth->fetchrow;
    if( $name ) {
	if( $sec ) {
	    if( $OWN ) {
		if( $cf{'LANGUAGE'} eq 'svenska' ) {
		    print "Detta ar ett meddelande fran din lokala Internet vakt...\n";
		} else {
		    print "This is a message from your local Internet watch...\n";
		}
	    }

	    # Calculate the cost...
	    $cost = calculate_cost($sec);

	    # Calculate number of minutes...
	    $min  = int($sec/60);

	    if( $cost =~ /^\-/ ) {
		# Remove the minus chars...
		$cost = (split(/-/, $cost))[1];
		$min  = (split(/-/, $min))[1];

		if( $OWN ) {
		    if( $cf{'LANGUAGE'} eq 'svenska' ) {
			print "Du har varigt paloggad i cirka `$min' minuter,\n";
			print "vilket betyder att du r skyldig $cost $cf{'MONEY_VALUE'}.\n\n";
			if(! $cf{'MIN_QUOTA'} ) {
			    print "Du kan inte lngre komma ut p ntet, svida du inte betalar in pengar...\n"
			}
			print "\n";
		    } else {
			print "You have been online for about `$min' minutes,\n";
			print "which means that you owes $cost $cf{'MONEY_VALUE'}.\n\n" if($sec < 0);
			if(! $cf{'MIN_QUOTA'} ) {
			    print "You can no longer access the net, unless you make a money deposit...\n";
			}
			print "\n";
		    }
		} else {
		    if( $cf{'LANGUAGE'} eq 'svenska' ) {
			printf("%-10s r skyldig %5d kronor.\n\n", $user, $cost);
		    } else {
			printf("%-10s owes %5d kronor.\n\n", $user, $cost);
		    }
		}
	    } else {
		if( $OWN ) {
		    if( $cf{'LANGUAGE'} eq 'svenska' ) {
			print "Du har betalat fr cirka `$min' oanvnda minuter,\n";
			print "vilket betyder att du har $cost $cf{'MONEY_VALUE'} at disponera.\n\n" if($sec > 0);
		    } else {
			print "You have payed for about `$min' unused minutes,\n";
			print "which means that you have $cost $cf{'MONEY_VALUE'} at your disposal.\n\n" if($sec > 0);
		    }
		} else {
		    if( $cf{'LANGUAGE'} eq 'svenska' ) {
			printf("%-10s har %5d kronor till godo.\n\n", $user, $cost);
		    } else {
			printf("%-10s have %5d kronor.\n\n", $user, $cost);
		    }
		}
	    }
	} else {
	    if( $OWN ) {
		if( $cf{'LANGUAGE'} eq 'svenska' ) {
		    print "Du har inte accumulerat ngon TCP quota...\n";
		} else {
		    print "You have not accumulated any TCP quota...\n";
		}
	    } else {
		if( $cf{'LANGUAGE'} eq 'svenska' ) {
		    # $USER does not have any quota...
		    print "$user har inte accumulerat ngon TCP quota...\n";
		} else {
		    # $USER does not have any quota...
		    print "$user have not accumulated any TCP quota...\n";
		}
	    }
	}
    } else {
	if(! $OWN ) {
	    if( $cf{'LANGUAGE'} eq 'svenska' ) {
		# $USER was not found in database...
		print "$user existerar inte i databasen...\n";
	    } else {
		# $USER was not found in database...
		print "$user does not exists in database...\n";
	    }
	}
    }
}

######################################################################
#
# truncate_value()
#
sub truncate_value {
    local($temp) = @_;
    my($sum);

    if( $temp ) {
	# Remove the leading '-'.
	@temp = split(/-/, $temp);

	# Remove the fractals
	$sum  = int($temp);

	# Return the sum...
	return $sum;
    }
}

sub help {
    print "Use: $0 [[all] [log] [user] [<user>]]\n";
    print "     $0 [[--all] [--log] [--user] [<user>]]\n";
    print "     $0 [[-a] [-l] [-u] [<user>]]\n";
    print "     (or any combination there of...)\n";
    exit(0);
}
