#!/usr/local/bin/perl
#
#Script to display user in PDB
use strict;
use warnings;

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

use Glue::HPCC::Cluster;
use Glue::HPCC::Project::Person;
use Glue::HPCC::Project::PersonProject;

use Getopt::Long;
my $VERBOSE=0;

sub usage()
{	print STDERR <<EOF;
$0: Display information about an user

Usage:
$0 [ -cluster CLUSTER ] [ -verbose ] [ -user ] [ --inactive ]  USERNAME

Shows detailed information about an user.
Will connect to DB for HPCC CLUSTER; will be
defaulted based on host running on if omitted.

Giving -verbose flags will provide successively more
detail, (up to 3 times).  Among the information provided
in verbose mode is a list of roles the user belongs to
for various projects.  By default, only roles for active
projects are displayed; use --inactive flag to see roles
for inactive projects as well.

USERNAME can either be username or uid of user.

EOF
}

my $usersep='';

sub dump_user($$;$)
#Dump an user
{	my $hpc = shift;
	my $uname = shift;
	my $options = shift || {};
	my $me = 'dump_user';

	die "$me: Invalid/missing hpc object '$hpc' at "
		unless $hpc && ref($hpc);
	die "$me: Missing username/uid at " unless $uname;

	my $show_disabled = $options->{inactive};

	my $user;
	if ( $uname =~ /^\s*\d+\s*$/ )
	{	#Looks like uid
		$user = Glue::HPCC::Project::Person->person_by_uid($hpc,
			$uname);
		unless ( $user )
		{	print "No uid $uname found in PDB.\n";
			return;
		}
		unless ( ref($user) )
		{	die "***ERROR looking up user with uid $uname in PDB: $user\n";
			return;
		}
	} else {
		#Assume is username
		$user = Glue::HPCC::Project::Person->person_by_username($hpc,
			$uname);
		unless ( $user )
		{	print "No user $uname found in PDB.\n";
			return;
		}
		unless ( ref($user) )
		{	die "***ERROR looking up user $uname in PDB: $user\n";
			return;
		}
	}

	$uname = $user->username;
	my $pkey = $user->ppl_pkey;
	my $full = $user->fullname;
	my $email = $user->email;
	my $uid = $user->uid;
	my $active = $user->active;
	my $minhrs = $user->min_hrs_btwn_alerts;
	my $actflag = '';
	my $created = $user->created;
	my $updated = $user->lastupdated;

	$actflag = '[ DISABLED ]' unless $active;
	if ( defined $minhrs )
	{	$minhrs = sprintf "%.1f", $minhrs;
	} else
	{	$minhrs = '----';
	}

	print $usersep;
	$usersep = "\n----------------------------------------------------\n";
	print "Username: $uname $actflag\n";
	print "Primary key: $pkey\n" if $VERBOSE > 1;
	print "UID: $uid\n" if $VERBOSE;
	print "Fullname: $full\n";
	print "Email: $email\n";
	print "Min Hrs Btwn Alerts: $minhrs\n" if $VERBOSE;
	print "Created: $created\n" if $VERBOSE > 1;
	print "Last Updated: $updated\n" if $VERBOSE > 1;

	my $pplprjs = Glue::HPCC::Project::PersonProject->get_project_persons_by_username($hpc,$uname);
	unless ( $pplprjs )
	{	print "\nUser $uname is not attached to any projects.\n\n";
		return;
	}
	unless ( ref($pplprjs) eq 'ARRAY' )
	{	die "***ERROR looking up projects for user $uname at ";
		return;
	}

	print "\nUser $uname is attached to the following projects: \n";

	PPLPRJ: foreach my $pplprj (@$pplprjs)
	{	my $prj_pkey = $pplprj->prj_pkey;
		my $is_member = $pplprj->is_member;
		my $is_poc = $pplprj->is_poc;
		my $is_coord = $pplprj->is_coordinator;

		my $proj = Glue::HPCC::Project::Project->project_by_pkey(
			$hpc, $prj_pkey);
		unless ( $proj )
		{	die "***ERROR: no project with pkey=$prj_pkey but " .
				"referenced in PersonProject table at ";
		}
		unless( ref($proj) )
		{	die "***ERROR looking up project with pkey=$prj_pkey: "
				. "$proj at ";
			exit;
		}
		my $project = $proj->name;

		next PPLPRJ unless $proj->active || $show_disabled;

		my $rolestr = "";
		my $rolesep = "";
		if ( $is_poc )
		{	$rolestr .="${rolesep}Point-of-Contact";
			$rolesep = ", ";
		}
		if ( $is_coord )
		{	$rolestr .="${rolesep}Coordinator";
			$rolesep = ", ";
		}
		if ( $is_member )
		{	$rolestr .="${rolesep}Member";
			$rolesep = ", ";
		}

		$rolestr = '(no active roles at this time)' unless $rolestr;
		print "$project:	$rolestr\n";
	}
}
		

	
	


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

my $help;
my $usernames = [];
my $cluster;
my $dump_options = { };

my $res = GetOptions(
	'help!' => \$help,
	'verbose+' => \$VERBOSE,
	'cluster=s' => \$cluster,
	'username=s' => $usernames,
	'inactive!' => \$dump_options->{inactive},
	
);

unless ( $res ) 
{	usage;
	die "\nError processing options.\n";
}
if ( $help )
{	usage;
	exit 0;
}
if ( scalar(@ARGV) )
{	if ( scalar(@$usernames) == 0 )
	{	#Allow one to omit --user
		$usernames = [ @ARGV ];
	} else
	{	my $tmp = join ", ", @ARGV;
		usage;
		die "\nError: extraneous 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;

unless ( scalar(@$usernames) )
{	usage;
	die "\nNo usernames specified.\n";
}

USER: foreach my $username (@$usernames)
{	
	dump_user($hpc, $username, $dump_options);

}
