$datafile = 'abbrtb.txt';
$linkfile = 'linktb.txt';

foreach $filename (a..z){
	$title = $filename;
	$title =~ tr/a-z/A-Z/;
	$filename .= '.htm';
	open(FILE, ">$filename");
	printf FILE ("<HTML>\n<HEAD>\n\t<TITLE>'97最新略語辞典～%s～</TITLE>\n</HEAD>\n\n",$title);
	printf FILE ("<BODY BGCOLOR=\"#fffffa\">\n\n");
	printf FILE ("<CENTER>\n");
	close(FILE);
}

open(DATA, $datafile);
while (<DATA>){
	@line = split(/\t/, $_);
	$key = $line[5]; #abbrkey
	$abbrid{$key} = $line[0];
	$abbr{$key} = $line[1];
	$categoryid{$key} = $line[2];
	$letter{$key} = $line[3];
	$description{$key} = $line[4];
	$abbrkey{$key} = $line[5];
	$hypertext{$key} = $line[6];
	$abbrname{$key} = $line[1];

	$abbridindex{$abbrid{$key}} = $abbrkey{$key};
}
close(DATA);

foreach $i (keys %abbrid){
	@tmp = split(/\|/, $hypertext{$i});
	$a = 1;
	for($a=1; $a<$#tmp+1; $a++){
		@tmp2 = split(/ /,$tmp[$a]);
		for($b=0; $b<$#tmp2+1; $b++){
			if($tmp2[$b] =~ /[0-9]+/){
				$href = $abbridindex{shift(@tmp2)};
				$hreffile = substr($href, 0, 1);
				$hreffile =~ tr/A-Z/a-z/;
				$hreffile .= '.htm';
				$tmp[$a] = sprintf("<A HREF=\"%s#%s\">%s</A>",$hreffile, $href, join(' ',@tmp2));
			}
		}
		$a++;
	}
	$hypertext{$i} = join('',@tmp);
}

open(FILE, $linkfile) || die("linktb.txtがあらへんで。\n");

while(<FILE>){
	chop;
	@line = split(/\t/);
	$r_refid = $line[2];
	$r_abbrid = $line[1];
	$hreffile = substr($abbridindex{$r_abbrid}, 0, 1);
	$hreffile =~ tr/A-Z/a-z/;
	$hreffile .= '.htm';
	$refhref = sprintf("<A HREF=\"%s#%s\">%s</A> ",$hreffile, $abbridindex{$r_abbrid}, $abbr{$abbridindex{$r_abbrid}});
	
	$reftext{$abbridindex{$r_refid}} .= $refhref;
}
close(FILE);


foreach $i (sort(keys %abbrkey)){
	$file = substr($abbrkey{$i}, 0, 1);
	$file =~ tr/A-Z/a-z/;
	$file .= '.htm';
	open(FILE, ">>$file");
	printf FILE ("<P><TABLE WIDTH=\"95%\" CELLPADDING=\"3\">\n");
	printf FILE ("<TR><TD BGCOLOR=\"#9adbe0\">\n");
	printf FILE ("<FONT SIZE=\"+1\"><STRONG><A NAME=\"%s\">%s</A></STRONG></FONT>　%s\n",$abbrkey{$i},$abbrname{$i},$letter{$i});
	printf FILE ("</TABLE>\n");
	printf FILE ("<TABLE WIDTH=\"90%\">\n<TR><TD>");
	printf FILE ("%s\n",$hypertext{$i});
	if($reftext{$i} ne ""){
		printf FILE ("<FONT SIZE=\"-1\"><P>　→%s</FONT>\n",$reftext{$i});
	}
	printf FILE ("</TABLE>\n");
	close(FILE);
}

foreach $filename (a..z){
	$filename .= '.htm';
	open(FILE, ">>$filename");
	printf FILE ("<HR NOSHADE>");
	printf FILE ("</CENTER>\n");
	printf FILE ("\n</BODY>\n</HTML>\n");
	close(FILE);
}

