#!/usr/local/bin/perl -w

# addbyelog
#Add to the data inventory files using the event log for source data
# R. Groman

$version = "V1.30 June 22, 1998";

# Modified February 24, 1998.  Fix format of object specification.  Check
#	for "?" character in pages entry and delete any found.  rcg
# Modified May 14, 1997.  Accomodate multiple consecutive tabs in level 0 data record
# Modified November 13, 1997.  Change description for status=collected to specify
# 	done by this routine. rcg
# Modified November 14, 1997. V1.1a Change print to print STDOUT. rcg
# February 9, 1998.  Change location of listgb to /data5
# March 24, 1998.  Add test to avoid Value of <HANDLE> construct can be "0"
#	error message. rcg
# June 22, 1999. V1.30  Replace listgb with list.  rcg

#Assumptions
#	top level of directory tree is location of inventory files
#	an entry in the globec/gb/.objects for the data inventory file is required
#	instrument list translation table is at /data/rgroman/Inventory/instrument.list	

#$topdir="/data/rgroman/Inventory/debug";	#use for debugging
$topdir="/data/rgroman/Inventory";		#this is the real data directory

#$scriptsdir="/data/rgroman/Inventory/scripts/debug";	#use for debugging
$scriptsdir="/data/rgroman/Inventory/scripts";		#this is the real script directory

$inventoryopt= ">" . $scriptsdir . "/inventoryopt";
$instrumenttable="/data/rgroman/Inventory/instrument.list";	
print STDOUT "Adding data to inventory using event log data Version $version\n\n";
print STDOUT "Updating inventory data from $topdir\n\n";
print STDOUT "Instrument table file name is $instrumenttable\n\n";

chdir($topdir) or die "Cannot change to $topdir directory";

#Get cruiseid to work on.
print STDOUT "What event log do you want to read (specify cruiseid, e.g. al9508): ";
$cruiseid = <stdin>;
chop($cruiseid);
#print STDOUT "\n**debug cruiseid = $cruiseid\n";

#Get information on legal equipment strings and place in hash table
&getequiptypehash ($instrumenttable);

#Get project description
&getprojdescription;
#print STDOUT "\n**debug, proj_desc=$proj_desc\n";

#Get chief scientist name
&getchiefsci;
#print STDOUT "\n**debug, chief_sci=$chief_sci\n";

#Get unique list of equipment and si names from event log data
unless (&getelogdata ($cruiseid) ) {exit; };

#print STDOUT ("\n**debug, project=",$project, " start date=", $start_date[0],
#	$start_date[1],$start_date[2],
#	 " end date=",$end_date[0],$end_date[1],$end_date[2], "\n");
if ( ! defined $si{"cruise_report"} ) {
	$si{"cruise_report"} = $chief_sci;
}
if ( ! defined $si{"eventlog"} ) {
	$si{"eventlog"} = $chief_sci;
}
if ( ! defined $si{"alongtrack"} ) {
	$si{"alongtrack"} = $chief_sci;
}

#Verify valid equipment names and correct (and expand) as necessary using %equip
&validateequip;

$filename = $project . ".dat";
#print STDOUT ("\n**debug, project=$project\n");
#print STDOUT ("\tplatform=$platform\n");
#print STDOUT ("\tstart_date[2]=$start_date[2]\n");
#print STDOUT ("\tstart_date[1]=$start_date[1]\n");
#print STDOUT ("\tstart_date[0]=$start_date[0]\n");
#print STDOUT ("\tend_date[2]=$end_date[2]\n");
#print STDOUT ("\tend_date[1]=$end_date[1]\n");
#print STDOUT ("\tend_date[0]=$end_date[0]\n");
#print STDOUT ("\tchief_sci=$chief_sci\n");
#print STDOUT ("\tproj_desc=$proj_desc\n");
#print STDOUT ("\tfilename=$filename\n\n");


#Update level 0 file
&add_to_level_0;

