#!/usr/bin/perl
# ?のあとの文字列から該当日記を表示する。ＣＧＩプログラム。ver 0.98b
# programed by Katsuhiro 'OMO' Konishi 
#
# ?[Date][command]
# [date]は西暦４桁、月日がそれ２桁の合計８桁。
# [command]は、+x-y の書式で、[date]からの差分を示す。
# 
# 例。
# ?19961231
# に対して、日記ヘッダ、1996年12月31日の日記本文、日記フッタを表示。
# 
# ?19961230+3
# に対して、日記ヘッダ、1996年12月30日から三日間の日記、日記フッタを表示。
#
# ?19961230-3
# に対して、日記ヘッダ、1996年12月30日まで三日間の日記、日記フッタを表示。
#
# ?19961230-1+2
# に対して、ヘッダ、1996年12月29日から1997年１月１日までの日記、
# フッタを表示。
# 
# 日記のファイルをどのようなディレクトリにおくのか、
# ヘッダやフッタのファイル名は？など、細かいところはソースを見てください。
#
# まぁ、このスクリプトのメインはなんといっても、２つのサブルーチン
# &dateToDays,&daysToDate なわけです。
# 他には、[command]部分の取り扱いもちょっと変な方法ですが……。
#
# このプログラムは、適当に改変して適当に使っていただいて結構です。
# また、使ってるよ。とか、こう改良した方が良いよ。とか、
# メール頂けると、うれしいです。
# 
# 平成８年１２月２２日(日)小西克博。
# konishi@paikara.polymer.hokudai.ac.jp

require 'jcode.pl';
&jcode::init;
$date = $ENV{"QUERY_STRING"};
#$date = '19961231';
$date =~ s/#.*//;
$url = "";
$days17521001 = &daysToDate(1752,10,1);
@youbi = ("日","月","火","水","木","金","土");


print "Content-type: text/html\n\n";
print "\n";
open(HEAD,"Head");
print <HEAD>;
close(HEAD);
open(NIKKIHEAD,"diaryHeader");
print <NIKKIHEAD>;
close(NIKKIHEAD);

if (length( $date ) < 8){
    print "<h4>wrong date</h4><hr>\n";
 
}else{
    $year  = substr($date,0,4);
    $month = substr($date,4,2);
    $day   = substr($date,6,2);

    $days = &dateToDays($year,$month,$day);

    $cmd = substr($date,8);
    if ( length($cmd) > 0 ){
	$cmd =~ s/(\+|-)/&$1/g;
	#print "$cmd\n";

	@command = sort by_number split("&",$cmd);
	
	$before = $command[0] ;
	$after = pop(@command);
	if ( $before * $after == 0){
	    if ($before != 0){ $before += 1;}
	    if ($after != 0){ $after -= 1;}
	}
    }else{
	$before = 0;
	$after = 0;
    }
    for ($i = $before ; $i <= $after; $i++ ){
	($year,$month,$day) = &daysToDate($days + $i);
	$index = join("/",($year,$month,$day)).".html";

	$color = "#000000";
	$youbiFlag = ($days + $i - $days17501001) % 7;
	if ($youbiFlag == 0){
	    $color = "#FF0000";
	}elsif($youbiFlag == 1){
	    if (isSpecialday($month,$day-1)){
		$color = "#FF0000";
	    }
	}elsif($youbiFlag == 6){
		$color = "#0000FF";
	}
	if (isSpecialday($month,$day)){
	    $color = "#FF0000";
	}
#	print $url."diary.pl?";
#	print "$year$month$day\n";
	print "</font><br>\n";
	$month += 0;
	$day += 0;
	print "<font color = \"$color\">";
	print &by_jis("<strong>$year年$month月$day日($youbi[$youbiFlag])</strong>\n");
	print "</font>";
	print "</p>\n";
#	print "</p><dl><dt><dd>\n";
	$month += 0;
	$day += 0;
	if (-e $index){
	    open(NIKKI,$index);
	    print <NIKKI>;
	    close(NIKKI);
	}else{
	    print &by_jis("該当ファイルがありません。\n");
	}
	print "<hr width=50%>\n";
#	print "</dl>\n<hr width=50%>\n";
    }
#    print "($year,$month,$day)\n<br>";
#    $days = &dateToDays($year,$month,$day);
#    print $days;
    $previous = join ("",&daysToDate($days + $before - 1));
    $next = join ("",&daysToDate($days + $after +1));
    print "<center>\n";
    print "[<a href=\"diary.pl?$previous-5\">";
    print "Previous 5 days</a>]\n";
    print "[<a href=\"diary.pl?$previous\">";
    print "Previous day</a>]\n";
    print "[<a href=\"./\">latest</a>]\n";
    print "[<a href=\"diary.pl?$next\">";
    print "Next day</a>]\n";
    print "[<a href=\"diary.pl?$next+5\">";
    print "Next 5 days</a>]\n";

    $days = &dateToDays($year,$month,$day);
    $ThisMonthDays = &daysOfMonth($year , $month);

    ($pyear,$pmonth,$pday) = &daysToDate($days - $day -1);
    ($tyear,$tmonth,$tday) = &daysToDate($days - $day + 1);
    ($nyear,$nmonth,$nday) = &daysToDate($days + $ThisMonthDays - $day + 1);

    print "<br>\n";
    print "[<a href=\"$url$pyear/$pyear$pmonth.html\">";
    print &by_jis("先月</a>]\n");

    print "[<a href=\"$url$tyear/$tyear$tmonth.html\">";
    print &by_jis("今月</a>]\n");

    print "[<a href=\"$url$nyear/$nyear$nmonth.html\">";
    print &by_jis("来月</a>]<br>\n");

    print "</center>\n";
    print "<hr>\n";
}
open(MAILME,"mailMe");
print <MAILME>;
close(MAILME);
print "<hr>\n";
open(NIKKIFOOT,"diaryFooter");
print <NIKKIFOOT>;
close(NIKKIFOOT);
print "</body></html>\n";




