#!/usr/bin/perl

$ENV{'TZ'}='JST-9';

##############################
#
##############################
require 'jcode.pl';
use CGI;

$query = new CGI;
@list=$query->param();
foreach $tag (@list) {
  $value=$query->param($tag);
  $keyword{$tag}=$value;
}
$num=$keyword{num};
$num=1 if (! $keyword{num});

$mday=3;
$mday=1 if ($keyword{mode} eq 'i');

##############################
#
##############################
($day,$month,$year)=(localtime(time))[3 .. 5];
$month++;
$year+=1900;

##############################
# 登録リスト
##############################
open(LOCK,".lock");
flock(LOCK,2);
seek(LOCK,0,2);
$count=0;
open(IN,"data/diary");
  while(<IN>){
    if (/^date:/) {
      $count++;
    }
    push(@diary,$_) if ($keyword{mode} eq 'list' || $count >= $num && $count < $num+$mday);
  }
close(IN);
close(LOCK);
($date)=grep(/^date:/,@diary);
chop($date);
$date=~s/^date://;

##############################
# 表示
##############################
$par="80%";
$par="100%" if ($keyword{mode} eq 'i');
$par="60%" if ($keyword{mode} eq 'list');
print "Content-type: text/html;Charset=EUC-JP\n\n";

$code=($keyword{mode} eq 'i')?"SHIFT_JIS":"EUC-JP";
$text="<html>
<head>
<meta HTTP-EQUIV=\"content-type\" CONTENT=\"text/html;charset=$code\">
<title>今日のアタクシ</title>
</head>
<body bgcolor=\"#FFFFFF\">
<center>
<h1>今日のアタクシ</h1>
<div align=center>
<font size=-2>劇団代表グッキーあんとくの不定期日誌。いつまで続くか不明。</font>
<br>
<img src=\"logcount.cgi?keta=6&img=sml&diary\" width=54 height=14>
<table width=\"$par\">
";

if ($keyword{mode} eq 'list') {
  $text.=&get_diary(*diary,'list');
} else {
  $text.=&get_diary(*diary);
}

$text.="</table>\n";
$text.="</div>";

if ($keyword{mode} ne 'list') {
  $fur=$num-$mday;
  $old=$num+$mday;

  $mode="";
  $mode="mode=i&amp;" if ($keyword{mode} eq 'i');
  $text.=sprintf("<a href=\"diary.cgi?%snum=%d\">過去</a>\n",$mode,$old) if ($old<$count);
  $text.="<a href=\"diary.cgi?mode=list\">一覧</a>\n" if ($keyword{mode} ne 'i');
  $text.=sprintf("<a href=\"diary.cgi?%snum=%d\">未来</a>\n",$mode,$fur) if ($fur>=1);
}
$text.=&body_end(*keyword);

&jcode'convert(*text,'sjis') if ($keyword{mode} eq 'i');
print $text;

sub get_diary{
  local(*diary,$mode)=@_;
  local($text);
  local($line,$main,$year,$month,$day);
  local($num,$size);
  foreach(@diary){
    chop;
    $line=$_;
    $main=0;
    if ($line =~ /date:/) {
      $line=~s/date://;
      if ($main) {
        $main=0;
        $text.="</tr>\n";
      }
      ($year,$month,$day)=split("_",$line);
      $year-=1988 if ($year>1900);
      $text.="<tr><td";
      $text.=" bgcolor=\"FF8080\"" if ($mode ne 'list');
      $text.=" valign=\"top\">\n";
      $text.=sprintf("<font size=-1>平成%2d年%d月%d日</font>\n",$year,$month,$day);
      $text.="<br>\n" if ($mode ne 'list');
    } elsif ($line=~ /subject:/) {
      $size=($mode eq 'list')?'1':'2';
      $num++;
      $line=~s/subject://;
      $text.="</td><td><a href=\"diary.cgi?num=$num\">" if ($mode eq 'list');
      $text.="<font size=+$size>$line</font>";
      $text.="</a>" if ($mode eq 'list');
      $text.="</tr>\n";
      $main=1;
      $text.="<tr><td>\n";
    } elsif ($mode eq "list") {
    } elsif ($line =~ /^\(.*::http:.*\)$/) {
      my ($n,$url)=split("::",$line);
      $n=~s/\(//;
      $url=~s/\)$//;
      $text.="<a href=\"$url\">$n</a><br>\n";
    } elsif ($line eq "---") {
      $text.="<hr>\n";
    } else {
      $text.="$line<br>\n";
    }
  }
  $text;
}

sub body_end{
  local(*keyword)=@_;
  local($text,$fig);
  $fig=($keyword{mode} eq 'i')?'../linei.gif':'../line.gif';
  $text="<BR>
<img src=\"$fig\" alt=\"---\"><br>
<a href=\"../\"><img src=\"../banner.gif\" border=0 alt=\"ホーム\"></a>
</center>
</body>
</html>
";
  $text;
}