#Output level 1 and 2 data to the inventory file
while (($key, $value) = each %si) {
#	print STDOUT ("\n**debug, calling addinventory-level1 program with project=",$project,
#		"\n\tequipment=", $key, " and siname=", $value, "\n");
	if ($key eq "nd") {next};
	$command=$scriptsdir . "/addinventory-level1";
	@args=($command, $project, $key, $value);
	system (@args) == 0 or die "Exectuting @args \n\tfailed: $?";
	
	$command=$scriptsdir . "/addinventory-level2";
	$description="nd";
	if ($key eq "cruise_report") {
		print STDOUT ( " \nWhat is the cruise report status for ", $project, 
			" (printed on-line, draft, waiting, nd): " );
		$ans=<STDIN>;
		chop ($ans);
		if ($ans eq "on-line" || $ans eq "printed") {
			print STDOUT ("\nHow many pages in the report? ");
			$pages=<STDIN>;
			chop ($pages);
			$pages =~ s/\?//g;
		}
		if ($ans eq "on-line") {
			$data_status="printed";
			$description=$pages . " pages";
			$description =~ s/ /%/g;
			@args=($command, $project, $key, $data_status, $description);
			system (@args) == 0 or 
				die "Executing \n\t@args \n\tfailed: $?";
			$data_status="on-line";
#			$link=lc $project . ".html";
#			$description="<a href=\"" . 		#trouble handling long
#				"http://globec.whoi.edu/globec-dir/" . #strings in csh?
#				"reports/" . $link . "\">on-line</a>";
			$description="on-line";
		}
		elsif ($ans eq "printed") {
			$data_status="printed";
			$description=$pages . " pages";
		}
		elsif ($ans eq "nd") {
			next;
		}
		elsif ($ans eq "draft") {
			$data_status="draft";
			$description="nd";
		}
		elsif ($ans eq "waiting") {
			$data_status="waiting";
			$description="nd";
		}
		else {
			print STDOUT ("\n%%Warning, the cruise report status = ", $ans, 
				' for ', $project, 
				" is not handled. Ignored.\n");
			next;
		}
	}
	elsif ($key eq "eventlog") {
		$data_status="on-line";
		$description="on-line";
	}
	else {
		$data_status="collected";
		$description="Entered by " . $0 . " (" . $version . ")";
	}
	$description =~ s/ /%/g;
	$description =~ s/</[/g;
	$description =~ s/>/]/g;
	@args=($command, $project, $key, $data_status, $description);
	system (@args) == 0 or 
		die "Executing \n\t@args\n\tfailed: $?";
}
print STDOUT "\n\n  Program to update by event log completed for project $project.\n";
exit 0;
		
#-------------------------------------------------------------------------
sub getequiptypehash {
#Get information about legal equipment types 
#Read data from file specified in $_[0]
#Place results in hash table called %equip

my ( @fields, $i);
#print STDOUT "\n**debug, in subroutine getequiptypehash, passed parameter=$_[0]\n";
open (EQUIPLIST, $_[0]) or die "Could not open equipment table file $_[0]";

while (<EQUIPLIST>) {
	chomp;
	if ( index ($_,"#") == 1) {
		next;	#Skip past comment lines
	}
	@fields = ( split  (/\t/, $_) );
#	print STDOUT ("\n**debug, fields=", @fields, "\n");
	if ( $#fields >= 1 ) {
		$fields[0] =~ s/%/ /g;	#Replace pseudo space character with real space
		$equip{$fields[0]} = $fields[1];
		if ( $#fields >= 2) {
			for ( $i=2; $i <= $#fields; $i++) {
				$equip{$fields[0]} = $equip{$fields[0]} . "\t" 
					. $fields[$i];
			}
		}
#	print STDOUT ("\n**debug, fields[0]=", $fields[0], " and equipment{fields[0]}=",
#		$equip{$fields[0]},"\n");
	}
}
#print STDOUT "\n**debug - finished reading in equipment table hash\n";
return 1;
}

#--------------------------------------------------------------------
sub getelogdata {

#Read $_[0] data from event log object, remove duplicate equipment names,
#and place results in $project, @start_date (d,m,y), @end_date (d,m,y),  
#$platform, and hash array @si, where $si{$equip} = $si_name.

my ( $ans, $object, @date );
$project=uc ($_[0]);

#print STDOUT "\n**debug reading $project event log object\n";
#print STDOUT "Is $project the correct project name (y/n)? ";
#$ans = lc (<STDIN>);
#chop($ans);
#if ( $ans ne "y") {
#	print STDOUT "\nReneter project: ";
#	$project = <STDIN>;
#	$project = uc ($project);
#}

$object = "//globec.whoi.edu/globec/gb/eventlogs\\(cruiseid=" . 
	$project . ",year,platform,inst," . 
	"day_local,month_local,si\\)";
#print STDOUT "\n**debug, object = $object";
open (EVENTLOG, "/data5/globec/bin/list -f -n -t -c $object|") or 
	die "Cannot open object $object";
#Read past variable list line
unless (defined ($record=<EVENTLOG>) ) {
	die "Could not read eventlog data for cruise $project.";
}
if (index ($record, "&x") >= 0 ) {
	print STDOUT ("\n\n %%Could not read inventory data for object=\n\t",
		$object, "\n\tRecord=$record\n\tCannot continue.\n\n");
	return 0;
}
$record=<EVENTLOG>;
if (index ($record, "&x") >= 0 ) {
	print STDOUT ("\n\n %%Could not read inventory data for object=\n\t",
		$object, "\n\tRecord=$record\n\tCannot continue.\n\n");
	return 0;
}
if (! defined $record ) {
	print STDOUT ("\n\n %%Could not find any eventlog data for project=$project.",
		"  Cannot continue.\n\n");
	return 0;
}
chomp($record);
#print STDOUT "\n**debug, record=$record\n";
( $start_date[2], $platform, $equip, $start_date[0], $start_date[1], $si_name ) = 
	split(/\t/, $record);
$si_name =~ s/ //g;
$si_name =~ s/\t//g;
&selectsinamefromlist($si_name);
#print STDOUT ("\n**debug, si_name=$si_name\n");
$equip =~ s/ //g;
$equip =~ s/\t//g;
$si{$equip} = $si_name;
while ( defined ($record=<EVENTLOG>) ) {
	chomp($record);
	( $date[2], $platform, $equip, $date[0], $date[1], $si_name ) = 
		split(/\t/, $record);
#	print STDOUT "\n**debug, record=$record\n";
	$equip =~ s/ //g;
	$equip =~ s/\t//g;
	$si_name =~ s/ //g;
	$si_name =~ s/\t//g;
	&selectsinamefromlist($si_name);
	if ( ! defined $si{$equip} ) {
#		print STDOUT ("\n**debug, si_name=$si_name\n");
		&selectsinamefromlist($si_name);
		$si{$equip} = $si_name;
	}
}
close (EVENTLOG);
@end_date=@date;
return 1;
}

