#!/usr/local/bin/perl
#
#Script to add department to 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 Glue::HPCC::Project::Department;

use Getopt::Long;

sub usage()
{	print STDERR <<EOF;
$0: Adds a department to HPC projects DB

Usage:
$0 [ --dryrun ] [ --verbose ] [ -cluster CLUSTER ] \\
	-code CODE \\
	-name NAME \\
	-college COLLEGE_CODE \\
	-desc DESC

Adds/inserts a new row in the department 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: a short code for the department.  This is
		use in all our command line tools.
		E.g. math, phys
	NAME: Name as will be shown in web page usage
		graphs.  E.g. Math, Physics
	DESC: A longer description of department
	COLLEGE_CODE: the code for the college is part of
All but DESC are REQUIRED.

In dryun mode, the SQL will be printed but not executed.
In verbose mode, the SQL will be printed AND executed.

EOF
}

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

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

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

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

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 department.\n";
}
$code = lc $code;

unless ( $name )
{	usage;
	die "\nMissing required parameter 'name' for department.\n";
}

unless ( $collcode )
{	usage;
	die "\nMissing required parameter 'college' for department.\n";
}
$collcode = lc $collcode;
my $coll = Glue::HPCC::Project::College->college_by_code(
	$hpc, $collcode);
unless ( $coll )
{	usage;
	die "\nNo college with code '$collcode' found in PDB, aborting.\n";
}
unless ( ref($coll) )
{	die "\nError looking up college with code $collcode: $coll.  Aborting\n";
}
my $collid = $coll->coll_pkey;


my $dept = Glue::HPCC::Project::Department->dept_by_code(
	$hpc, $code);
if ( $dept && ref($dept) )
{	die "\nDepartment with code '$code' already exists, aborting.\n";
}
$dept = Glue::HPCC::Project::Department->dept_by_name(
	$hpc, $name);
if ( $dept && ref($dept) )
{	die "\nDepartment with name '$name' already exists, aborting.\n";
}

my $error = Glue::HPCC::Project::Department->pdb_insert( $hpc,
	code => $code,
	name => $name,
	description => $desc,
	college => $collid,
);

if ( $error )
{	die "\nERROR: error inserting department: $error\n";
}

$hpc->db_commit;
print "Successfully inserted record for department $code\n";
