#!/usr/local/bin/perl -w
#	setuprealaudio.pl AUDIODIR RAMDIR LABELDATAFILE copytoserver addmissing foilabstract
#
#	Set up Control Parameters
$copytoserver =1; # If nonzero copy .ra files to server
$addmissing =0; # if non zero fill in null files
$foilabstract=2; # if abstract.ra missing set up from this foil
$copytoserver = $ARGV[3] if(defined($ARGV[3]));
$addmissing = $ARGV[4] if(defined($ARGV[4]));
$foilabstract = $ARGV[5] if(defined($ARGV[5]));
#
$source= $ARGV[0];
opendir(AUDIODIR,$source) || die("\nSorry Could not open ",$source," \n");
#
#	Get MetaData from LABELDATAFILE which is typically talkmeta.html
open(LABELDATAFILE,'<'.$ARGV[2]) ||die("\nSorry Could not open ",$ARGV[2]," \n");
$author='No Author';
$today=`date`;
chomp($today);
$today =~ s/\d\d:\d\d:\d\d ... //;      # Today's Date
$eventdate=$today;
if( $ARGV[2] =~ m%([^/]*)/([^/]*)$% )	{
	$label = $1;
}
else	{
	$label=`pwd`;
}
$label=`pwd` unless $label;
chomp($label);
if( $label =~ m%/([^/]*)$% )   {
	$label = $1;
}
#
while( $line=<LABELDATAFILE> )	{
	chomp($line);
	if($line =~ m%^author:(.*)%i )	{
		$author=$1;
		$author =~ s%<[^<>]*>%%g;
	}
	if( $line =~ m%^eventdate:(.*)%i )  {
		$eventdate = $1;
		$eventdate =~ s%<[^<>]*>%%g;
	}
	if( $line =~ m%^foil(\d*):(.*)%i )	{
		$index=$1;
		$value=$2;
		$value =~ s%<[^<>]*>%%g;
		$value =~ s%'%"%g;
		$value =~ s%\&quot;%"%g;
		$value =~ s%\&amp;%&%g;
		$value =~ s%\&lt;%<%g;
		$value =~ s%\&gt;%>%g;
		$title{$index}=$value;
		$foilmissing{$index}=1;
		$nullaudio{$index}=0;
	}
}
$foilmissing{'abstract'}=1;
$nullaudio{'abstract'}=0;
#
#	First Pass
while( $name=readdir(AUDIODIR))	{
	next if( $name eq '.' || $name eq '..' );
	next unless $name;
	next unless $name =~ m%\.ra$% ;
	unless( $name =~ m%^(\D*)(\d*)\.ra$% )	{
		die("Illegal File name ",$name,"\n");
	}
	$begin=$1;
	$end=$2;
	unless( ($begin eq 'foil') || ($begin eq 'abstract') )	{
		$new='foil' . $2 . '.ra';
		$com= 'mv ' . $source . '/' .$name . ' '. $source .'/'.$new;
		system("$com");
		$name = $new;
	}
	$actualname = $source .'/' . $name;
	$end = 'abstract' if( $begin eq 'abstract' );
	$foilmissing{$end}=0;
	next unless( -e $actualname.'info' );
	open(RAINFO,'<'.$actualname.'info') || die("Sorry Could not open ",$actualname.'info'," \n");
	while( $grab = <RAINFO> )	{
		$nullaudio{$end} = 1 if( $grab =~ m%nullaudio:yes% );
	}
	close(RAINFO);
}
#
#	Create Abstract if Requested and Necessary
if( $foilmissing{'abstract'} == 1 )	{
	if( $foilabstract )	{
		$getfrom = $source.'/foil'.$foilabstract.'.ra';
		unless(-e $getfrom)	{
			print('Cannot Create Abstract from ',$getfrom," As requested\n");
		}
		else	{
			$foilmissing{'abstract'} = 0;
			$nullaudio{'abstract'}=$nullaudio{$foilabstract};
			$com = 'cp '.$getfrom.' '.$source.'/abstract.ra';
			system($com);
		}
	}
}
if($addmissing)	{
	foreach $key (keys %foilmissing)	{
		next unless $foilmissing{$key};
		$nullaudio{$key}=1;
		$foilmissing{$key}=0;
		if( $key eq 'abstract' )	{
			$name = $key;
		}
		else	{
			$name = 'foil'.$key;
		}
		$com='cp ../generalaudio/noaudio.ra ' .$source .'/'.$name. '.ra';
		system($com);
	}
}
#
#	Second pass
closedir(AUDIODIR);
opendir(AUDIODIR,$source) || die("\nSorry Could not open ",$source," \n");
while( $name=readdir(AUDIODIR))	{
	next if( $name eq '.' || $name eq '..' );
	next unless $name;
	next unless $name =~ m%\.ra$% ;
	unless( $name =~ m%^(\D*)(\d*)\.ra$% )	{
		die("Illegal File name ",$name,"\n");
	}
	$begin=$1;
	$end=$2;
	if( $begin eq 'abstract' )	{
		$end='abstract';
		$begin='';
		$title{$end} = 'Abstract from ' . $label unless(exists($title{$end}) );
	}
	$actualname = $source .'/' . $name;
	unless(exists($title{$end}) )	{
		$title{$end} = 'Foil'.$end. ' from ' . $label;
	}
	$com = 'rax -t \''.$title{$end}.'\' -a \''.$author.'\' -c \'Public Domain:' . $eventdate . ' ' . $label . '\'';
	system("$com $actualname");
	$com='rax -i ';
	@info = `$com$actualname`;
	$lengthinfo = @info;
	open(RAINFO,'>'.$actualname.'info') || die("\nSorry Could not open ",$actualname.'info'," \n");
	for($i=0;$i<$lengthinfo;$i++)	{
		$infoline=$info[$i];
		chomp($infoline);
		if( $infoline =~ m%^([^:]*):(.*)$% )	{
			$lclabel=$1;
			$infovalue = $2;
			$lclabel =~ tr/A-Z/a-z/;
			$lclabel =~ s%\s%%g;
			if( $lclabel eq 'length' ) 	{
				if( $infovalue =~ m%([\d\.]*)(\s*)minutes%i )	{
					$min = $1;
				}
				else	{
					$min =0;
				}
				$infovalue =~ m%([\d\.]*)(\s*)seconds%i;
				$sec = $1;
				$sec += 60*$min;
				print RAINFO 'seconds:',$sec,"\n";
			}
			else	{
				print RAINFO $lclabel.':',$infovalue,"\n";
			}
		}
	}
	print RAINFO 'raplace:'.$actualname,"\n";
	print RAINFO 'ramplace:'.$ARGV[1] . '/'.$name.'m',"\n";
	if( $nullaudio{$end} )	{
		print RAINFO "nullaudio:yes\n";
	}
	else	{
		print RAINFO "nullaudio:no\n";
	}
	close(RAINFO);
	open(RAMINFO,'>' . $ARGV[1] . '/'.$name.'m') ||die("\nSorry Could not open ",$ARGV[1] . '/'.$name.'m',"\n");
	print RAMINFO 'pnm://clyde.npac.syr.edu/users/gcf/' . $label .'/audio.ra/'.$name."\n";
	close(RAMINFO);
}
#
#	Copy to Server if Requested
if($copytoserver)	{
	$stem='/project/C422/pnserver/rafiles/users/gcf/';
	mkdir($stem.$label,0755)	unless( -e $stem.$label );
	mkdir($stem.$label.'/audio.ra',0755)	unless( -e $stem.$label.'/audio.ra');
	$com= 'cp '.$source . '/*.ra ' . $stem . $label . "/audio.ra\n";
	system($com);
}
#
1;