#--------------------------------------------------------------------
sub getprojdescription {
#Get project description and put results in $proj_desc.
my (@descriptions, $i, $ans);
$proj_descfile="/data/rgroman/Inventory/project-descriptions";
open (PROJ_DESCFILE, $proj_descfile) or die "Could not open platform file $proj_descfile";
@descriptions= <PROJ_DESCFILE>;
close (PROJ_DESCFILE);
chomp (@descriptions);
print STDOUT "\n Pick the project description by number\n\n";
for ($i=0; $i < $#descriptions; $i++) {
	print STDOUT "   ($i) -- $descriptions[$i] \n";
}
print STDOUT "\nWhat number do you choose? ";
$ans=<STDIN>;
chomp($ans);
$_=$ans;
if ($ans < 0 || $ans > $#descriptions || ! /\d+/ ) {
	print STDOUT "\nYour response ($ans) was outside the range of values.  Reenter number: ";
	$ans=<STDIN>;
	chomp($ans);
}
$proj_desc=$descriptions[$ans];
}

#---------------------------------------------------------------------
sub getchiefsci {
#Get chief scientist name
my ($i, $ans, $j);
$sinamesfile="/data/rgroman/Inventory/sinames";
open (SINAMES, $sinamesfile) or die "Could not open $sinamesfile";
@names=<SINAMES>;
chomp (@names);
$j=0;
until ($chief_sci) {
	$j++;
	die "Better check your spelling or logic in getting chief scientist name" 
		unless $j < 4;
	print STDOUT ("Enter project/cruise chief scientist name (e.g. ",
		"Beardsley or Bob_Beardsley): ");
	$ans=<STDIN>;
	chop ($ans);
	$ans =~ s/ /_/;
	$ans = ucfirst ($ans);
	for ($i=0; $i < $#names; $i++) {
		if (index ($names[$i], $ans)  != -1 ) { 
			$chief_sci=$names[$i];
			return $chief_sci;
		}
	}
}
}	
print STDOUT "\n %%Error in logic.  Reached end of getchiefsci subroutine.\n";
print STDOUT " %%Will return chief scientist name as na.\n";
$chief_sci="nd";
return $chief_sci;
#---------------------------------------------------------------------
sub validateequip {
#Validate the %si hash using %equip hash and expand or delete entries as necessary
my ($entry, $flag, $key, $key1, $name);
foreach $key (keys(%si)) {
	$key1 = $key;
	$key1 =~ s/ //g;
#	print STDOUT "\n**debug, key1=$key1\n";
	if ($key1 eq "nd") {next}
	if (defined $equip{$key1}) {
		$flag = "yes";
#		print STDOUT ("\n**debug, original key=", $key1 );
		foreach $entry (split /\t/, $equip{$key1}) {
#			print STDOUT ("\n\t\twill use key=", $entry, "\n");
			$name=$si{$key};
			if ($key1 eq $entry) { $flag = "no" }
			if (! defined $si{$entry}) {$si{$entry}=$name}
			if ($si{$entry} eq "nd") {$si{$entry}=$name}
#			print STDOUT ("**debug, si{",$entry,"}=");
#				print STDOUT ($si{$entry},"\n");
		}
		if ( $flag eq "yes" ) { delete $si{$key} };
	}
	else {
		print STDOUT "\n %%Invalid equipment type ($key)",  " with si=",
			$si{$key}, ".  Not added to inventory.\n";
		delete $si{$key};
	}
}
}
return 1;

