#!/usr/bin/perl
use Date::Manip;

#enable multi-line matching
$* = 1;

$e_count = 0;
$is_body = 0;
$is_comment = 0;
$convert_breaks = 0;
$seemore = 1;

%category_dir = (
	'PC一般' => 'pc',
	'ウェブログ環境構築' => 'sys', 
	'十日市場駅周辺' => 'Tokaichiba',
	'将棋' => 'shogi',
	'社会' => 'soc',
	'雑記' => 'misc'
);

%href_change = (
	'http://ynomura.dip.jp/java/' => 'http://www.sam.hi-ho.ne.jp/~ynomura/java/',
	'http://ynomura.dip.jp/archives/images/' => 'http://www.sam.hi-ho.ne.jp/~ynomura/images/',
	'http://ynomura.dip.jp/archives/misc/' => 'http://www.sam.hi-ho.ne.jp/~ynomura/misc/',
	'/java/' => '/~ynomura/java/',
	'/archives/images/' => '/~ynomura/images/',
	'/archives/misc/' => '/~ynomura/misc/',
);


while(<>){
	if ($is_body){
		if (/^-----$/){
			if ($convert_breaks){
				chomp $body[$e_count];
				$body[$e_count] =~ s#\n\n+#</p><p>#g;
				$body[$e_count] =~ s/\n/<br>/g;
				$body[$e_count] = "<p>" . $body[$e_count] . "</p>";
			}

			$is_body = 0;
			next;
		}else{
			if (/<a / || /<img /){
				foreach $li (keys %href_change){
					s/href="$li/href="$href_change{$li}/g;
					s/href=$li/href=$href_change{$li}/g;
					s/src="$li/src="$href_change{$li}/g;
					s/src=$li/src=$href_change{$li}/g;
				}
			}
			$body[$e_count] .= $_;
		}
	}
	elsif ($is_comment){
		if (/^-----$/){
			$comment_body =~ s/\n//mg;
			if ($comment_url){
				$comment_body .= "<a href=\"$comment_url\">[link]</a>";
			}
			$comment[$e_count] .= "name: $comment_author\n" .
			"url: $comment_url\n" .
			"date: $comment_date\n" .
			"title: $comment_title\n" .
			"comment: $comment_body\n" .
			"excerpt: \n" .
			"blog_name: $comment_blogname\n" .
			"ip: $comment_ip\n" .
			"-----\n";
			$comment_author = $comment_url = $comment_date = $comment_title = $comment_body = $comment_blogname = $comment_ip = '';
			$is_comment = 0;
			next;
		}elsif (/^DATE:\s+(.+)$/){
			$comment_date = $1;
		}elsif (/^AUTHOR:\s+(.*)$/){
			$comment_author = $1;
		}elsif (/^EMAIL:\s+(.*)$/){
			$comment_email = $1;
		}elsif (/^IP:\s+(.*)$/){
			$comment_ip = $1;
		}elsif (/^URL:\s+(.*)$/){
			$comment_url = $1;
		}elsif (/^TITLE:\s+(.*)$/){
			$comment_title = $1;
		}elsif (/^BLOG NAME:\s+(.*)$/){
			$comment_blogname = $1;
		}else{
			$comment_body .= $_;
		}
	}else{
		$title[++$e_count] = $1, $is_comment = 0 if /^TITLE:\s+(.+)$/;
		$is_comment = 1 if /^COMMENT:$/;
		$is_comment = 1 if /^PING:$/;
		$category[$e_count] = $1 if /^CATEGORY:\s+(.+)$/;
		$date[$e_count] = $1 if /^DATE:\s+(.+)$/;
		$is_body = 1 if /^BODY:$/ || /^EXTENDED BODY:$/;
		$body[$e_count] .= "\f" if /^EXTENDED BODY:$/ && $seemore;
		$convert_breaks = $1 if /CONVERT BREAKS: (.+)$/;
	}
}


for($i=1; $i<=$e_count; $i++){
	next if length($body[$i]) < 2;
	$dir = $category_dir{$category[$i]} || $category[$i] || ".";

	$date = ParseDate($date[$i]);
#	$secs = UnixDate($date, "%s");
#	$date2 = DateCalc("Jan 1, 1970  00:00:00 GMT","+ $secs");
#	print "dir=$dir date=$date secs=$secs date2=$date2\n";

	system("mkdir -p $dir");
	open(F, ">$dir/$i.txt") && do{
		print F "$title[$i]\n\n$body[$i]";
		close F;
		$date =~ s/://;
		$date =~ s/:/./;
		system("touch -t $date $dir/$i.txt");
	};
}

for($i=1; $i<=$e_count; $i++){
	next if length($comment[$i]) < 2;
	$dir = $category_dir{$category[$i]} || $category[$i] || ".";

	system("mkdir -p writeback/$dir");
	open(F, ">writeback/$dir/$i.wb") && do{
		print F $comment[$i];
		close F;
	};
}
