#!/moira/bin/perl -Tw

# $Id: ip-billing.gen 3956 2010-01-05 20:56:56Z 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;

$ENV{PATH} = "/bin:/usr/bin:/sbin:/usr/sbin";

$outdir = '/moira/dcm/ip-billing';
$outfile = '/moira/dcm/ip-billing.out';
$outdat = '/moira/dcm/ip-billing/moira.dat';
$outctl = '/moira/dcm/ip-billing/moira.ctl';
$count = 0;

use DBI;

umask 022;
open(DAT, ">$outdat") || exit $MR_OCONFIG;
open(CTL, ">$outctl") || exit $MR_OCONFIG;

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

# First report all the NETWK-A hosts.
$sth = $dbh->prepare("SELECT m.address, m.location, m.name, m.contact, " .
		     "m.billing_contact, m.account_number, m.created, " .
		     "m.modtime FROM machine m, subnet s WHERE m.status = 1 " .
		     "AND m.snet_id = s.snet_id AND s.status = 1 " .
		     "AND m.address != 'unassigned' ORDER BY m.address") 
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

while (($address, $location, $name, $contact, $billingcontact, $accountnumber, $created, $modtime) =
       $sth->fetchrow_array) {
    $row = "$address\t$location\t$name\t$contact\t$billingcontact\t$accountnumber\t+NETWK-A\t$created\t$modtime\n";
    $row =~ s/\0//g;
    print DAT $row;
    $count++;
}

# Next we do the private subnets.
# +NETWK-10P.
$sth = $dbh->prepare("SELECT s.saddr, s.name, s.contact, " .
		     "s.account_number, s.modtime FROM subnet s " .
		     "WHERE s.status = 2 ORDER BY s.saddr")
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

while (($saddr, $name, $contact, $accountnumber, $modtime) =
       $sth->fetchrow_array) {
    @addr = unpack("C4", pack("N", $saddr));
    $address = "$addr[0].$addr[1].$addr[2].$addr[3]";
    # Blank field between contact and account number below because that's
    # where the billing contact would go if we had it for subnets.
    $row = "$address\t\t$name\t$contact\t\t$accountnumber\t+NETWK-10P\t\t$modtime\n";
    $row =~ s/\0//g;
    print DAT $row;
    $count++;
}

# +NETWK-100P
$sth = $dbh->prepare("SELECT s.saddr, s.name, s.contact, " .
		     "s.account_number, s.modtime FROM subnet s " .
		     "WHERE s.status = 3 ORDER BY s.saddr")
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

while (($saddr, $name, $contact, $accountnumber, $modtime) =
       $sth->fetchrow_array) {
    @addr = unpack("C4", pack("N", $saddr));
    $address = "$addr[0].$addr[1].$addr[2].$addr[3]";
    # Blank field between contact and account number below because that's
    # where the billing contact would go if we had it for subnets.
    $row = "$address\t\t$name\t$contact\t\t$accountnumber\t+NETWK-100P\t\t$modtime\n";
    $row =~ s/\0//g;
    print DAT $row;
    $count++;
}

# +NETWK-1000P
$sth = $dbh->prepare("SELECT s.saddr, s.name, s.contact, " .
		     "s.account_number, s.modtime FROM subnet s " .
		     "WHERE s.status = 7 ORDER BY s.saddr")
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

while (($saddr, $name, $contact, $accountnumber, $modtime) =
       $sth->fetchrow_array) {
    @addr = unpack("C4", pack("N", $saddr));
    $address = "$addr[0].$addr[1].$addr[2].$addr[3]";
    # Blank field between contact and account number below because that's
    # where the billing contact would go if we had it for subnets.
    $row = "$address\t\t$name\t$contact\t\t$accountnumber\t+NETWK-1000P\t\t$modtime\n";
    $row =~ s/\0//g;
    print DAT $row;
    $count++;
}

# +NETWK-DORM
$sth = $dbh->prepare("SELECT m.address, m.location, m.name, m.contact, " .
		     "m.billing_contact, m.account_number, m.created, " .
		     "m.modtime FROM machine m, subnet s WHERE m.status = 1 " .
		     "AND m.snet_id = s.snet_id AND " .
		     "s.status = 5 AND m.address != 'unassigned' " .
		     "ORDER BY m.address")
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

while (($address, $location, $name, $contact, $billingcontact, $accountnumber, $created, $modtime) = 
       $sth->fetchrow_array) { 
    $row = "$address\t$location\t$name\t$contact\t$billingcontact\t$accountnumber\t+NETWK-DORM\t$created\t$modtime\n";
    $row =~ s/\0//g;
    print DAT $row;
    $count++;
}

# +NETWK-FSILG
$sth = $dbh->prepare("SELECT m.address, m.location, m.name, m.contact, " .
                     "m.billing_contact, m.account_number, m.created, " .
		     "m.modtime FROM machine m, subnet s WHERE m.status = 1 " .
                     "AND m.snet_id = s.snet_id AND " .
                     "s.status = 8 AND m.address != 'unassigned' " .
                     "ORDER BY m.address")
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

while (($address, $location, $name, $contact, $billingcontact, $accountnumber, $created, $modtime) = 
       $sth->fetchrow_array) { 
    $row = "$address\t$location\t$name\t$contact\t$billingcontact\t$accountnumber\t+NETWK-FSILG\t$created\t$modtime\n";
    $row =~ s/\0//g;
    print DAT $row;
    $count++;
}

# +NETWK-OCS
$sth = $dbh->prepare("SELECT m.address, m.location, m.name, m.contact, " .
		     "m.billing_contact, m.account_number, m.created, "  .
		     "m.modtime FROM machine m, subnet s WHERE m.status = 1 " .
		     "AND m.snet_id = s.snet_id AND " .
		     "s.status = 6 AND s.name LIKE '%-DHREG' " .
		     "AND m.address != 'unassigned' ORDER BY m.address")
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

while (($address, $location, $name, $contact, $billingcontact, $accountnumber, $created, $modtime) =
       $sth->fetchrow_array) {
    $row = "$address\t$location\t$name\t$contact\t$billingcontact\t$accountnumber\t+NETWK-OCS\t$created\t$modtime\n";
    $row =~ s/\0//g;
    print DAT $row;
    $count++;
}

# +NETWK-0
$sth = $dbh->prepare("SELECT m.address, m.location, m.name, m.contact, " .
		     "m.billing_contact, m.account_number, m.created, " .
		     "m.modtime FROM machine m, subnet s WHERE m.status = 1 " .
		     "AND m.snet_id = s.snet_id AND " .  
		     "(s.status = 4 OR s.status = 6) AND " .
		     "s.name NOT LIKE '%-DHREG' " .
		     "AND m.address != 'unassigned' ORDER BY m.address") 
    || exit $MR_DBMS_ERR;

$sth->execute || exit $MR_DBMS_ERR;

while (($address, $location, $name, $contact, $billingcontact, $accountnumber, $created, $modtime) =
       $sth->fetchrow_array) {
    $row = "$address\t$location\t$name\t$contact\t$billingcontact\t$accountnumber\t+NETWK-0\t$created\t$modtime\n";
    $row =~ s/\0//g;
    print DAT $row;
    $count++;
}

print CTL "$count\n";
close(CTL);
close(DAT);

system("cd $outdir && tar cf $outfile .") == 0 || exit $MR_OCONFIG;

$dbh->disconnect;

exit 0;
