#!/usr/local/bin/perl
#
#Script to modify 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: Adds a college to HPC projects DB

Usage:
$0 [ --dryrun ] [ --verbose ]  [ --force ] \\
	[ -cluster CLUSTER ] -code CODE \\
	[ -newcode NEWCODE ] \\
	[ -name NEWNAME ] \\
	[ -desc NEWDESC ]

Modifies a row in the college table of the
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).

The following fields can be given.
	CODE: the (current) code for the college to modify.
		E.g. cmns, engr.  REQUIRED
	NEWCODE: the code to change to.
	NEWNAME: the name to change to.
	NEWDESC: the desc to change to.
If any of NEWCODE, NEWNAME, NEWDESC not given, that field
will not be changed.

In dryun mode, the SQL will be printed but not executed.
In verbose mode, the SQL will be printed AND executed.
In force mode, bypass the verification prompt.

EOF
}

sub verify_changes($$)
{	my $old = shift;
	my $changes = shift;

	my $pkey = $old->coll_pkey;
	my $oldcode = $old->code;
	my $oldname = $old->name;
	my $olddesc = $old->description;

	my $notchanging = '(not changing)';
	my $newcode = $changes->{code} || $notchanging;
	my $newname = $changes->{name} || $notchanging;
	my $newdesc = $changes->{description} || $notchanging;

	print <<EOF;
The following update was requested to the college table.
Record: key=$pkey, code=$oldcode
Code: $oldcode => $newcode
Name: $oldname => $newname
Description: $olddesc => $newdesc

Type yes to continue, or ctrl-c to abort:
EOF

	my $ans = <STDIN>;
	chomp $ans;
	return if $ans =~ /^\s*y(es)?\s*$/i;
	die "Aborting at user request.\n";
}

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

my ($help, $verbose, $dryrun, $cluster, $force);
my ( $code, $newcode, $name, $desc );

my $res = GetOptions(
	'help!' => \$help,
	'verbose!' => \$verbose,
	'force!' => \$force,
	'dryrun!' => \$dryrun,
	'cluster=s' => \$cluster,

	'code=s' => \$code,
	'newcode=s' => \$newcode,
	'name=s' => \$name,
	'desc=s' => \$desc,
);

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";
}

#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->dryrun($dryrun);
$hpc->verbose($verbose);

unless ( $code )
{	usage;
	die "\nMissing required parameter 'code' for college.\n";
}
$code = lc $code;
$newcode = lc $newcode if $newcode;

my $coll = Glue::HPCC::Project::College->college_by_code(
	$hpc, $code);
unless ( $coll )
{	die "\nNo college with code $code found in $cluster, aborting.\n";
}
unless ( ref($coll) )
{	die "\nError looking up college $code in $cluster: $coll.  Aborting.\n";
}

my $changes = {};
my $coll2;
if ( $newcode )
{	die "Ignoring attempt to change code $code to itself" 
		if $code eq $newcode; #This could happen due to case
	$coll2 = Glue::HPCC::Project::College->college_by_code(
		$hpc, $newcode);
	if ( $coll2 && ref($coll2) )
	{	die "\nA college with code $newcode already exists.\n";
	}
	$changes->{code} = $newcode;
}

if ( $name )
{	die "Ignoring attempt to change name $name to itself"
		if $name eq $coll->name;
	$coll2 = Glue::HPCC::Project::College->college_by_name(
		$hpc, $name);
	if ( $coll2 && ref($coll2) )
	{	die "\nA college with name $name already exists.\n";
	}
	$changes->{name} = $name;
}

if ( $desc )
{	die "Ignoring attempt to change desc $desc to itself"
		if $desc eq $coll->desc;
	$changes->{description} = $desc;
}

unless ( scalar(%$changes) )
{	usage;
	die "No changes requested, aborting.\n";
}

verify_changes($coll, $changes) unless $force;

my $error = Glue::HPCC::Project::College->pdb_update( $hpc,
	{ coll_pkey => $coll->coll_pkey } , $changes);

if ( $error )
{	die "\nERROR: error updating college: $error\n";
}

$hpc->db_commit;
print "Successfully updated record for college $code\n";
