#!/usr/bin/perl

use	Jcode;

open(NKF,"|/usr/bin/nkf");
select(NKF);

$base_url = 'http://www.zukeran.org/shin/funifuni/';

print <<"__EOF__";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<link rev="MADE" href="mailto:shin\&#64;opus.or.jp">
<link rel="INDEX" href="/shin/index.html">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-2022-JP">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" type="text/css" href="/shin/css/zu.css">
<LINK REL="SHORTCUT ICON" HREF="http://www.zukeran.org/shin/favicon.ico">
<title>shin's page / funifuni</title>
</head>
<body>
<!--#include file="header.html"-->

__EOF__

while(<>){
	if (/^\\month\{(.*)\}/) {
		$current_month = $1;
		$yyyy = sprintf("%04d",substr($current_month,0,4));
		$mm = sprintf("%02d",substr($current_month,4,2));
		&do_month;
	}elsif (/^\\day\{(.*)\}/) {
		$current_day = $1;
		$dd = sprintf("%02d",$current_day);
		&do_day;
	}elsif (/^\\topic\{(.*)\}/) {
		$current_topic++;
		$tt = sprintf("%02d",$current_topic);
		&do_topic($1);
	}elsif (/^(.*)\\isbn\{([0-9\-A-Z]+)\}(.*)$/) {
		print "$1";
		&do_isbn($2);
		$_ = $3;
		redo;
	}elsif (/^([^[]*)\[\[([^ \]]+?) ([^\]]+)\]\](.*)$/) {
		print "$1<a href=\"$2\">$3</a>";
		$_ = $4;
		redo;
	}elsif (/^([^[]*)\[\[([^\]]+?)\]\](.*)$/) {
		print "$1";
		do_wiki($2);
		$_ = $3;
		redo;
	}elsif (/^$/) {
		next;
	}elsif (/^\n$/) {
		next;
	}else{
		print $_;
	}
}
print <<"__EOF__";
<!--#include file="footer.html"-->
</body>
</html>
__EOF__

exit 0;

sub	do_month	{

print <<"__EOF__";
<h1>$yyyy年$mm月のふにふにとしています</h1>
__EOF__
}

sub	do_day		{
	$current_topic = 0;

print <<"__EOF__";
<hr><!-- ======================================================== -->
<h2><a name="$dd" href="$base_url$yyyy$mm\.html\#$dd" title="$yyyy$mm$dd">$yyyy\/$mm\/$dd</a></h2>
__EOF__
}

sub	do_topic	{
	my($topic) = @_;

print <<"__EOF__";

<h3><a name="$dd\_$tt" href="$base_utl$yyyy$mm.html\#$dd\_$tt" title="$yyyy$mm$dd\_$tt">$topic</a></h3>
__EOF__
}

sub	do_isbn	{
	my($isbn) = @_;
	my($isbn2) = $isbn;

	$isbn2 =~ s/-//g;

	print "[ISBN$isbn:";
	print "<a href=\"http://www.amazon.co.jp/exec/obidos/ASIN/$isbn2/\">amazon</a>:";
	print "<a href=\"http://www.bk1.co.jp/cgi-bin/srch/srch_result_book.cgi?idx=3&amp;isbn=$isbn\">bk1</a>";
	print "]";
}

sub	do_wiki {
	my ($wikiname) = shift;

	$url = Jcode->new($wikiname)->euc;
	
	$url =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;

	print "<a href=\"http://wiliki.zukeran.org/cgi-bin/wiliki.cgi?$url\">";
	print "$wikiname";
	print "</a>";
}
