#!/usr/local/bin/perl -w
#	SETUPPHOTO  OLDDIRECTORY NEWDIRECTORY SORTORDER
#	initialize outline(.txt) given images in OLDDIRECTORY
#	if SORTORDER non null preserve order in current index; otherwise use order on file
#
#
$today=`date`;
chomp($today);
$today =~ s/\d\d:\d\d:\d\d ... //;      # Today's Date
#
#	Open all the critical files
#
$sourcebasic= $ARGV[0];
$source = $sourcebasic;
$source = '../../../04/hitachi/' . $sourcebasic unless(-e $source);
$source = '../../../05/hitachi/' . $sourcebasic unless(-e $source);
$source = '../../../07/hitachi/' . $sourcebasic unless(-e $source);
$source = '../../../04/rawphotos/' . $sourcebasic unless(-e $source);
$source = '../../../07/rawphotos/' . $sourcebasic unless(-e $source);
opendir(ORG,$source) || die("\nSorry Could not open ",$source," \n");
#
$keeporder=0;
$keeporder= $ARGV[2] if(defined($ARGV[2]));
$newcount =1;
#
if(defined($ARGV[1]))	{
	$processed = '/usr/local/archives/public/html/users/gcf/01/familyphotos/'.$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 "source:rawphotos/$source\n";
	close(EXTRA);
}
#
#	See what files are on original OLDDIRECTORY
#	Identify and convert if necessary original summary(outline) file
$summary='';
$originalindex='';
while( $name=readdir(ORG))	{
	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 = $name;
		$com = "macconv $source/$name";
		system($com);
		next;
	}
	if( ($lcname =~ m%abstract%) || ($lcname =~ m%summary%) )	{
		$summary = $name;
		$com = "macconv $source/$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}=$name;
	$referenced{$namemod}='';
	$newcountlabel{$namemod}=$newcount;
	++$newcount;
}
close(ORG);
unless($summary)	{
	print("No abstract or summary provided for this cassette\n");
	$summfile = $source.'/summary';
	open(SUMM,'>'.$summfile) || die("\nSorry Could not open $summfile \n");
	print SUMM <<EOF;
No Abstract Provided for this Photoset
EOF
	close(SUMM);
}
if($originalindex)	{
	$oldindex = "$source/$originalindex";
	$copy=0;
	$movedindex = $source .'/MOVEDINDX'.$copy;
	while(-e $movedindex)	{
		++$copy;
		$movedindex = $source .'/MOVEDINDX'.$copy;
		last if($copy > 5 );
	}
	$newindex = "$source/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");
	$count =1;
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;
				$originalcount{$vocabmem}=$count;
				++$count;
				next WHILE;
			}
			print NEW $line,"\n";
		}
	}
	close(OLD);
}
else	{
	print("No Original index file on $source\n");
	$oldindex = "$source/index.made";
	open(NEW,'>'.$oldindex) || die("\nSorry Could not open $oldindex \n");
}
foreach $vocabmem (sort { makenumber($a) <=> makenumber($b)} keys %filename)      {
	if( $referenced{$vocabmem} )	{
		print NEW $referenced{$vocabmem},"\n";
	}
	else	{
		if($keeporder != 2)	{
			$vocabmem =~ s%^(\D*)0*%%; }
		print NEW "$vocabmem: No Title Provided for Photo $vocabmem\n";
	}
}
close(NEW);
#
sub makenumber	{
	my($in)=@_;
#
	if( $keeporder == 2 )	{
		return $newcountlabel{$in}; }
	if($keeporder)	{
		if( exists($originalcount{$in}) )	{
			return $originalcount{$in}; }
	}
	$in =~ s%_%%;
	$in =~ s%(\D+)(\0*)%%;
	$in =~ s%^(\0*)%%;
	$in =~ m%^(\d+)%;
	my($numin) = $1;
	return (1000+$numin) if($keeporder);
	return $numin;
}
	
