#!/moira/bin/perl -Tw

# $Id: print-accounting.gen 4030 2011-03-04 21:01:14Z zacheiss $
# The following exit codes are defined and MUST BE CONSISTENT with the
# error codes the library uses:
$MR_DBMS_ERR = 47836421;
$MR_OCONFIG = 47836460;

$outfile = '/moira/dcm/print-accounting.out';

use DBI;

$dbh = DBI->connect("dbi:Oracle:moira", "moira", "moira")
  || exit $MR_DBMS_ERR;


$sth = $dbh->prepare("SELECT p.name, l.name FROM list l, printers p " .
		     "WHERE (p.status = 1 OR p.status = 2) AND p.report_list = l.list_id AND l.maillist = 1 AND l.active = 1 " .
		     "ORDER BY p.name")
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

umask 022;
open(OUT, ">$outfile") || exit $MR_OCONFIG;
print OUT "# This File is automatically generated by Moira.  Do not edit.\n";

while (($name, $mailinglist) = $sth->fetchrow_array) {
    next if $name eq "[NONE]";
    $name = lc($name);
    print OUT "$name	$mailinglist\@mit.edu\n";
}

close(OUT);
$dbh->disconnect;

exit 0;
