#! /usr/bin/perl

@header= ("<html> <body bgcolor=\"aliceblue\"> <font size=4>\n");
@footer= ("\n</font></body></html>\n");

$page = -1;
$co= 0;

while(<>){
    # 置換定義を憶える
    if ($_ =~ /^#define/){
	( $dummy, $target[$co], $replace[$co] ) = split;
	# 引数のある置換定義の引数は，１個だけと仮定。
	if ($target[$co] =~ /\(\)/){
	    $target[$co] =~ s/\(\)/\\(([^)]*)\\)/;
  	    ( $replace1[$co], $replace2[$co] ) = split(/\$1/,$replace[$co]);
	}
	print "#置き換え".$co." : ".$target[$co]." -> ".$replace[$co]."\n";
	$co++;
	next;
    }
    # ヘッダ
    if ($_ =~ /^#header/){
	( $dummy, @header ) = split;
	next;
    }
    # フッタ
    if ($_ =~ /^#footer/){
	( $dummy, @footer ) = split;
	next;
    }
    # タイトル ... index.html の <title></title> に使う
    if ($_ =~ /^#title/){
	( $dummy, @title_arr ) = split;
	$title= join(" ",@title_arr);
	next;
    }
    # コメント行
    if ($_ =~ /^#/){ next; }
    # ページ区切り行
    if ($_ =~ /^---/){
	$page++;
	$filebase= $ARGV;  $filebase =~ s/\.drf//;
	if ($page > 0){
	    print FH join(" ",@footer)."\n";
	    close(FH);
	}
	open FH, ">$filebase$page.html";
	print FH join(" ",@header)."\n";
	next;
    }
    # 置換の実行
    for($i=0; $i<=$#target; $i++){
	if ($replace1[$i] ne ""){
	    $_ =~ s/$target[$i]/$replace1[$i]$1$replace2[$i]/g;
	}else{
	    $_ =~ s/$target[$i]/$replace[$i]/g;
	}
    }
    # 最初のページ区切り以前の内容は画面に出す。
    if ($page<0){
	print;
    }else{
	print FH;
    }
}
print FH join(" ",@footer)."\n";
close(FH);

# ----------------------------- index.html -------------------------------
# open FH, ">index.html";
# for directory listing.
open FH, ">index-.html";
print FH << "END";
<html>
<head>
<title> $title </title>
</head>
<frameset cols=\"440,*\" border=0 BORDER=0 frameborder=0 FRAMESPACING=0>
  <frame src= \"display.html\" name=\"display\">
  <frameset rows=\"20%,*\" border=0 BORDER=0 frameborder=0 FRAMESPACING=0>
    <frame src= \"blank.html\" name=\"pagenum\">
    <frame src= \"${filebase}0.html\" name=\"textframe\">
  </frameset>
</frameset>
</html>
END
close(FH);
# ----------------------------- index.html -------------------------------