#---------------------------------------------------------------------
sub selectsinamefromlist {
#Convert the passed name $_[0] to a valid name using names from the
#array @names 
#print STDOUT "\n**debug, in selectsinamefromlist, passed value=$_[0]";
my ($i);
#print STDOUT ("\n**debug (from selectsinamefromlist), passed value=", $_[0], "\n");
$_[0] =~ s/ //g;
$_[0] =~ s/\t//g;
if ( $_[0] eq "nd") {return $_[0]} ;	#avoid matching na to real name
for ($i=0; $i < $#names; $i++) {
	if (index (lc $names[$i], lc $_[0])  != -1 ) { 
		$_[0] = $names[$i];
#		print STDOUT ("**debug (from selectsinamefromlist), new _[0]=",$_[0],"\n");
		return $_[0];
	}
}
#print STDOUT "\n%%Could not find valid si name given $_[0]. Will use na.\n";
$_[0] = "nd";
}

#---------------------------------------------------------------------
sub add_to_level_0 {
#Add to level 0 if record does not exist; fix level 1 file name in any case
#Write out data to special file in order to pass data along to other routines

my ($datatop, @existing, $record);
$datatop=$topdir . "/toplevel.dat";
$record = `grep $project $datatop | tr ' ' '\011' `;
$record =~ s/\t\t/\t/g;
@existing=split (/\t/, $record);
chomp (@existing);
#print STDOUT ("**debug, existing=\n\t",@existing);
open(OPT, $inventoryopt) or die "Could not open $inventoryopt file";
if ($#existing == 10 or $#existing == 20) {	#will take 1st record if see 2
	$record=$existing[0] ."\t" . $existing[1] . "\t" . $existing[2] . "\t" .
	$existing[3] . "\t" . $existing[4] . "\t" . $existing[5] . "\t" . 
	$existing[6] . "\t" .
	$existing[7] . "\t" . $existing[8] . "\t" . $existing[9] . "\t" . 
	$filename . "\n";
	print OPT ($record);
	print <<"AAAA";
		\n\tThis project already exists in the inventory.
		\tValues you entered\tExisting values
		\t$project\t\t\t$existing[0]
		\t$platform\t\t$existing[1]
		\t$start_date[2]\t\t\t$existing[2]
		\t$start_date[1]\t\t\t$existing[3]
		\t$start_date[0]\t\t\t$existing[4]
		\t$end_date[2]\t\t\t$existing[5]
		\t$end_date[1]\t\t\t$existing[6]
		\t$end_date[0]\t\t\t$existing[7]
		\t$chief_sci\t\t$existing[8]
		\t$proj_desc\t\t$existing[9]
		\t$filename\t\t$filename
	I will use the values from the second column.\n
AAAA
	close(OPT);
	if ($existing[0] ne $project) {
		print STDOUT ("\n %%The project you entered ($project) does not match",
			" the project as exists\n\tin the inventory ($existing[0]).",
			"\n\n\tDo you want to continue (y or n)? ");
			$ans=<STDIN>;
			chop ($ans);
			$ans = lc $ans;
			if ( $ans ne "y" ) {exit 1};
			$project=$existing[0];		#set to existing value
	}						#for consistent file names
	$command=$scriptsdir . "/updateinv-level0";
	@args=($command);
	system (@args) == 0 or die "Executing @args failed: $?";
}
elsif ($#existing == -1) {
	$record=$project . "\t" . $platform . "\t" . $start_date[2] . "\t" . 
	  $start_date[1] . "\t" . 
	  $start_date[0] . "\t" . $end_date[2] . "\t" . $end_date[1] . "\t" . 
	  $end_date[0] . "\t" . 
	  $chief_sci . "\t" . $proj_desc . "\t" . $filename . "\n";
	$record =~ s/ //g;
	print OPT ($record);
	close (OPT);
	print <<"BBBB";
		\n\tThe $project project is new to the inventory and has been added.
		Platform=$platform
		Start year=$start_date[2]
		Start month=$start_date[1]
		Start day=$start_date[0]
		End year=$end_date[2]
		End month=$end_date[1]
		End day=$end_date[0]
		Chief scientist=$chief_sci
		Project description=$proj_desc
		Level 1 data file name=$filename\n\n
BBBB
	$command=$scriptsdir . "/addinventory-level0";
	@args=($command);
	system (@args) == 0 or die "Executing @args failed: $?";
}
else {
	die "%%Unexpected number of elements ($#existing) found in record. \n\t@existing";
}
return 1;	
}

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