##################################################
sub by_jis{
    local($value) = @_;
    &jcode::convert(*value, 'jis');
    $value;
}
sub by_number{
    $a <=> $b;
}				# 



# 使い方はソースを見てくれ。

sub isSpecialday{
    local($month,$day) = @_;
    $month = sprintf("%.2d", $month);
    $day = sprintf("%.2d",$day);
#    print join("",($month,$day));
    $var = "0101 0115 0211 0320 0429 0503 0504 0505 0720 0915 0923 1010 1103 1123 1223";
    index($var ,join("",($month,$day)) ) >= 0;
}


sub daysOfMonth{
    local( @dom )  = (0,31,28,31,30,31,30,31,31,30,31,30,31,31);
    local($year,$month);
    ($year,$month) = @_;
    if ( (($year % 4 == 0)&&($year % 100 != 0 )) ||( $year % 400 == 0)) {
	$dom[2] = 29;
    }
    $dom[$month];
}
sub daysOfYear{
    local ($year);
    ($year) = @_;
    if ( (($year % 4 == 0)&&($year % 100 != 0 )) ||( $year % 400 == 0)) {
	366;
    }else{
	365;
    }
}
sub daysToDate{
    local($sum,$year,$month,$day);
    ( $sum ) = @_;


    $year = 1;
    while($sum >1641){
	$sum -= 1460;
	$year += 3;
	if ( (($year % 4 == 0)&&($year % 100 != 0 )) ||( $year % 400 == 0)) {
	    $sum--;
	}
	$year += 1;
    }
    while($sum > &daysOfYear($year )){

	$sum -= &daysOfYear($year );
	$year +=1;
    }
#    print "$sum\n";
    $month = 1;
    while( $sum > &daysOfMonth($year , $month )){
	$sum -= &daysOfMonth($year , $month);
	++$month;
    }

    (sprintf("%.2d",$year) ,sprintf("%.2d", $month),sprintf("%.2d",$sum));
}
sub dateToDays{
    local($i,$tyear,$sum,$year,$month,$day);
    ($year,$month,$day) = @_;

    $tyear = $year -1;
    $sum = $tyear * 365 
	+ int($tyear / 4) 
	    - int($tyear / 100) 
		+ int($tyear / 400);
#    print $sum."\n";

    for ($i = 1; $i < $month; $i++){
#	print &daysOfMonth($year , $i )."\n";
	$sum += &daysOfMonth($year , $i);
#	print ",$i,$sum\n";
    }
#    print "$day,$sum\n";
    $sum += $day;

}
