#!/moira/bin/perl -Tw

# $Id: longjobs.gen,v 1.1 2001-08-02 17:21:11 zacheiss Exp $

# 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/longjobs.out';

use DBI;

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

$sth = $dbh->prepare("SELECT name FROM list WHERE grouplist = 1")
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

umask 022;
open(OUT, ">$outfile") || exit $MR_OCONFIG;

while ($name = $sth->fetchrow_array) {
    
    $sth2 = $dbh->prepare("SELECT UNIQUE u.login FROM users u, imembers i, " .
			  "list l WHERE l.name = " . $dbh->quote($name) .
			  "AND l.list_id = i.list_id " .
			  "AND i.member_type = 'USER' " .
			  "AND i.member_id = u.users_id") || exit $MR_DBMS_ERR;

    $sth2->execute || exit $MR_DBMS_ERR;
		 
    $row = "$name:";
    $row =~ s/\0//g;
    print OUT $row;
    $maybecomma = "";

    while ($member = $sth2->fetchrow_array) {
	$row = "$maybecomma$member";
	$row =~ s/\0//g;
	print OUT $row;
	$maybecomma = ",";
    }

    $row = "\n";
    $row =~ s/\0//g;
    print OUT $row;
}

close(OUT);
$dbh->disconnect;

exit 0;
