#!/usr/bin/perl
# $Id: alib.pl,v 1.4 1994/06/22 06:46:40 agl Exp agl $
# /usr/local/lib/apc/alib.pl

$firstpage="58";
$nextpage="58"; # "66";

$faxsys="glas";
$fqdn="by.glas.apc.org";
$faxdir="/usr/local/lib/pgpfax";
$faxrouteout=$faxdir."/route.fax.out";
$billdir="/usr/local/billing/";
$datadir="/var/local/transient/";

$justbody="/tmp/_tmp_$$_$$_b";
$postpgp="/tmp/_tmp_$$_$$";
$billdb=$billdir."users";

$pgp="/usr/local/bin/pgp";
$faxer="/etc/apc/bin/rfax"; #usr/local/bin/rfax";

#require("sys/fcntl.ph");
$userdbrec="A37A8A11A1";	# 37+8+11+1= 57; User Group Balance \n
$shityrec ="A37A8A12";	# Somewhat shity to make this fucking perl grok \n

sub logtrans {
    local($who,$type,$credits,$balance)=@_; $credits=-$credits;
    if(!open(TRANSDB, ">>$billdir/bills/$who")) {
	print ABORT "--ERROR-- alib.pl: can't open transaction for $who.\n";
	&aexit(-1);
    }
    flock(TRANSDB,$LOCK_EX); # a portable but ugly alternative
    $date=`date "+%a, %b %H:%M %Y"`; chop $date;
    $credits= sprintf("%1.2f",$credits);
    print TRANSDB "$date $type $credits$balance\n";
    close(TRANSDB);
}
sub charge { local($who,$type,$credits,$newgrp,$force)=@_; $status=0;
    if(!open(BILLDB, "+<$billdb")) {
	print ABORT "--ERROR-- alib.pl: can't open $billdb.\n";
	&aexit(-1);
    }
    flock(BILLDB,$LOCK_EX);          # a portable but ugly alternative of fcntl
    while(<BILLDB>) {
	$pos=tell-($userdblen=length($_));
				# in Linux only! Customize for your OS!
#	$lock=pack("sslli",($F_WRLCK,$SEEK_SET,$pos,$userdblen));
#	fcntl(BILLDB,F_SETLKW,lock);
	local($user,$group,$balance,$nl)=unpack($userdbrec,$_);
	if ( $user =~ /^$who/ ) {
	    if($credits>=0 && $balance<=$credits) {
		print ABORT
		    "--WARNING-- alib.pl: balance for $who is NEGATIVE.\n";
		if($force) { $status=-1; } else { &aexit(-1); }
		#return(-1);
	    }
	    $balance-=$credits;

	    if(!seek(BILLDB,$pos,$SEEK_SET))
	    {	print ABORT
		    "--ERROR-- alib.pl: can't position to $pos in BILLDB\n";
	    }
	    $group = $newgrp if $newgrp;
	    print BILLDB pack($userdbrec,($user,$group,$balance,$nl));
	    close(BILLDB);
	    if($type ne "PAYMENT") { $balance=""; }
	    else { $balance= sprintf(" New balance is %1.2f",$balance); }
	    &logtrans($who,$type,$credits,$balance); return($status);
	}
    }
    print ABORT "--ERROR-- alib.pl: user $who is not listed in database.\n";
    &aexit(-1);
}
sub adduser {
    local($who,$group)=@_;
    if(!open(BILLDB, "+<$billdb")) {
	print ABORT "--ERROR-- alib.pl: can't open $billdb for append.\n";
	&aexit(-1);
    }
    flock(BILLDB,$LOCK_EX);
    while(<BILLDB>) {
	local($user,$group,$balance,$nl)=unpack($userdbrec,$_);
	if ( $user =~ /^$who/ ) {
	    print "Ok, user is in the database\n"; close BILLDB;
	    return 1;
	}
    }
    print "Group for $who (<ENTER> to abort operation): ";
    chop($group=<STDIN>); if(!$group) { print "Ok, exiting\n"; return 0; }
	$shit="0.0        \n";
    print BILLDB pack($shityrec,($user,$group,$shit));
    #print BILLDB " \n"; 
    close BILLDB;
    return 1;
}
sub getbalance {
    local($who)=@_;
    if(!open(BILLDB, "+<$billdb")) {
	print ABORT "--ERROR-- alib.pl: can't open $billdb for append.\n";
	&aexit(-1);
    }
    flock(BILLDB,$LOCK_EX);
    while(<BILLDB>) {
	local($user,$group,$balance,$nl)=unpack($userdbrec,$_);
	if ( $user =~ /^$who/ ) {
	    close BILLDB;
	    return $balance;
	}
    }
    return -1;
}
