# *********************************************************
# This Procedure is for rewriting the new foil information
# to the foil_log file. 
# *********************************************************
	
	sub recalculate {
		open(FILE1,"<tmp/foil_log");
		$j = 0;
		while (<FILE1>)  {
			@fields = split;
            $foil_url[$n]     = $fields[0];
            $start_time[$n]   = $fields[1];
            $clip_name[$n]    = $fields[2];
            $start_offset[$n] = $fields[3];
            $size[$n]         = $fields[4];
            @url 			  = split(/\//,$foil_url[$n]);
            $foil[$n] 	      = $url[$#url];

            if ( ($fields[2] =~ $source[$j]) && ($j < $loops) ) {
				$total_size = $total_size + $fields[4];
                $j++;
            }
			if ($fields[2] =~ $var[2]) {
				$the_url = $fields[0];
			}
			if ($fields[2] =~ $source[0]) {
				$the_start = $fields[1];
				$the_start2 = $fields[3];
			}
		}
		print "<p><p>";
		close(FILE1);
	}

	sub rewrite {
		open(FILE1,"<tmp/foil_log");
        open(FILE2,">tmp/foil_log2");
		$j = 0;
		while(<FILE1>) {
			@fields = split;
            if ( ($fields[2] =~ $source[$j]) && ($j < $loops) ) {
				if  ($fields[2] =~ $var[0]) {
			print FILE2 "$the_url $the_start $fields[2] $the_start2 $total_size \n";
				}
				$j++;
			}	
			else {
		print FILE2 "$fields[0] $fields[1] $fields[2] $fields[3] $fields[4] \n";
            }
        }
        system("/bin/rm tmp/foil_log");
        system("/bin/mv tmp/foil_log2 tmp/foil_log");
		close(FILE1);
		close(FILE2);
	}
	
# **********************************************************
# This procedure sets up the audio files for playing from
# the web
# **********************************************************

    sub read_foil_log {
        `/bin/rm /project/C422/pnserver/rafiles/users/gcf/tmp/*`;
        `/bin/rm /usr/local/archives/public/users/gcf/01/BossDocRoot/AudioEdit/tmp/*`;
        open(FILE, "<tmp/foil_log");
        $n=1;
        while(<FILE>) {
            @fields = split;
            $foil_url[$n]     = $fields[0];
            $start_time[$n]   = $fields[1];
            $clip_name[$n]    = $fields[2];
            $start_offset[$n] = $fields[3];
	$local_offset[$n] = $start_offset[$n] - &makeseconds($start_time[$n]) + &makeseconds($start_time[1]);
            $size[$n]         = $fields[4];
            @url = split(/\//,$foil_url[$n]);
            $foil[$n] = $url[$#url];
            `/bin/cp tmp/$clip_name[$n] /project/C422/pnserver/rafiles/users/gcf/tmp/.`;
            $RAM_FILE = "/usr/local/archives/public/users/gcf/01/BossDocRoot/AudioEdit/tmp/".$clip_name[$n]."m";
            open(FILEn, ">$RAM_FILE");
            print FILEn "pnm://clyde.npac.syr.edu/users/gcf/tmp/$clip_name[$n]\n";
			close(FILEn);
            $n++;
        }
		system("chmod 755 /usr/local/archives/public/users/gcf/01/BossDocRoot/AudioEdit/tmp/*");
        system("chmod 755 /project/C422/pnserver/rafiles/users/gcf/tmp/*");
		close(FILE);
    }
#
sub getaudiosize	{
	my($raxfile,$File_Size,@times);

        $raxfile   = "tmp/raxfile";
	$total_audio =0;
	$total_audiofiles =0;
	open(RAXFILE,"<$raxfile");
        while(<RAXFILE>) {
            if ( $_ =~ /Length/) {
                @times = split;
                $File_Size = $times[1] * 60 + $times[3];
		$startaudiofile[$total_audiofiles] = $total_audio;
		$lengthaudiofile[$total_audiofiles] = $File_Size;
		++$total_audiofiles;
                $total_audio = $total_audio + $File_Size;
            }
        }
		close(RAXFILE);
}
sub	printaudiosize	{
	my($i);
	print "<center><b>Total Audio Available $total_audio seconds</b></center><br>\n";
	for($i=0;$i<$total_audiofiles;$i++)	{
		print "<center><b>File $i Starts $startaudiofile[$i] Length $lengthaudiofile[$i] seconds</b></center><br>\n";
	}
}
#
    sub generate_output {
        print "<img src = \"http://boss.npac.syr.edu:8080/AudioEdit/rainbow.gif\" height = 5 width = 750><p>";
        print "<h2>The following table displays the current AudioClips formed...</h2><br>";
	&printaudiosize();
        print "<p><center><TABLE BORDER = 5  WIDTH = 65%>";
        print "<TR><TD><b> Foil <TD><b> Old Start time <TD> <b>Start Shift <TD> <b> Start Offset <TD> <b> End Offset <TD><b> Audio-Clip <TD><b>Target Length<TD><b>Actual Length in Secs.";
        for ($i=1;$i<$n;$i++) {
		$end_offset[$i] = $start_offset[$i] + $size[$i];
		$actualsize[$i] = $size[$i];
		$actualsize[$i] = $total_audio - $start_offset[$i] if($end_offset[$i] >$total_audio);
            print "<TR><TD> $foil[$i] <TD> $start_time[$i] <TD> $local_offset[$i] <TD> $start_offset[$i] <TD> $end_offset[$i] <TD><a href=\"http://boss:8080/AudioEdit/tmp/$clip_name[$i]m\">$clip_name[$i]</a> <TD> $size[$i]<TD> $actualsize[$i]";
        }
        print "</TABLE></center><br>";
print "If you need to alter the clips";
print "<font color = red><a href = \"http://boss:8080/cgi-bin/AudioEdit/Alter_Clips\">PRESS HERE...  </a></font><br><p>";
        print "<img src = \"http://boss.npac.syr.edu:8080/AudioEdit/rainbow.gif\" height = 5 width = 750><p>";
	}
#
#	Convert UNIX time into seconds
	sub makeseconds	{
	my($unixtime) =@_;
	my(@splittimes);
	@splittimes = split(':',$unixtime);
	return $splittimes[0]*3600 + $splittimes[1]*60 +$splittimes[2];
}
#
#	Cleanup Clip Name
sub cleanupclipname	{
	my($clipname) = @_;
	my($target_clip);

	$clipname =~ s%\s*%%g;
	unless( $clipname =~ m%Clip% )	{
		if( $clipname =~ m%^\d% )	{
			$clipname = 'Clip_'.$clipname;
		}
		elsif( $clipname =~ m%^(\D+)(\d+)% )	{
			$clipname = $1 . 'Clip_' . $2 . '.ra';
		}
	}
	if( $clipname =~ m%\d$% )	{
		$clipname .= '.ra';
	}
	$target_clip = "tmp/$clipname";
	unless( -e $target_clip )	{
		print "Illegal Target Clip $target_clip\n";
		print "<img src = \"http://boss.npac.syr.edu:8080/AudioEdit/rainbow.gif\" height = 5 width = 750><p>";
		exit;
	}
	return $clipname;
}
