#!/usr/local/bin/perl -w
#	SETUPHITACHI  OLDDIRECTORY NEWDIRECTORY
#	initialize outline(.txt) given images in OLDDIRECTORY
#	assumed that /usr/local/archives/public/html/ImageFest is basedirectory for original images
#	or if this not so use /usr/local/archives/public/users/gcf/01/familyphotos/rawphotos
#	assumed that /home/A7E/gcf/foils3/hitachi/rawindex is basedirectory for original indices
#	assumed that NEWDIRECTORY is relative to /home/A7E/gcf/foils3/hitachi/
#
$baseimage = '/usr/local/archives/public/html/ImageFest/';
$baseimage1 = '/usr/local/archives/public/users/gcf/01/familyphotos/rawphotos/';
$baseimage2 = '/usr/local/archives/public/users/gcf/04/hitachi/';
$baseimage3 = '/usr/local/archives/public/users/gcf/05/hitachi/';
$baseimage4 = '/usr/local/archives/public/users/gcf/07/hitachi/';
$baseindex = '/home/A7E/gcf/foils3/hitachi/rawindex/';
$basenewdir = '/home/A7E/gcf/foils3/hitachi/';
#
$today=`date`;
chomp($today);
$today =~ s/\d\d:\d\d:\d\d ... //;      # Today's Date
#
#	Open all the critical files
#
unless( defined($ARGV[0]) && defined($ARGV[1]) )	{
	die("Please Supply two valid arguments!\n");
}
$sourceimage = $baseimage . $ARGV[0];
unless(-e $sourceimage)	{ $sourceimage = $baseimage1 . $ARGV[0]; }
unless(-e $sourceimage)	{ $sourceimage = $baseimage2 . $ARGV[0]; }
unless(-e $sourceimage)	{ $sourceimage = $baseimage3 . $ARGV[0]; }
unless(-e $sourceimage)	{ $sourceimage = $baseimage4 . $ARGV[0]; }
opendir(ORGIMAGE,$sourceimage) || die("\nSorry Could not open ",$sourceimage," \n");
$sourceindex = $baseindex . $ARGV[0];
opendir(ORGINDEX,$sourceindex) || die("\nSorry Could not open ",$sourceindex," \n");
#
if(defined($ARGV[1]))	{
	$processed =  $basenewdir . $ARGV[1];
	unless(-e $processed)	{
		mkdir($processed,0755);
	}
	unless(-d $processed)	{
		die("$processed Processed Photos is not a directory\n");
	}
	$extra="$processed/extrainfo";
	open(EXTRA,'>'.$extra) || die("\nSorry Could not open ",$extra," \n");
	print EXTRA "sourceimage:$sourceimage\n";
	print EXTRA "sourceindex:$sourceindex\n";
	close(EXTRA);
}
#
#	See what files are on original OLDDIRECTORY's
#	Identify and convert if necessary original summary(outline) file
$summary='';
$originalindex='';
while( $name=readdir(ORGIMAGE))	{
	next if( $name eq '.' || $name eq '..' );
	next unless $name;
	$lcname = $name;
	$lcname =~ tr/A-Z/a-z/;
	if( ($lcname =~ m%outline%) || ($lcname =~ m%index%) )	{
		$originalindex = $sourceimage . '/' . $name;
		$com = "macconv $sourceimage/$name";
		system($com);
		next;
	}
	if( ($lcname =~ m%abstract%) || ($lcname =~ m%summary%) )	{
		$summary = $sourceimage . '/' . $name;
		$com = "macconv $sourceimage/$name";
		system($com);
		next;
	}
	next unless $name =~ m%^(.*)\.([^\.]*)$%;
	$namemod = $1;
	$tag = $2;
	$tag =~ tr/A-Z/a-z/;
	next unless (($tag eq 'gif') || ($tag eq 'jpg') || ($tag eq 'htm') || ($tag eq 'html') );
	$filename{$namemod} = $sourceimage . '/' . $name;
	$referenced{$namemod}='';
}
close(ORGIMAGE);
while( $name=readdir(ORGINDEX))	{
	next if( $name eq '.' || $name eq '..' );
	next unless $name;
	$lcname = $name;
	$lcname =~ tr/A-Z/a-z/;
	if( ($lcname =~ m%outline%) || ($lcname =~ m%index%) )	{
		$originalindex = $sourceindex . '/' . $name;
		$com = "macconv $sourceindex/$name";
		system($com);
		next;
	}
	if( ($lcname =~ m%abstract%) || ($lcname =~ m%summary%) )	{
		$summary = $sourceindex . '/' . $name;
		$com = "macconv $sourceindex/$name";
		system($com);
		next;
	}
	next unless $name =~ m%^(.*)\.([^\.]*)$%;
	$namemod = $1;
	$tag = $2;
	$tag =~ tr/A-Z/a-z/;
	next unless (($tag eq 'gif') || ($tag eq 'jpg') || ($tag eq 'htm') || ($tag eq 'html') );
	$filename{$namemod} = $sourceindex . '/' . $name;
	$referenced{$namemod}='';
}
close(ORGINDEX);
unless($summary)	{
	print("No abstract or summary provided for this Hitachi Set\n");
	$summary = "$sourceindex/summary";
	open(SUMM,'>'.$summary) || die("\nSorry Could not open $summary \n");
	print SUMM <<EOF;
No Abstract Provided for this Photoset
EOF
	close(SUMM);
}
#
$nameset=0;
$labelset=0;
#
if($originalindex)	{
	$oldindex = $originalindex;
	$sourceindex = $oldindex;
	$sourceindex =~ s%/[^/]*$%%;
	$sourceindex ='.' if($sourceindex eq '');
	$copy=0;
	$movedindex = $sourceindex .'/MOVEDINDX'.$copy;
	while(-e $movedindex)	{
		++$copy;
		$movedindex = $sourceindex .'/MOVEDINDX'.$copy;
		last if($copy > 5 );
	}
	$newindex = "$sourceindex/index.made";
	$com = "cp $oldindex $movedindex;rm $oldindex";
	system($com);
	open(OLD,'<'.$movedindex) || die("\nSorry Could not open $movedindex \n");
	open(NEW,'>'.$newindex) || die("\nSorry Could not open $newindex \n");
WHILE:
	while($line =<OLD> )	{
		chomp($line);
                $colon = index($line,":");
		unless($colon>0)	{
			print NEW $line,"\n";
		}
		else	{
			$possiblekey = substr($line ,0, $colon);
	                $possiblekey =~ s/\s+//g;
			$possiblekey =~ s/^0*//;
	                $possiblekey =~ s/\.(.*)$//;
	                foreach $vocabmem (keys %filename)      {
	                        next unless( $vocabmem =~ m%^(\D*)0*$possiblekey$% );
	                        $referenced{$vocabmem}=$line;
				next WHILE;
			}
			print NEW $line,"\n";
			$nameset = 1	if($possiblekey eq 'name');
			$labelset = 1	if($possiblekey eq 'label');
		}
	}
	close(OLD);
}
else	{
	print("No Original index file on $sourceindex\n");
	$oldindex = "$sourceindex/index.made";
	open(NEW,'>'.$oldindex) || die("\nSorry Could not open $oldindex \n");
}
#
print NEW "label:$ARGV[0]\n"	unless($labelset);
print NEW "name:hitachi/$ARGV[1]\n"    unless($nameset);
#
foreach $vocabmem (sort { makenumber($a) <=> makenumber($b)} keys %filename)      {
	if( $referenced{$vocabmem} )	{
		print NEW $referenced{$vocabmem},"\n";
	}
	else	{
		$vocabmem =~ s%^(\D*)0*%%;
		print NEW "$vocabmem: No Title Provided for Photo $vocabmem\n";
	}
}
close(NEW);
#
sub makenumber	{
	my($in)=@_;
#
	$in =~ s%^(\D*)(\0*)%%;
	$in =~ m%^(\d+)%;
	return $1;
}
