#!/usr/local/bin/perl5
#  Edit a .ra Audio File
#
require "cgi-lib.pl"; # Public Domain library to read form data
#
$statusofread =  &ReadParse(*formdata); # read data in associate array formdata
#
if( $statusofread )     { # If $statusofread nonzero, data read OK
        $rafile = $formdata{"filename"}; # The input ra file
        $radirectory = $formdata{"radirectoryname"}; # The input/output ra directory
	$realaudiodirectory = $formdata{"realaudiodirectory"}; # The input/output directory pointed to by RAM file
}
else    { # Error Found!
	&dataerror('System Error');
}
#
&dataerror('No Audiofile defined')	unless(defined($rafile));
&dataerror('No Audiofile defined')	unless($rafile);
&dataerror('No Audio Directory defined')	unless(defined($radirectory));
&dataerror('No Audio Directory defined')	unless($radirectory);
&dataerror("Illegal $radirectory Audio Directory")	unless(-e $radirectory );
$radirectory .= '/' unless( $radirectory =~ m%/$% );
$absrafile = $radirectory . $rafile;
&dataerror("Illegal $absrafile Real audio Input File")	 unless(-e $absrafile);
&dataerror("Cannot write $absrafile Real audio Input File")   unless(-w $absrafile);
$restored = 'FAILURE';
&dataerror("Restore Error in $absrafile Real audio Input File")   unless(&restorefile($absrafile));
#
if( defined($realaudiodirectory) && ($realaudiodirectory ne '') )       {
        $realaudiodirectory .= '/' unless( $realaudiodirectory =~ m%/$% );
        $absrealaudiofile = $realaudiodirectory . $rafile;
	if( -e $absrealaudiofile )	{
		&dataerror("Cannot write $absrealaudiofile Real audio Input File")   unless(-w $absrealaudiofile);
	}
	else	{
		&dataerror("Cannot write $realaudiodirectory Real audio Directory")	unless(-w $realaudiodirectory);
	}
}
#
if( defined($realaudiodirectory) && ($realaudiodirectory ne '') )	{
	$absrealaudiofile = $realaudiodirectory . $rafile;
	system("cp $absrafile $absrealaudiofile");
}
#
$today =`date`;
chomp($today); # Today's date captured from UNIX
#
print <<EOF; # Generate the HTML responding page starting with HTTP line
Content-Type: text/html

<html>
<head>
<title>Editraframe0.html Modified with Update on File Editing</title>
EOF
#
$source = 'Editraframe0.html';
open(OLDEDIT,$source) || die("\nSorry Could not open ",$source," \n");
$copy =0;
while( $line=<OLDEDIT>) {
        chomp($line);
        print "$line\n" if( $copy );
        if($line eq '<!-- STARTCOPY -->' )      {
                $copy =1;
                print "$line\n";
                next;
        }
        if( $line eq '<!-- STARTMESSAGE -->' )       {
                &printmessage();
                next;
        }
}
exit; # End Program
#
sub printmessage	{ # Print What was done!

	print "<h2>File <font color=red >$rafile</font> Was Restored from $restored</h2>\n";
	return;
}	# End printmessage
#
sub dataerror	{
	my($errmess) = @_;
#
	print <<EOF; # Start the HTML responding page beginning with HTTP line
Content-Type: text/html

<html>
<head>
<title>Data Input Error!</title>
</head>
<body bgcolor="white" >
<h2>There was a Data Input Error in Form -- Sorry</h2>
<h2>Reason was: $errmess</h2>
</body>
</html>
EOF
        exit; # End Program
}
#
sub restorefile	{
	my($file) = @_;
#
	@listoffiles = `ls -t $absrafile.save1 $absrafile.save2 $absrafile.save3`;
	$countoffiles=0;
        foreach $file  (@listoffiles)    {
		chomp($file);
	        next unless $file;
		next unless $file =~ m%\.save\d$%;
		$chompedfiles[$countoffiles] = $file;
		++$countoffiles;
	}
	if( $countoffiles == 0 )	{
		return 0;
	}
	$restored = $chompedfiles[0];
	$com = "cp $restored $absrafile";
	system($com);
	return 1;
}	# End restorefile
