#!%PERL_PATH%

# HyperNikkiSystem di.cgi
# version 0.0.5
# 2000/6/26 yar-3 <ore@yar-3.net>

# Copyright (C) 2000  yar-3 <ore@yar-3.net>, HyperNikkiSystem Project

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# $Id: di.cgi.in,v 1.7 2000/09/27 14:18:15 kenji Exp $

# 更新時刻通知cgi
# 分散管理を前提にしています

use strict;
use Time::Local;

require './config.ph';


# *.hnfのあるディレクトリ
my $diarydir = $HNS::System::DiaryDir;
my $lastmodified = 0;
my $newestfile = "";

my ($sec, $min, $hour, $mday, $mon, $year,
    $wday, $yday, $isdst) = localtime(time);
$year += 1900;

my $file;
my @a = ();
opendir(DIR, "$diarydir/$year");
while ($file = readdir(DIR)) {
	push (@a, "$diarydir/$year/$file") if $file =~ /^d.*\.hnf$/; 
}
$year = $year - 1;
opendir(DIR, "$diarydir/$year");
while ($file = readdir(DIR)) {
	push (@a, "$diarydir/$year/$file") if $file =~ /^d.*\.hnf$/; 
}
opendir(DIR, "$diarydir");
while ($file = readdir(DIR)) {
	push (@a, "$diarydir/$file") if $file =~ /^d.*\.hnf$/; 
}

sort @a;        # ascii順に並び替え
@a = reverse(@a);       # 逆順に

foreach my $i (@a) {
    my $t = (stat($i))[9];
    #print "$i time:$t\n";
    if ($lastmodified < $t) {
        $lastmodified = $t;
        $newestfile = $i;
    }
}

my $lm = gmtime($lastmodified);
# Wed Oct  6 21:10:18 1999
# $1  $2  $3 $4       $5
$lm =~ /(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/;

print "Content-Type: text/html; charset=EUC-JP\r\n";
# DD Cal YYYY hh:mm:ss Zone
# 24 Aug 1999 13:12:01 JST
# RFCにのっとった形に
my $day = $3;
if ($day < 10) {
    $day = "0" . $day;
}
my $LM = "$1, $day $2 $5 $4 GMT";

print "Last-Modified: $LM\r\n";
print "Author: $HNS::System::Author\r\n" if ($HNS::System::Author);
print "\r\n";

$newestfile =~ s!.*d(\d+)(\d\d)(\d\d)\.hnf!$1/$2/$3!;

print qq(<html>
<head>
);
if ($HNS::System::Author) {
    print qq(<meta name="author" content="$HNS::System::Author">\n);
}
else {
    print qq(<meta name="author" content="$HNS::System::UserMail">\n)
};
print qq(<title>$HNS::System::Title</title>
</head>
<body>
Last-Modified: $LM<br>
Newest Day is $newestfile.
</body>
</html>
);

1;

