#!/usr/local/bin/perl5 -w
#	Add latest URL Information to file
#
require "cgi-lib.pl";
$mailprog = '/usr/lib/sendmail';
#
$statusofread =  &ReadParse(*formdata);
$userdir = 'defaultuser';
if( $statusofread && exists($formdata{"username"}) ) {
        $userdir = $formdata{"username"} if( defined($formdata{"username"}) );
}
$testuserdir = $userdir;
$testuserdir =~ tr/A-Z/a-z/;
if( ($testuserdir eq 'guest') || ($testuserdir =~ m%^anon%) )   {
        $userdir = 'defaultuser';
}
#
#	Base URL
$basefordumping = "/usr/local/archives/public/html/users/gcf/nobody/webwisdom/";
#
$today =`date`;
chomp($today);
#
$basefordumping .= $userdir .'/';
#
$filetodumpto = $basefordumping . 'storeurl';
#
$count = 0;
while (!&LockDatabase($filetodumpto)) { # Sleep for a second
        sleep(1);
        ++$count;
        &therewaserror("Server out to lunch -- please email -- lock stuck") if($count > 20);
}
#
$dumperrorflag=0;
if(-e $filetodumpto )	{
	open(FILETODUMPTO,'>>'.$filetodumpto) || ($dumperrorflag=1);
}
else	{
	open(FILETODUMPTO,'>'.$filetodumpto) || ($dumperrorflag=1);
	print FILETODUMPTO "List of URL's Visited\n";
}
if( $dumperrorflag == 1 )	{
	print "Location: http://www.npac.syr.edu/users/gcf/webwisdom/OngoingDumpedError.html\n\n";
	&UnlockDatabase($filetodumpto);
	exit 1;
}
#

