#!/usr/bin/perl
#
#	Create_Pipeline
#
#	Create a Conductor pipeline table set in a MySQL database.
#
#
#	Use:
#
#		Create_Pipeline [-Catalog <catalog>] [-Pipeline] [<catalog>.]<pipeline>
#
#
#	Description:
#
#	A pair of pipeline tables will be created in the database catalog.
#
#	The <pipeline>_Procedures table will contain the following fields:
#
#		Field Name			Field Type
#		------------------	----------------------------------------------
#		Sequence         	FLOAT NOT NULL PRIMARY KEY
#		Description      	TEXT
#		Command_Line     	TEXT NOT NULL
#		Success_Status   	TEXT
#		Success_Message  	TEXT
#		Time_Limit       	TEXT
#		On_Failure       	TEXT
#		Last_Update      	TIMESTAMP
#
#	The <pipeline>_Sources table will contain the following fields:
#
#		Field Name			Field Type
#		------------------	---------------------------------------------
#		Source_Number		INT UNSIGNED NOT NULL AUTO_INCREMENT
#								PRIMARY KEY
#		Source_Pathname		TEXT NOT NULL
#		Source_ID			VARCHAR(64) BINARY
#		Conductor_ID		VARCHAR(64)
#		Status				TEXT
#		Log_Pathname		TEXT
#		Last_Update			TIMESTAMP
#
#
#	Options -
#
#	-Catalog <catalog>
#		The name of the database catalog where the pipeline tables will be
#		created. Aternatively the pipeline name may be qualified with a
#		catalog name in the form "<catalog>.<pipeline>" which take
#		precedence over the -catalog option.
#
#	- Pipeline <pipeline>
#		The name of the pipeline.
#
#
# Exit status:
#
#	Success: 0
#	Failure: 1 - command syntax error
#	         2 - tables creation failed
#
#
# Author:
#
#	Bradford Castalia, UA/PIRL
#
#	PIRL CVS ID: Create_Pipeline,v 1.5 2006/02/01 21:03:47 castalia Exp
#
################################################################################
#
#	Copyright (C) 2005  Arizona Board of Regents on behalf of the Planetary
#	Image Research Laboratory, Lunar and Planetary Laboratory at the University
#	of Arizona.
#
#	This library is free software; you can redistribute it and/or modify it
#	under the terms of the GNU Lesser General Public License, version 2.1, as
#	published by the Free Software Foundation.
#
#	This library is distributed in the hope that it 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 library; if not, write to the Free Software Foundation,
#	Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
#
################################################################################

$CVS_ID = 'Create_Pipeline,v 1.5 2006/02/01 21:03:47 castalia Exp';

($Command_Name = $0) =~ s|.*/(\w+)$|$1|;
print "$Command_Name - $CVS_ID\n";


#	Database client for submitting SQL statements.
@SQL_Command			= ('mysql', '-e');

$Procedures_Fields		= qq(
    Sequence            FLOAT NOT NULL PRIMARY KEY,
    Description         TEXT,
    Command_Line        TEXT NOT NULL,
    Success_Status      TEXT,
    Success_Message     TEXT,
    Time_Limit          TEXT,
    On_Failure          TEXT,
    Last_Update         TIMESTAMP
);

$Sources_Fields			= qq(
    Source_Number       INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    Source_Pathname     TEXT NOT NULL,
    Source_ID           VARCHAR(64) BINARY,
    Conductor_ID        VARCHAR(64),
    Status              TEXT,
    Log_Pathname        TEXT,
    Last_Update         TIMESTAMP
);

#	Exit status values:
$SUCCESS				= 0;
$BAD_SYNTAX				= 1;
$CREATE_FAILED			= 2;

#-------------------------------------------------------------------------------
#	Command line arguments:

while ($option = shift @ARGV)
	{
	if ($option =~ /^-[Pp]/)
		{
		#	Pipeline.
		if (! @ARGV || $ARGV[0] =~ /^-/)
			{
			Missing_Pipeline:
			print STDERR
				"$Command_Name: Missing pipeline name.\n";
			&Usage;
			}
		$Pipeline = shift @ARGV;
		next;
		}
	if ($option =~ /^-[Cc]/)
		{
		#	Database catalog.
		if (! @ARGV || $ARGV[0] =~ /^-/)
			{
			Missing_Catalog:
			print STDERR "$Command_Name: Missing database catalog.\n";
			&Usage;
			}
		$Catalog = shift @ARGV;
		next;
		}
	if ($option =~ /^-/)
		{
		print STDERR "$Command_Name: Unrecognized option - \"$option\"\n";
		&Usage;
		}
	if ($Pipeline)
		{
		print STDERR "$Command_Name: Multiple pipelines specified - ",
			"$Pipeline and $option\n";
		&Usage;
		}
	$Pipeline = $option;
	}

goto Missing_Pipeline
	if (! $Pipeline);

#	Try to split the Pipeline into Catalog and Pipeline.
($catalog, $pipeline) = split (/\./, $Pipeline, 2);
if ($pipeline)
	{
	if ($Catalog &&
		$Catalog ne $catalog)
		{
		print STDERR "$Command_Name: Multiple database catalogs specified - ",
			"$Catalog and $catalog\n";
		&Usage;
		}
	$Catalog  = $catalog;
	$Pipeline = $pipeline;
	}

goto Missing_Catalog
	if (! $Catalog);

$Create_Table_SQL = "CREATE TABLE IF NOT EXISTS $Catalog.$Pipeline";


#	Sources table

print
	"$Command_Name: Creating the ${Catalog}.${Pipeline}_Sources table,\n",
	"  if it doesn't already exist, with fields -",
	$Sources_Fields;

#	Replace NL and tabs and compress down whitespace.
$Sources_Fields =~ tr/\n\t /   /s;

push @SQL_Command, $Create_Table_SQL."_Sources ($Sources_Fields)";
&Database_Operation;


#	Procedures table

print
	"$Command_Name: Creating the ${Catalog}.${Pipeline}_Procedures table,\n",
	"  if it doesn't already exist, with fields -",
	$Procedures_Fields;

#	Replace NL and tabs and compress down whitespace.
$Procedures_Fields =~ tr/\n\t /   /s;

push @SQL_Command, $Create_Table_SQL."_Procedures ($Procedures_Fields)";
&Database_Operation;

exit (0);

#-------------------------------------------------------------------------------

sub Database_Operation {

system @SQL_Command;
if ($? == -1)
	{
	print STDERR
		"$Command_Name: The command could not be executed!\n";
		"@SQL_Command\n";
	exit ($CREATE_FAILED);
	}
if ($status = $? >> 8)
	{
	print STDERR
		"$Command_Name: The command failed - exit status $status.\n",
		"  You may not have the database privileges to do this.\n",
		"@SQL_Command\n";
	exit ($CREATE_FAILED);
	}
pop @SQL_Command;
}


sub Usage {

print STDERR
	"Usage: $Command_Name [-Catalog <catalog>] [-Pipeline] [<catalog>.]<pipeline>\n",
	"  Arguments -\n",
	"    [-Catalog <catalog>]\n",
	"      The name of the database catalog where the pipeline tables will\n",
	"      be created.\n",
	"    [-Pipeline] [<catalog>.]<pipeline>\n",
	"      The name of the pipeline used to create the pipeline tables -\n",
	"      <pipeline>_Procedures and <pipeline>_Sources in the catalog.\n",
	"      The pipeline name may be qualified with a catalog name in the\n",
	"      form <catalog<.<pipeline> in which case the specified catalog\n",
	"      will take precedence over the -catalog option.\n";

exit ($BAD_SYNTAX);
}
