#!/usr/local/bin/perl
#
#Script to list colleges in PDB
use strict;
use warnings;

use lib "/cell_root/software/HPCC/lib/site_perl";
#Temporary for debugging
#use lib "/afs/.glue.umd.edu/software/HPCC/1.1/lib/site_perl";

use Glue::HPCC::Cluster;
use Glue::HPCC::Project::College;

use Getopt::Long;

sub usage()
{	print STDERR <<EOF;
$0: List projects

Usage:
$0 [ -verbose [ --parsable ] ] [ -cluster CLUSTER ] \\
	[ -grep REGEXP  [ --[no]case-sensitive ] ]

List colleges in projects DB for a HPC cluster.  
If CLUSTER is not provided, it will be defaulted based on the 
system you are running the command on (i.e. whatever HPC 
cluster it belongs to).

If REGEXP is given, only those colleges whose description match
REGEXP are displayed.  Matching is by default NOT case sensitive,
but the case-sensitive flag can change that.  Note that you must still
include the --verbose flag to see more than college codes in the output.

In non-verbose mode, only a list of college 'codes' printed.  
In verbose mode, some additional information is printed.
If --parsable also given, produce machine parsable output.


EOF
}

my $PARSABLE;

sub dump_verbose($;$)
#Dumps college entry in verbose mode
#If no arg given, print header
#Otherwise, we want a College object
{	my $hpc = shift;
	my $coll = shift;
	my $me = 'dump_verbose';

	die "Missing req parm hpc at " unless $hpc && ref($hpc);
	my $cluster = $hpc->db_clustername;

	#           clu pkey code name des
	my $fmt = "%-8s %-4s %-12s %-20s %-s\n";
	$fmt = "%s|%s|%s|%s|%s\n" if $PARSABLE;
	my $hfmt = $fmt;
	$hfmt="#$fmt" if $PARSABLE;
	unless ( $coll )
	{	#Print header mode
		printf $hfmt, "Cluster", "Pkey", "Code", "Name", "Description";
		return;
	}

	my $code = $coll->code;
	my $name = $coll->name;
	my $desc = $coll->description;
	my $pkey = $coll->coll_pkey;
	
	printf $fmt, $cluster, $pkey, $code, $name, $desc;
	return;
}


#-------------------------------------------------------------------
#		Main
#-------------------------------------------------------------------

my ($help, $verbose, $cluster);
my ( $regexp);
my $casesens = 0;

my $res = GetOptions(
	'help!' => \$help,
	'verbose!' => \$verbose,
	'parsable!' => \$PARSABLE,
	'cluster=s' => \$cluster,
	'grep=s' => \$regexp,
	'case-sensitive|case_sensitive|casesensitive!' => \$casesens,
);

unless ( $res )
{	usage;
	die "\nError processing options.\n";
}
if ( $help )
{	usage;
	exit 0;
}
if ( scalar(@ARGV) )
{	usage;
	my $tmp = join ", ", @ARGV;
	die "\nError: extraneous arguments [ $tmp ], aborting.\n";
}

if ( $PARSABLE && ! $verbose )
{	warn "WARNING: --parsable doesn't do anything unless --verbose set.\n";
}

#Default cluster if needed
my $hpc = Glue::HPCC::Cluster->new_from_clustername($cluster);
die "\nInvalid cluster $cluster at " if $cluster && ! $hpc && ! ref($hpc);
die "\nError instantiating cluster $hpc " unless $hpc && ref($hpc);
$cluster = $hpc->db_clustername;

#Get all college entries
my $colls = Glue::HPCC::Project::College->pdb_query($hpc);
die "No colleges found.  Error???? at " unless $colls;
die "Error looking up colleges: $colls at " unless ref($colls);

my @colls = @$colls;

if ( $regexp )
{	if ( $casesens )
	{	@colls = grep { $_->description =~ /$regexp/ } @colls;
	} else
	{	@colls = grep { $_->description =~ /$regexp/i } @colls;
	}
}
	
unless ( scalar(@colls) )
{	print "*** No colleges found in $cluster meeting requested criteria\n";
	exit;
}

if ( $verbose )
{	dump_verbose($hpc);
}

@colls = sort { $a->code cmp $b->code } @colls;

COLLEGE: foreach my $coll (@colls)
{	
	if ( $verbose )
	{	#We print for each matching entry
		dump_verbose($hpc,$coll);
	} else
	{	#We only print once
		print $coll->code, "\n";
	}
	next COLLEGE;
}
