#!/usr/bin/perl

# $rcs = ' $Id: whatsnew.cgi,v 1.4 1998/06/20 07:15:03 fitz Exp $ ' ;

unshift(@INC, "lib");

require 'config.pl';
require 'bib.pl';
require 'web.pl';

# Maximum number of entries to display
$max = 1000;
if ($ENV{PATH_INFO} =~ /.*\/(\d+)/)
{
    $max = $1;
}

print "Content-type: text/html\n\n";

&bib::load("META", "DATE", "TITLE", "AUTHOR")
  || &error("Database error: $bib::error");

foreach $id (sort by_date_then_title keys %bib::db)
{
    next if $bib::db{$id}->{META} =~ /submit|delete/;

    $series .= "$id,";

    if ($bib::db{$id}->{DATE} =~ /^\s*(\d\d\d\d)(\d\d)(\d\d)\s*$/)
    {
        $date = "$2/$3/$1";
    }
    else
    {
        next;
    }

    if ($date ne $olddate)
    {
        $all .= "</UL>\n\n" if $olddate;

        $all .= "<STRONG>$date</STRONG>\n<UL>\n";

        $olddate = $date;
    }

    $all .= " <LI> " . &bib::link_to_id($id);

    last if ++$count > $max;
}

$all .= "<LI> ..." if $count > $max;

$all .= "</UL>" if $olddate;

# Create the HTML page
print &bib::template("basic.html",
                     "TITLE", "What's New",
                     "DATA",
                     &bib::template("whatsnew.html",
                                    "DATA", $all,
                                    "SERIES", $series,
                                    "MAX", $max,));

exit 0;


sub by_date_then_title
{
    if ($bib::db{$b}->{DATE} == $bib::db{$a}->{DATE})
    {
        $bib::a = $a;
        $bib::b = $b;
        return &bib::id_by_title();
    }
    else
    {
        return $bib::db{$b}->{DATE} <=> $bib::db{$a}->{DATE};
    }

    return 0;
}
