#!/s/std/bin/perl

# $Id: condorstatus.pl,v 1.14 2008/12/16 21:57:33 koczan Exp $

# condorstatus.pl dumps out the condor bit for hosts so condor can
# properly configure machine pools within the CS department.

use strict;
use warnings;

use DBI;
use CSL::Pg;
use CSL::DBs;

use Date::Format;

my $dbh = DBI->connect($CSL::DBs::csdb)
  or die "Could not connect to CSDB: $!";
$dbh->do("set search_path to csdb");

# If they gave a file, redirect STDOUT to there.
my $outfile = shift @ARGV;
if ($outfile) {
  open STDOUT, '>', $outfile
    or die "$0: Could not open $outfile for writing: $!";
}

# Header
my @lc = localtime;
my $date = strftime("%D", @lc);
print "CS MACHINES AND THEIR CONDOR STATUS: $date\n";

# let's get cpuname length via a query
my $col_length_query = $dbh->prepare("select max(char_length(cpuname)) as width from machines where cpuname like '%.wisc.edu'");
$col_length_query->execute;
my $row = $col_length_query->fetchrow_hashref;

# finish up the header
# this format assumes things like 8-character login limits
my $format = "%-" . $row->{width} . "s %s";
printf "$format\n", "machine", "condor";
print "-" x 80 . "\n";

# get the data now
my $machines_query = $dbh->prepare("select distinct cpuname, condor from machines where cpuname like '%.wisc.edu' order by cpuname");

my $count = 0;
$machines_query->execute;
while (my $row = $machines_query->fetchrow_hashref) {
  my $condor = $row->{condor} ? 'Y' : 'N';
  printf "$format\n", $row->{cpuname}, $condor;
  $count++;
}
print "\n";

# Footer
# no footer this time

# clean up
$col_length_query->finish;
$machines_query->finish;
$dbh->disconnect;
