#!/usr/bin/perl
# $Id: mfee.pl,v 1.0 1994/07/28 12:11:40 alexeyko Exp alexeyko $

$billdir="/usr/local/billing/";
$billdb=$billdir."users";

#require("sys/fcntl.ph");
$userdbrec="A37A8A11c";	# User Group Balance \n

(open(BILLDB, "+<$billdb")) || die "ERROR(mfee.pl): can't open $billdb.\n";
flock(BILLDB,$LOCK_EX);	# a portable but ugly alternative of fcntl

while( <BILLDB> ) {
	local($user,$group,$balance,$nl)=unpack( $userdbrec, $_ );
	print $_;
	if ($group=~/indy|mspon/) {$balance = $balance - 6;}
	else	{
		if ( $user  =~ /^@/ ) {$balance = $balance - 9;}
		else	{
			if ( $group =~ /vbank/ ) {$balance = $balance - 8;}
			}
		}
	$pos=tell-($userdblen=length($_));
	if(!seek(BILLDB,$pos,$SEEK_SET)) {
		print ABORT "--ERROR-- alib.pl: can't position to $pos in BILLDB\n";
	}
#	^^^^^ <-  in Linux only! Customize for your OS!
#	$lock=pack("sslli",($F_WRLCK,$SEEK_SET,$pos,$userdblen));
#	fcntl(BILLDB,F_SETLKW,lock);
	print BILLDB pack($userdbrec,($user,$group,$balance,$nl));
}
close(BILLDB);
exec("mail -s Monthly_fee_calculated_! alexeyko </dev/null");
