#!/usr/local/bin/perl
#
# htwhatsnew.pl v1.1  (C) 1996 Iain Lea
# modified 26 Oct 1998 (c) 1998 Jacques Reynes
# modified 05 Feb 1999 (c) 1999 heddy Boubaker
#
# ChangeLog
# 960321 IL  Reversed sorting to show newest documents first
# 981026 JR  Modified to work with Berkeley DB2.
# 990205 HB  Modified to work with HtDig.pm
#
# Produces a HTML 'Whats New' page with custom header and footer.
#
#   Title
#   Descriptions
#   URL
#   Last modification date (in ctime format)
#
# The date is specified as yyyymmdd
#
# Usage: htwhatsnew.pl [options]
#        -h       help
#        -d date  base date [default: $DefDate]
#        -n days  list documents newer than days old [default: $DefDays]
#        -c conf  Config name
#        -F file  HTML footer 
#        -H file  HTML header
#        -o file  HTML generated file
#        -v       verbose

use strict;
no strict 'vars';

use HtDig;
use URI;
require 'timelocal.pl';
require 'getopts.pl';

my $DefOutputFile = 'htwhatsnew.html';
my $DefDays = 7;
my $DefFooter = '';
my $DefHeader = '';
my $DefDate = `date +%Y%m%d`;
chop $DefDate;

my $TmpFile = "/tmp/htwhatsnew.$$";
my $Verbose = 0;
my $NewNum = 0;

### main ###

&Getopts ('d:c:F:hH:n:o:v');

if ($opt_h ne "") {
    print <<EndOfHelp

Produce an HTML 'Whats New' page with custom header & footer for database.

Usage: $0 [options]
  -h       help
  -d date  base date [default: $DefDate]
  -n days  list documents newer than days old [default: $DefDays]
  -c conf  Config name
  -F file  HTML footer 
  -H file  HTML header
  -o file  HTML generated file [default: $DefOutputFile]
  -v       verbose

EndOfHelp
;
    exit 0;
}       
$DefDate       = $opt_d if ($opt_d ne "");
$DefDays       = $opt_n if ($opt_n ne "");
$DefFooter     = $opt_F if ($opt_F ne "");
$DefHeader     = $opt_H if ($opt_H ne "");
$DefOutputFile = $opt_o if ($opt_o ne "");
$Verbose       = 1 if ($opt_v ne "");

$htdig = new HtDig ( 'Config'=>$opt_c, 'Verbose'=>$opt_v ) || die "Pb w/ config \"$opt_c\" - $! -\n";;
$docdb = $htdig->doc_db() || die "Could not open docdb", $htdig->config->doc_db, " - $!\n";

$DefDate =~ /([0-9]{4})([0-9]{2})([0-9]{2})/;
my $When = timelocal (0, 0, 0, $3, $2 - 1, $1 - 1900)- ($DefDays * 86400);
my $NewDate = localtime ($When);

print "Generating 'Whats New' for documents newer than '$NewDate'...\n" if $Verbose;

&ReadDatabase ($docdb, $TmpFile);
&WriteWhatsNew ($TmpFile, $DefOutputFile, $DefHeader, $DefFooter);

exit 0;

#############################################################################
# Subroutines
#

sub write2tmp {
    my $doc = shift;
    if ( $doc && $doc->validity() && $doc->{'TIME'} > $When ) {
        $Line  = $doc->{'TIME'} . "|";
        $Line .= $doc->url . "|"; 
        $Line .= $doc->title . "|"; 
        $Line .= $doc->description . "|";
        $Line .= $doc->size . "\n";
        $doc->print if $Verbose;
        print TMP $Line;
        $NewNum++;
    }
} # end parse();


sub ReadDatabase {
    my ( $docdb, $TmpFile ) = @_;
    print "Reading db $Index to $TmpFile\n" if $Verbose;
    open( TMP, ">$TmpFile" ) || die "Error: $TmpFile - $!\n";
    $docdb->process( \&write2tmp );
    close (TMP);
} # end ReadDatabase();


sub WriteWhatsNew {
    my ($InFile, $OutFile, $Header, $Footer) = @_;
    
    open (URLS, "sort -r $InFile |") || die "Error: $InFile - $!\n";
    open (HTML, ">$OutFile") || die "Error: $OutFile - $!\n";
    
    &PrintBoilerPlate ($Header, 1);
    
    while (<URLS>) {
        chop;
        ($Time, $URL, $Title, $Description, $Size) = split ('\|');
        if ( !$Title ) {
            my $u = URI->new( $URL );
            if ( $u->path =~ /\/([^\/]+)$/ ) {
                $Title = $1;
            } else {
                $Title = $u->path || $URL;
            }
        }
        $Ctime = localtime ($Time);
        if ($Verbose) {
            print <<EOT
Title:       $Title
Description: $Description
URL:         $URL
Modified:    $Ctime

EOT
;
        }
        print HTML <<EOT
<dt> <a href="$URL">$Title</a>
<dd> <em>$URL</em></br>
     Description:&nbsp;<strong>$Description</strong><br>
     Last Modified:&nbsp;$Ctime&nbsp;-&nbsp;$Size bytes
EOT
;
    }
    &PrintBoilerPlate ($Footer, 0);
    
    close (HTML);
    close (URLS);
    
    unlink ($InFile);
} # end WriteWhatsNew();


sub PrintBoilerPlate {
    my ($File, $IsHeader) = @_;
    
    if ($File ne "" && -e $File) { 
        open (FILE, $File) || die "Error: $File - $!\n";
        while (<FILE>) {
            print HTML;
        }
        close (FILE);
    } else {
        if ($IsHeader) {
            print HTML <<EOT
<html>
<head>
<title>Whats New!</title>
</head>
<body>
<hr noshade>
<h1>Whats New!</h1>
<hr>
<h2>Found $NewNum documents newer than '$NewDate'</h2>
 
<dl>
EOT
;
        } else {
            print HTML <<EOT
</dl>

<hr noshade>
</body>
</html>
EOT
;
        }
    }
} # end PrintBoilerPlate();

### htwhatsnew.pl ens here ###