print FILETODUMPTO $today," ";
print FILETODUMPTO $ENV{'REMOTE_ADDR'},' ',$ENV{'REMOTE_HOST'},' ';
#
$whattooutput = 'AUDIO:none';
#
if( $statusofread )	{
	print FILETODUMPTO ' MESSAGE:',$formdata{"usermessage"} if(exists($formdata{"usermessage"}) && ($formdata{"usermessage"}));
	print FILETODUMPTO ' WORLD: ',$formdata{"url"};
	if(exists($formdata{"audiodir"}) && ($formdata{"audiodir"}) && exists($formdata{"audiofiletype"}) )	{
		$audiodir = $formdata{"audiodir"};
		$audiofiletype = $formdata{"audiofiletype"};
		getfiledata();
	}
	print FILETODUMPTO  ' ',$whattooutput,"\n";
}
else	{
	$dumperrorflag=1;
	print FILETODUMPTO "Error\n";
}
close(FILETODUMPTO);
#
&UnlockDatabase($filetodumpto);
#
if( $dumperrorflag == 1 )	{
	print "Location: http://www.npac.syr.edu/users/gcf/webwisdom/OngoingDumpedError.html\n\n";
	exit 1;
}
else	{
	print <<EOF;
Content-Type: text/html

<html>
<head>
<title>Set Advanced Dump On Ongoing Basis</title>
<script language="JavaScript">
<!--scriptbegin
if(top.bigboss.advanceddumpingset == 1 )        {
	localmess = 'Timed Log ON';
}
else    {
        if( top.bigboss.advanceddumpoption == 1)        {
		localmess = 'Start in WebWisdom';
	}
	else	{
		localmess = 'SetUp in WebWisdom';
	}
}
//scriptend-->
</script>
</head>
<script language="JavaScript">
<!--scriptbegin
document.writeln('<body background="' + top.bigboss.wisdomhomeurl + '../background/' + top.bigboss.currentparameters.bgfile + '"  >');
document.writeln('<font size=3 color="' + top.bigboss.currentparameters.imgfgcolor + '" >');
document.writeln('<h2>Set Special Dump Options ' + localmess + '</h2>');
//scriptend-->
</script>
<b>$whattooutput</b>
<hr>
</form>
<form name="formtodump" ACTION="http://boss.npac.syr.edu:8080/cgi-bin/webwisdom/ongoingdump.pl" METHOD="post"
ENCTYPE="application/x-www-form-urlencoded"  >
<input type="submit" name="List" value="Record URL!" >
 User Message<input name="usermessage" type="text" value="" >
<script language="JavaScript">
<!--scriptbegin
/* Removed as Security Problem
if(top.bigboss.advanceddumpingset == 1 )        {
        document.writeln('<input type="button" name="EndTimeout" value="End Ongoing Record" onclick="top.bigboss.clearongoingdump();" >');
}
else    {
        if( top.bigboss.advanceddumpoption == 1)        {
                document.writeln('<input type="button" name="SetTimeout" value="Start Ongoing Record" onclick="top.bigboss.startongoingdump();" >');
        }
}
*/
document.writeln('<br><input name="url" type="hidden"  value="' + top.bigboss.dumpedurl  + '" >');
document.writeln('<input name="audiodir" type="hidden" value="' + top.bigboss.audiodir + '" >');
document.writeln('<input name="audiofiletype" type="hidden" value="' + top.bigboss.audiofiletype + '" >');
document.writeln('<input name="audiomonitortime" type="hidden" value="' + top.bigboss.audiomonitortime + '" >');
document.writeln('<input name="username" type="hidden" value ="' + top.bigboss.username + '" >');
document.writeln('</form>');
top.bigboss.setupadvanceddump();
//scriptend-->
</script>
</body>
</html>
EOF
}
sub cgidebug    {

open(DEBUGFILE,'>>/usr/local/archives/public/html/users/gcf/nobody/webwisdom/debug') || return;
foreach $val (@_)	{
	if(defined($val) )	{
		print DEBUGFILE $val,' ';
	}
	else	{
		print DEBUGFILE 'Undefined ';
	}
}
print DEBUGFILE "\n";
close(DEBUGFILE);
return;
}
sub getfiledata     {
#
	$com = 'ls -t ' . $audiodir . '/*' . $audiofiletype;
        @listoffiles = `$com`;
	$wantedfile = $listoffiles[0];
	chomp($wantedfile);
	$whattooutput = `ls -lt $wantedfile`;
	$whattooutput =~ s%^(\S+)(\s+)(\S+)(\s+)%%;
	$whattooutput =~ m%\s+(\d+)\s+(.*)$%;
	$whattooutput = 'AUDIO:'. $1 . ' ' . $2. 'ENDAUDIO';
}
#
sub therewaserror       {

        my($mess) = @_;
#
        print <<EOF;
Content-Type: text/html

<html>
<head>
<title>Error in ongoingdump.pl Processing!</title>
</head>
<h2>Error in Processing Ongoing Dump: $mess</H2>
Please correct or if it's our fault
<A HREF="mailto:gcf\@npac.syr.edu?subject=ongoingdump.pl Lock Error $mess" onMouseOver="window.status=' Sadness!.';return
true">Mail Complaint</A>
<hr>
EOF
        exit;
}
sub LockDatabase {
# PURPOSE: Attempt to "lock" the database by creating a lock file.
# INPUTS:  Database file name
# OUTPUTS: A boolean telling if the lock was successful

        my ($dbfile) = @_;

        if (-e "$dbfile.lock") {  # DB is already locked
                return 0;
        }
        else {
                system("touch $dbfile.lock");  # Create the lock file
                return 1;
        }
}

sub UnlockDatabase {
# PURPOSE: Attempt to "unlock" the database by deleting the lock file.
# INPUTS:  Database file name
# OUTPUTS: None (but should probably check that the unlock was successful)

        my ($dbfile) = @_;
        unless( (unlink("$dbfile.lock")) )      {
                open (MAIL, "|$mailprog gcf") || (&therewaserror("Server Error Can't Mail -- Please email us")) ;
                print MAIL <<EOF;
From:gcf
To:gcf
Subject:Error in unlink
------------------------------------------------------

Please check ongoingdump.pl
EOF
                close(MAIL);
        }
}
#
1;

