#!/usr/local/bin/perl
#
#Removes user as a PoC for a project
#

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::Project;
use Glue::HPCC::Project::PersonProject;
use Glue::HPCC::Project::Log;
use Glue::HPCC::Utils::MailUtils qw(send_template_email);

use Getopt::Long;
use Time::ParseDate;
use POSIX qw(strftime);

#Globals
my @always_cc = (	'payerle@umd.edu',
		);

sub usage()
{	print STDERR <<EOF;
$0: Removes user as a PoC for a project

Usage:
$0 -user USERNAME -project PROJECT -reason REASON \
	[ -cluster CLUSTER ] [ -dryrun ] [ -noemail ] \
	[ --force ]

This script removes USERNAME as a Point-of-Contact (PoC) for
the project named PROJECT in HPC CLUSTER.  CLUSTER will
be defaulted based on what host you are running the command
on.

All modifications will be logged and require a REASON.

Normally, email is sent to all PoCs for the project (including
the old one) informing them of the change.  --noemail will prevent
that.

--dryrun causes actions that would have been taken to just
be printed.

--force allows last PoC to be removed from an active project.

EOF
}

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

my ($help, $dryrun, $verbose);
my ($username, $projname, $reason );
my $do_email = 1;
my $cluster;
my $force;

my $res = GetOptions(
	'help!' => \$help,
	'dryrun!' => \$dryrun,
	'email!' => \$do_email,
	'verbose!' => \$verbose,
	'force!' => \$force,

	'cluster=s' => \$cluster,
	'user=s' => \$username,
	'project=s' => \$projname,
	'reason=s' => \$reason,
);

unless ( $res )
{	usage;
	die "\nError processing arguments\n";
}

if ( $help )
{	usage;
	exit 0;
}

my $tmp;
if (scalar(@ARGV) )
{	usage;
	$tmp = join ', ', @ARGV;
	die "\nUnrecognized 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;

$hpc->verbose($verbose);
$hpc->dryrun($dryrun);
Glue::HPCC::Utils::MailUtils::verbose($verbose);
Glue::HPCC::Utils::MailUtils::dryrun($dryrun);


unless ($username)
{	usage;
	die "\nMissing required parameter 'user'\n";
}

unless ($projname)
{	usage;
	die "\nMissing required parameter 'project'\n";
}
unless ($reason)
{	usage;
	die "\nMissing required parameter 'reason'\n";
}

my $user = Glue::HPCC::Project::Person->person_by_username($hpc, $username);
die "Illegal value $username for user, not found in PDB.\n" unless ( $user );
die "Error looking up $username in PDB: $user.\n" unless ref($user);
my $oldemail = $user->email;
my $oldname = $user->fullname;


my $proj = Glue::HPCC::Project::Project->project_by_name($hpc, $projname);
die "Illegal value $projname for project, not found in PDB.\n" 
	unless $proj;
die "Error looking up project $projname in PDB: $proj.\n" 
	unless ref($proj);

my $perproj = Glue::HPCC::Project::PersonProject->get_project_person_by_names(
	$hpc, $projname, $username);
die "Error looking up user ${username}'s status in $projname: $perproj at "
	if $perproj && ! ref($perproj);
unless ( $perproj && $perproj->is_poc )
{	#No entry for user/project, or have entry but poc flag unset
	#Either way, nothing to do.
	die "User $username is not currently a PoC for project $projname\n" .
		"Nothing to do.\n";
}

my $allpocs = Glue::HPCC::Project::PersonProject->get_pocs_for_project(
	$hpc, $projname);
die "No pocs for project $projname: WTF??? at "
	unless $allpocs;  #$username should be a poc
my @allpocs = grep { $_ ne $username } @$allpocs;
unless ( @allpocs )
{	#You asked to remove the last remaining PoC of project
	#OK for inactive projects, but bad otherwise
	if ( $proj->active )
	{	if ( $force )
		{	warn "Removing last PoC from project $projname.\n";
		} else
		{	die "Refusing to remove last PoC from project $projname.\n" .
				"Use --force if you REALLY want to do this.\n";
		}
	} else
	{	warn "Removing last PoC from inactive project $projname.\n";
	}
}

my $error = $perproj->pdb_update_me($hpc, is_poc => 0 );
die "Error removing user $username as PoC for project $projname: "
	. "$error\n" if $error;
Glue::HPCC::Project::Log->add_log_entry($hpc,
	person => $username,
	project => $projname,
	tablename => 'personproject',
	fieldname => 'is_poc',
	oldval => 1,
	newval => 0,
	reason => $reason,
	description => "Removing user $username as poc of $projname",
	action_code => 'modify-person-project',
);

$hpc->db_commit;
print "PoC removed, committing db\n";

#Send email
my $template = "${cluster}_remove_poc_from_project.tmplate";

#Get all PoCs (including newly added PoC)
my $poc_emails = Glue::HPCC::Project::PersonProject->get_poc_emails_for_project(
	$hpc, $projname) || [];
push @$poc_emails, @always_cc;
push @$poc_emails, $oldemail;

#REmove duplicates
my %temp = map { $_ => undef } @$poc_emails;
$poc_emails = [ keys %temp ];
my $to_str = join ", ", @$poc_emails;

my $macros = 
{	EMAIL => $to_str,
	OLDPOC => $oldname,
	PROJECT => $projname
};

send_template_email($template, $macros) if $do_email;

