#!/usr/bin/perl

use Jcode;

$datdir=$ARGV[0]; #新月の dat ディレクトリの位置 ex) ../shingetsu-0.4b1/dat
$str=$ARGV[1]; #スレッドのタイトル ex) "ネタ板"

$str = Jcode->new($str)->h2z->utf8;
$filename = unpack("H*", $str);
$datfile = "$datdir/thread_\U$filename\E.dat";
$str =~ s|[^\w]|'%' . uc(unpack('H2', $&))|eg;
#print "$datfile\n";

$num=0;

open(IN, "$datfile") or die;
@list = <IN>;
close(IN);

print "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n",
	"<html><head>\n",
	"<meta content=\"text/html; charset=EUC-JP\" http-equiv=\"content-type\">\n",
	"<title>$ARGV[1]</title>\n",
	"</head><body>\n";

foreach(@list){
	$dat = @list[$num];
	@thread = split(/<>/, $dat);
	#print "@thread\n";
		foreach(@thread){
			$_ =~ s/body:.*//;
			$_ =~ s/attach:.*//;
			$_ =~ s/filename:.*//;
		}
	#$thread =~ s/body:.*//;
	#$thread =~ s/attach:.*//;
	#$thread =~ s/filename:.*<>//;
	#print "@thread\n";
	$name = @thread[0];
	$url = @thread[1];
	if (@thread[2] =~ /suffix:/){ $suffix = @thread[2]; }
	if (@thread[3] =~ /suffix:/){ $suffix = @thread[3]; }
	if (@thread[4] =~ /suffix:/){ $suffix = @thread[4]; }

	$suffix =~ s/suffix://;

	print "<br><img style=\"\"\n",
		"src=\"http://localhost:8000/thread.cgi/$str/$url/$name.$suffix\"><br>\n";
	#print "http://localhost:8000/ENCODE_URL/$url\n";
	$num = $num +1;
}

print "</body></html>";

