#!/usr/athena/bin/perl -Tw
#
# $Id: warehouse.gen,v 1.1 2007-01-03 20:31:58 zacheiss Exp $
# Generate a database extract from the users table for the MIT Data Warehouse.
$MR_DBMS_ERR = 47836421;
$MR_OCONFIG = 47836460;

$outfile = '/moira/dcm/warehouse.out';

use DBI;

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

$sth = $dbh->prepare("SELECT login, clearid, first, last, middle " .
		     "FROM users WHERE (status = 1 OR status = 2 OR " .
		     "status = 5 OR status = 6 OR status = 7 OR status = 9)");

$sth->execute || exit $MR_DBMS_ERR;

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

while (($login, $clearid, $first, $last, $middle) = $sth->fetchrow_array) {
    next if (length($clearid) < 2);
    next if ($clearid eq "0" || $clearid eq "\0" || $clearid =~ /^.\D/);
    $row = "$login\t$clearid\t$first\t$last\t$middle\n";
    $row =~ s/\0//g;
    print OUT $row;
}

close(OUT);
$dbh->disconnect;

exit 0;
