#!/usr/bin/perl

#<TITLE>What's new</TITLE>
#<META name="description" content="A what's new list script, original from Randal L. Schwartz.">
# read: http://www.teleport.com/~merlyn/WebTechniques/col06.html

use File::Find;
use lib "/usr/src/libwww-perl-5.00/lib";
use HTML::Entities;

## begin config

my (@TOP) =			# top level directories
  qw(/www/Sites/Casema/ftp/);
sub PRUNE {			# don't search these dirs
  ## $_[0] is basename, $_[1] is full path
  $_[0] =~ /gfx|\.stamps|jibat/;
# $_[0] =~ /gfx|\.stamps/;  
}
sub IGNORE {			# don't notice these files
  ## $_[0] is basename, $_[1] is full path
  $_[0] =~ /^\.|~$|\.(gif|jpe?g|JPG|au)$|latest.html/;
}
my ($HOWMANY) =			# keep this many new files
  40;
sub TRANSFORM {		# turn path into URL
  local($_) = @_;
  s!/www/Sites/Casema/!!;
  $_;
}
my ($LIST_PRE) =		# prefix list
  "\n";
my ($ITEM_PRE) =		# prefix item
  "";
my ($ITEM_POST) =		# suffix item
  "\n";
my ($LIST_POST) =		# suffix list
  "\n";
## end config

my (%when);			# record of stamps

find (sub {
	return $File::Find::prune = 1
	  if PRUNE $_, $File::Find::name;
	 return if PRUNE $_, $File::Find::name;
	return unless -f;	# only files
	return if IGNORE $_, $File::Find::name;
	$when{$File::Find::name} = (stat _)[9];
      }, @TOP);


#sub numerically { $a <=> $b; }
sub numerically { $when{$a} <=> $when{$b}; }   



#my @names = sort numerically keys %when;
my @names = sort { $when{$b} <=> $when{$a}; } keys %when;
splice(@names, $HOWMANY);	# discard older stuff

my $name;			# shuddup $name
print $LIST_PRE;
for $name (@names) {
  my $url = encode_entities TRANSFORM $name;
  (my $urlname) = $url =~ m!/~jipp/(.+)!;
#	@paged = lookup_title($name);
	($title,$description,$icon,$iconw,$iconh) =  lookup_title($name);
	$date =  scalar localtime $when{$name};
print "<A NAME=$urlname>\n";
# begin table
print "<TABLE WIDTH=100% BORDER=0>";
# begin 2nd table met date etc.
print "<TR><TD><TABLE border=0 CELLSPACING=0 CELLPADDING=3 bgcolor=\"003300\">",
	"<TR><TD COLSPAN=2 VALIGN=bottom>",
	"<FONT FACE=\"Terminal\"><H3 align=left><A HREF=\"$url\">$url</a></H3></
FONT>",
	 "<TR><TD>date: </TD>",
	"<TD>",
	$date," </TD></TR>";
 if ($title ne ""){
        print
                "<TR><TD>title: </TD>",
                "<TD>";
                print $title,
                " </TD></TR><BR>";
 }                                
 if ($description ne ""){
        print
                "<TR><TD>description: </TD>",
                "<TD>",
                $description;
                 " </TD> </TR> ";
 }
 if (defined($stateng{$url})){
        print "<TR><TD>hits this month: </TD><TD>",
                $stateng{$url},
                "</TD></TR> ";
 }
 print "</TABLE></TD>";
# print icon if there is any
 if ($icon){
	print "<TD VALIGN=MIDDLE>","<A HREF=\"$url\">",
		"<IMG SRC=\"$icon\" WIDTH=$iconw HEIGHT=$iconh",
		" BORDER=0 ALIGN=RIGHT ALT=\"goto : $urlname\"></A></TD>";
 }
# end of table
 print "</TABLE><BR>\n";

	
}
print $LIST_POST;

print "hits this month for latest.html: $stateng{'/~jipp/latest.html'}<BR> ";
print "hits this month for my shoe: $stateng{'/~jipp/gfx/schoen.jpg'}";




sub lookup_title{
        local($file) = @_;
        local($intitle, $title,$description,$icon,$iconw,$inconh);
        if (open(IN, "<$file")) {
                $intitle = 0;
                line: while (<IN>) {
                        chop;

                        if((/\<title\>(.*)$/i)) {
                                $intitle = 1;
                                $title = $1;
                        } elsif ($intitle) {
                                $title .= " $_";
                        }
                        if ($intitle && $title =~ s#</title>.*##i) {
                                last line;
                        }
		}
		line: while (<IN>) {
			chop;
			if ( m!<META (name|NAME)=\"(description|DESCRIPTION)\"\s(content|CONTENT)=\"(.*)\">!i ){
				$description = $4;	
			}
			if ( m!<META name=\"icon\"\scontent=\"(.*),(\d*),(\d*)\">!i){
				$icon = $1;	
				$iconw = $2;
				$iconh = $3;
				last line;
			}
		}
        }
        # if there's no title, just return "", let webglimpse write 'No title'.
         if($title eq ""){
                 $title="";
         } else {
		$title=$title;
	}

        return $title,$description,$icon,$iconw,$iconh;
}





sub readstat{
	local($i,$url,$hits);
	open(IN, "<$statfile");
	line: while (<IN>) {
		#chop;
		if (m!.+<A\sHREF=\"http://www\.xs4all.nl(.*)\">.+</A>\s+(\d+).+!){	
			$url = $1;
			$hits = $2;
			$stateng{$url} = $hits;
		}
		$i++;
	}
	return %stateng;
			
			

}

