#!/usr/bin/perl

############################################
##                                        ##
##       WebHints ("Popper" Script)       ##
##           by Darryl Burgdorf           ##
##       (e-mail burgdorf@awsd.com)       ##
##                                        ##
##         last modified: 1/18/98         ##
##           copyright (c) 1998           ##
##                                        ##
##    latest version is available from    ##
##        http://awsd.com/scripts/        ##
##                                        ##
############################################

# ASSIGN THE FOLLOWING VARIABLES

$HintsDir = "/usr2/http/asa/webhints/hints";
$NewHintsDir = "/usr2/http/asa/webhints/newhints";

$HintsTitle = "Фраза дня:";

$MailProg = '/usr/sbin/sendmail';
$MailAddress = "asa\@guru.ru";

# DON'T ALTER ANYTHING BELOW THIS LINE

$hint = time;
($mday,$mon,$year) = (localtime($hint))[3,4,5];
$mon = $mon+1;
$year += 1900;
if ($mon < 10) { $mon = "0".$mon; }
if ($mday < 10) { $mday = "0".$mday; }
$filename = "$year"."$mon"."$mday".".txt";

unless (-e "$HintsDir/$filename") {
	if (-e "$NewHintsDir/$filename") {
		open (HINTIN,"$NewHintsDir/$filename");
		@hint = <HINTIN>;
		close (HINTIN);
		unlink ("$NewHintsDir/$filename");
		open (HINTOUT,">$HintsDir/$filename");
		foreach $line (@hint) {
			print HINTOUT $line;
		}
		close (HINTOUT);
	}
	else {
		$oldest = $filename;
		opendir(HINTS,$HintsDir);
		@hints= readdir(HINTS);
		closedir(HINTS);
		foreach $hint (@hints) {
			if ((int($hint) > 0)
			  && (int($hint) < int($oldest))) {
				$oldest = $hint;
			}
		}
		rename ("$HintsDir/$oldest","$HintsDir/$filename");
	}
}

opendir(HINTS,$NewHintsDir);
@hints= readdir(HINTS);
@sortedhints = sort {$b<=>$a} @hints;
closedir(HINTS);
$Count = 0;
foreach $hint (@sortedhints) {
	if ($hint < 1) { last; }
	$Count++;
}

if ($Count < 8) {
	open (MAIL, "|$MailProg -t");
	print MAIL "To: $MailAddress\n";
	print MAIL "From: $MailAddress\n";
	if ($Count == 0) {
		print MAIL "Subject: \"$HintsTitle\" Has Run Out!\n\n";
		print MAIL "\"$HintsTitle\" has no new entries ";
		print MAIL "remaining!  It's *definitely* time to add ";
		print MAIL "a few more!\n\n";
	}
	else {
		print MAIL "Subject: \"$HintsTitle\" is Running Out!\n\n";
		print MAIL "\"$HintsTitle\" has only $Count new entries ";
		print MAIL "remaining!  It's probably about time that ";
		print MAIL "you added a few more!\n\n";
	}
	close (MAIL);
}

exit;

