#!/usr/bin/perl
# = --------------------------------------------------------------------------
# This worked to delete a file and directory from the tp site 25/01/2001

use CGI::Carp qw(fatalsToBrowser);
srand;
$logname = "access.";

#my(
#		$year, 
#		$mon, 
#		$day, 
#		$hour, 
#		$min,
#           $final, 
#		$sec,
#		$logname
#);

$doc_root = "temp";
$download = "../../../db/optimism/whodown.dat";


my ($cwd, $proname, $scrname, $propath);
	use Cwd;

		($cwd = cwd() ) =~ s#\\#/#g;
		($proname = $0) =~ s#\\#/#g;
		($scrname = $proname) =~ s/.*\/\Z?//i;
		($propath = $proname) =~ s/$scrname//i;
		if (-e "$cwd/$scrname") {
			$path = "$cwd/";
		} elsif (-e "$propath$scrname") {
			$path = "$propath";	
		} else {
			print "Content-type: text/html\n\n"; 
			print "<H1>Error: can't get path</H1>\n";
		}
		$path =~ s#/+#/#g;

	
		my $n_bref = $doc_root =~ s/\.\.\///g;
		my @get_path = split /\//, $path;
	
		for (my $i = 0; $i < $n_bref; $i++) {
			pop @get_path;
		}
	
	my $new_path;
	
	foreach (@get_path) {
		$new_path .= "$_/";
	}
	
	$doc_root .= "/" if $doc_root;
	$path = "../../temp";
# > ---------------------------------------------------

	($daynyear,$year,$mon,$day,$hour,$min,$sec) 
	= (localtime(time))[7,5,4,3,2,1,0];
	
	$mon++;
	
	$year = $year + 1900;
	
	if ($mon > 12) { #* Ops we got an error here.
		$mon = 0;
	}
	
	$date = sprintf(qq/%04d%02d%02d/,$year,$mon,$day);
	$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
      $final = "$logname$time";
	set_exp_time();
# > -----------------------------------------------------------------------
# Set expiry time for temp-id.
# -------------------------------------------------------------------------
sub set_exp_time {
	my(
		$hour, 
		$min, 
		$add_min, 
		$exp_hour
	);
	
	($hour, $min, $_ ) = split(/:/, $time);
	$add_min = $min + 60;
	
		if ($add_min > 59) {
			$exp_hour = $hour;
			$exp_hour++;	
		}
		
	$exp_time = qq/$date$exp_hour$min/;
	$ref_time = qq/$date$hour$min/;
}


 open(DOWNLOAD, "$download") || die error1();
		print "Content-type: text/html\n\n";	
		while(<DOWNLOAD>) {
		chomp;		
		@download = split(/\n/);
		
		foreach $line (@download) {
			($id_time, $id, $filename,) = split(/::/, $line);
			$lines++;
			$dir = "/$id/";
			$file = "$path$dir$filename";
#			print "$id_time $filename $ref_time<br>";
#			print "$path$dir$filename<br>";


			if ("$id_time" < "$ref_time") {
			-e $file and unlink $file;
			print "deleted $file<br>";
			$finaldir = "$path/$id";
			-e $finaldir and rmdir $finaldir;		

			# rmdir $final || die error3();
			
				


			}
		 
		
		}
		
	}
close DOWNLOAD;
sub error1 {
	print <<END_of_html;
Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
	<TITLE>Download record file not found</TITLE>
</HEAD>
<BODY>
<H1>Download record file not found</H1>
$!.
</BODY>
</HTML>
END_of_html
}

sub error2 {
	print <<END_of_html;
Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
	<TITLE>Could not delete download file</TITLE>
</HEAD>
<BODY>
<H1>Could not delete download file</H1>
$!.
</BODY>
</HTML>
END_of_html
}

sub error3 {
	print <<END_of_html;
Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
	<TITLE>Could not delete download directory</TITLE>
</HEAD>
<BODY>
<H1>Could not delete download directory</H1>
$!.
</BODY>
</HTML>
END_of_html
}
# chmod 225, $download;

	print <<END_of_html;
Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
	<TITLE>Download record file not found</TITLE>
</HEAD>
<BODY>
<H1>finished: all expired files and directories deleted</H1>
$!.
</BODY>
</HTML>
END_of_html

# -----------------------------------------------------------------------------
#     print "Content-type: text/html\n\n";
#	print $final;
#	exit;
#* EOF