#!/usr/local/bin/perl
#
#Script to manually add a log message
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::Log;

use Getopt::Long;
use Time::ParseDate;
use POSIX qw(strftime);
use Glue::HostinfoVars qw($HOSTNAME);


sub usage()
{	print STDERR <<EOF;
$0: Manually add a log entry

Usage:
$0 [ -user USER ] [ -program PROGRAM ] [ -host HOST ] \\
	[ -time TIME ] \\
	-action_code ACODE -reason REASON  -description DESC \\
	[ -project PROJECT ] [ -account ACCOUNT ] [ -cluster CLUSTER ] \\
	[ -person UNAME ] [ -tablename TABLE ] [ -field FIELD ] \\
	[ -oldval OLD ] [ -newval NEW ] 

This will add a new entry to the log table with the specified values.
ACODE: action code, required
REASON: reason (log message), required
DESC: description of change; REQUIRED
USER: user performing the action logged, default is undef (add_log_entry
	will default to current user)
PROGRAM: program making the change, defaults to $0
HOST: host change made on, default to current host
TIME: time of log entry, defaults to current time
PROJECT: project being changed; default is undef
ACCOUNT: account being changed; default is undef
CLUSTER: cluster being changed; default is based on host running on
UNAME: user being changed; default is undef
TABLE: table being changed; default is undef
FIELD: field being changed; default is undef
OLD: value before the change; default is undef
NEW: value after the change; default is undef

USER, PROGRAM, and HOST refer to who made the change (i.e. the sysadmin).

If --dryrun is given, SQL will be displayed but no log entry made

EOF
}

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

my $help;
my $dryrun;

my $user; # = $ENV{USER};
my $program = $0;
my $hostname = $HOSTNAME;
my $time = strftime("%Y-%m-%d %H:%M:%S", localtime);
my ($reason, $action_code, $project, $account, $cluster, $person);
my ($tablename, $fieldname, $oldval, $newval, $description);

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

	'user=s' => \$user,
	'program=s' => \$program,
	'hostname=s' => \$hostname,
	'time=s' => \$time,
	'reason=s' => \$reason,
	'action_code|action-code|acode=s' => \$action_code,
	'project=s' => \$project,
	'account=s' => \$account,
	'cluster=s' => \$cluster,
	'person=s' => \$person,
	'tablename|table-name|tablename=s' => \$tablename,
	'fieldname|field-name|fieldname=s' => \$fieldname,
	'oldval=s' => \$oldval,
	'newval=s' => \$newval,
	'description=s' => \$description,

);

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

unless ( $action_code )
{	usage;
	die "\nError: missing required parm 'action_code'.\n";
}

unless ( $reason )
{	usage;
	die "\nError: missing required parm 'reason'.\n";
}

unless ( $description )
{	usage;
	die "\nError: missing required parm 'description'.\n";
}
#Default cluster if needed
my $hpc = Glue::HPCC::Cluster->new_from_clustername($cluster);
die "\nError instantiating cluster $hpc " unless $hpc && ref($hpc);
$hpc->dryrun($dryrun);

my @args = (	reason => $reason,
		action_code => $action_code,
		username => $user,
		program => $program,
		hostname => $hostname,
		time => $time,
		project => $project,
		account => $account,
		cluster => $cluster,
		person => $person,
		tablename => $tablename,
		fieldname => $fieldname,
		oldval => $oldval,
		newval => $newval,
		description => $description,
);

Glue::HPCC::Project::Log->add_log_entry( $hpc, @args);
$hpc->db_commit;

