#!/usr/local/bin/perl
#
#Script to report on project_types 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::ProjectType;

use Glue::HPCC::Project::Project;

use Getopt::Long;

sub usage()
{	print STDERR <<EOF;
$0: Display information about a project type

Usage:
$0 [ --cluster CLUSTER ] [ --list ] PTYPE

Shows detailed information about the project type named PTYPE.
CLUSTER is the name of the cluster, which is in turn used
to determine which projects database to connect to.  It
will default based on the system you are running on.

EOF
}


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

my $help;
my $list;
my $cluster;

my $res = GetOptions(
	'help!' => \$help,
	'list!' => \$list,
	'cluster=s' => \$cluster,
);

unless ( $res ) 
{	usage;
	die "\nError processing options.\n";
}
if ( $help )
{	usage;
	exit 0;
}

my $ptname = shift @ARGV;
unless ($ptname)
{	usage;
	die "\nYou must supply a project type name\n";
}

if ( scalar(@ARGV) )
{	usage;
	my $tmp = join ", ", @ARGV;
	die "\nExtraneous arguments [ $tmp ], aborting.\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;

my $ptype = Glue::HPCC::Project::ProjectType->projecttype_by_name(
	$hpc, $ptname);
unless ( $ptype )
{	die "Unrecognized project type name '$ptname'.\n";
}
unless ( ref($ptype) )
{	die "Error looking up project type '$ptname': $ptype.\n";
}

my $pkey = $ptype->pkey;
my $desc = $ptype->description;
$ptname  = $ptype->name;
my $autorepl = $ptype->auto_replenishes?'Yes':'No';
my $normpri  = $ptype->has_normal_priority?'Yes':'No';
my $hipri  = $ptype->has_high_priority?'Yes':'No';
my $specpart  = $ptype->has_special_partition?'Yes':'No';
my $scavq  = $ptype->can_use_scavenger_q?'Yes':'No';
my $debq  = $ptype->can_use_debug_q?'Yes':'No';
my $defexpdays = $ptype->default_expiry_days;
my $defexpstr;
if ( defined $defexpdays )
{	my $defexpmos = sprintf "%.1f", $defexpdays/30;
	my $defexpyrs = sprintf "%.1f", $defexpdays/365;
	$defexpstr = "Default Expiry: $defexpdays days = $defexpmos months = " .
		"$defexpyrs yrs";
} else
{	$defexpstr = "Default Expiry: n/a";
}
my $requnix  = $ptype->requires_unix_group?'Yes':'No';
my $members  = $ptype->can_have_members?'Yes':'No';
my $metered  = $ptype->is_metered?'Yes':'No';

my $qos_tag = $ptype->qos_tag;
my $qoses = '';
if ( $qos_tag )
{	$qoses = $hpc->get_qos_list($qos_tag, "FOO") || [];
	$qoses = join ", ", @$qoses;
	$qoses = " ($qoses)";
}
$qos_tag = '<NULL>' unless $qos_tag;


print <<EOF;
Project Type: $ptname ($pkey)
Description: $desc
Auto-replenishes? $autorepl
Has normal priority access to standard partition? $normpri
Has high-priority access to high-priority partition? $hipri
Has a special partition? $specpart
Can use scavenger partition? $scavq
Can use debug partition? $debq
Requires unix group? $requnix
Can have members? $members
QoS Tag: $qos_tag$qoses
Is Metered?: $metered
$defexpstr
EOF

if ( $list )
{	my $projs = Glue::HPCC::Project::Project->pdb_query($hpc,
		projtype => $pkey);	
	unless ( $projs )
	{	print "\nNo projects in $cluster of type $ptname.\n\n";
		exit 0;
	}
	die "Error looking up projects of type $ptname: $projs.\n"
		unless ref($projs);

	my @projs = sort { $a->name cmp $b->name } @$projs;

	my @prjnames = map { $_->name } @projs;
	my $str = join "\n\t", @prjnames;
	print <<EOF;

Projects of type $ptname in cluster $cluster are:
	$str
EOF
}

