#!/usr/bin/perl
#
#	Wrapper for the Pipeline_Configuration Java application.

=pod

=head1 NAME

B<Pipeline_Configuration>

Reads and writes pipeline configuration parameters stored in a database
table.

=head1 SYNOPSIS

Pipeline_Configuration [options ...]

Options:

 -catalog <catalog>               override the configuration catalog
                                  (useful for testing)
 -configuration <configuration>   configuration file
 -description <description>       write-mode: the description to write
 -help                            print this message
 -info                            show all available information for a
                                  parameter
 -parameter <parameter>           the parameter to query
 -pipeline <pipeline>             the pipeline to use when querying for a
                                  parameter
 -quiet                           suppress extraneous output to simplify
                                  parameter value parsing
 -strict                          treat value validation failures as
                                  global errors
 -validation <validation>         write-mode: the validation string to
                                  write
 -value <value>                   write-mode: the value to write
 -write                           enable write-mode for updating or adding
                                  a pipeline configuration


=head1 DESCRIPTION 

The following are examples of using B<Pipeline_Configuration> which
includes shell transcriptions of (1) querying a pipeline's parameter for a
value, (2) getting the full information of a parameter, and (3) writing 
information back to the catalog for the pipeline.

1. Querying a pipeline parameter value:
The result of the execution of will give the value stored in the catalog.

 $ Pipeline_Configuration \
        -configuration /HiRISE/Configuration/EDRgen/EDRgen.conf \
        -pipeline HiColorNorm -parameter Make_Unfiltered_Cube
 
 Returns:

 TRUE


2. Getting all available information from the catalog for a given parameter:
The result of the execution of will give a PVL-formatted string.

 $ Pipeline_Configuration \
        -configuration ~/HiRISE/Configuration/EDRgen/EDRgen.conf \ 
        -pipeline HiColorNorm -parameter Make_Unfiltered_Cube -info

 Returns:

 PIPELINE = HiColorNorm
 PARAMETER = Make_Unfiltered_Cube
 VALUE = TRUE
 VALIDATION_STRING = TRUE|FALSE
 DESCRIPTION = A boolean value to indicate if the HiColorNorm pipeline should 
                create the unfiltered color cube that has not passed through 
                the noise or furrow filtering corrections
 LAST_UPDATE = 2011-05-17 13:51:59.0


3. Writing a value and validation string to the catalog for the given pipeline:
The result of this execution will be an exit code of 0 and no output.

 $ Pipeline_Configuration \
        -configuration ~/HiRISE/Configuration/EDRgen/EDRgen.conf \
        -pipeline HiColorNorm -parameter Make_Unfiltered_Cube -write \
        -value FALSE -validation "TRUE|FALSE|true|false"

Notice that for write operations the "-write" parameter must be given.

=head1	Author

Chris Van Horne, UA/HiROC

=head1	Copyright

Copyright (C) 2003-2012  Arizona Board of Regents on behalf of the
Planetary Image Research Laboratory, Lunar and Planetary Laboratory at
the University of Arizona.

This file is part of the PIRL Java Packages.

The PIRL Java Packages are free software; you can redistribute them
and/or modify them under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

The PIRL Java Packages are distributed in the hope that they will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

=head1	Version

1.7 2012/04/16 07:01:53

=cut

#	CVS ID: Pipeline_Configuration,v 1.7 2012/04/16 07:01:53 castalia Exp
#===============================================================================
$CVS_ID = 'Pipeline_Configuration (1.7 2012/04/16 07:01:53)';

($Command_Name = $0) =~ s|.*/(\S+)$|$1|;

use Getopt::Long qw(:config ignore_case);

my %h= ();

GetOptions(
    \%h,
    "buildtable",
    "catalog=s",
    "configuration=s",
    "description=s",
    "help",
    "info",
    "parameter=s",
    "pipeline=s",
    "quiet",
    "strict",
    "table=s",
    "validation=s",
    "value=s",
    "write",
);

if(!exists($h{"quiet"}))
{
	print "$Command_Name - $CVS_ID\n";	
}

# find all nonlower command-line args and map them to lowercase
for $nonlower (grep { lc($_) ne $_ } keys %h) {
    $lower = lc $nonlower;
    $h{$lower} = delete $h{$nonlower};
}

# join lowercase options with a space-delimiter and quote the values
$cmdline = join " ", map "-$_ \"$h{$_}\"", keys %h;

$APPLICATION = "PIRL.Conductor.Pipeline_Configuration.Pipeline_Configuration";
$PIRL_JAVA_HOME = $ENV{"PIRL_JAVA_HOME"};
$PIRL_JAVA_HOME = "/opt/java"
	if (! $PIRL_JAVA_HOME &&
		-e "/opt/java/PIRL");
$CLASSPATH = $ENV{"CLASSPATH"};

if($PIRL_JAVA_HOME)
{
	if($CLASSPATH)
	{
		$CLASSPATH = "$PIRL_JAVA_HOME:$CLASSPATH"
			unless $CLASSPATH =~ /$PIRL_JAVA_HOME/;
	}
	else
	{
		$CLASSPATH = $PIRL_JAVA_HOME;
	}
	$MySQL_JDBC = "$PIRL_JAVA_HOME/mysql-connector/mysql-connector.jar"
		unless $MySQL_JDBC = $ENV{"MySQL_JDBC"};
	Add_to_CLASSPATH($MySQL_JDBC);
	$PostgreSQL_JDBC = "$PIRL_JAVA_HOME/PosgreSQL/postgresql.jar"
		unless $PostgreSQL_JDBC = $ENV{"PostgreSQL_JDBC"};
	Add_to_CLASSPATH($PostgreSQL_JDBC);
	$COMMONS_CLI = "${PIRL_JAVA_HOME}/commons-cli/commons-cli.jar"
		unless $COMMONS_CLI = $ENV{"COMMONS_CLI"};
	Add_to_CLASSPATH($COMMONS_CLI);
}

exec "java -client -cp ${CLASSPATH} ${APPLICATION} $cmdline";


sub Add_to_CLASSPATH
{
my ($pathname) = @_;

if (-e "$pathname")
	{
	if ($CLASSPATH)
		{
		$CLASSPATH = "$CLASSPATH:$pathname"
			unless "$pathname" =~ /"$pathname"/;
		}
	else
		{
		$CLASSPATH = $pathname;
		}
	}
}
