#!/usr/bin/perl
#
# This script is in public domain.
#
# man2html を使って xjman の html ツリーを作る.
#                              2000.3.4 NAKANO Takeo.
#

# definitions
#
$MANROOT = "man/ja_JP.eucJP";
unless (-d $MANROOT) {die "$MANROOT does not exist\n"};

$WWWROOT = "xjman";

$MAN2HTML = "/home/nakano/bin/man2html";
unless (-x $MAN2HTML) {die "$MAN2HTML does not executable\n"};

#
# man2html 出力に対して置換する各リンク (決め打ち(^^;)
#
$MAIN='<A HREF="../../index.html">xjman/web INDEX</A>';
$MAN2HTMLPAGE='man2html';
$heads= << 'EOM';
<LINK REL="stylesheet" TYPE="text/css" HREF="http://xjman.dsl.gr.jp/xjman.css">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-2022-jp">
</HEAD>
<BODY>
EOM

# for debugging purpose:
#
#$MANROOT = "/home/nakano/public_html/JMwww/roff";
#$WWWROOT = "/home/nakano/public_html/JMwww/html";
#$MAN2HTML = "/home/nakano/bin/man2html";

#
# いったん $WWWROOT をお掃除。リンクの関係とかあるので、
# 依存関係を保持するのはめんどくさすぎ。
#
#system("rm -rf $WWWROOT");

#
# $MANROOT をサーチして、 hash に push.
#
print "collecting roff file...\n";
open RL,"find $MANROOT |";
while(<RL>){
#	print;
	chomp;
#	unless(/\/([^\/]*)\/release\/[^\/]*\/([^\/]*)\.([1-9])$/){
	unless(/man[1-8]\/([^\/]*)\.([1-9])x$/){
		next;
	}
	$name=$1;
	$sec=$2;

	$roffs{$name,$sec} = "$name,$sec";
	print "collect: $name.$sec\n"
}
close RL;

#
# 変換開始。
#
print "start converting...\n";

foreach $fname (sort keys %roffs){
    #
    # 各 html ページ作成
    #
    ($name,$sec)=split /,/, $roffs{$fname};
    $secx = $sec . "x";

    $hdir = "$WWWROOT/man$sec";
    $hfile = "$hdir/$name.$secx.html";

    system("mkdir -p $hdir");

    #
    # .so ファイルならシンボリックリンクを作成。
    #
    $grep=`grep "^\.so " $MANROOT/man$sec/$name.$secx`;
    $grep=~s/[\r\n]//g;
    if( $grep=~/\.so man.\/([^\/]*)\.([1-9])x$/){
	$lname = $1;
	$lsec = $2;
	$lsecx = $lsec . "x";
	
	if ($roffs{$lname,$lsec} eq undef){
	    print "WARN: dest $lname.$lsecx for symlink $name.$secx lacks.\n";
	    next;
	}

	if ($lsec ne $sec){
	    system("cd $hdir; ln -s ../man$lsec/$lname.$lsecx.html $name.$secx.html");
	} else {
	    system("cd $hdir; ln -s $lname.$lsecx.html $name.$secx.html");
	}

	print "man$sec/$name.$sec.html: create symlink to $lname.$lsec.html\n";
	next;
    }

    #
    # man2html を使って convert
    #
    print "converting $name.$secx...";
    my $roffpage = "$MANROOT/man$sec/$name.$secx";

    open M2H, "$MAN2HTML $roffpage |";
    open WL,"| nkf -j > $hfile";
    
    while(<M2H>){
	if($. < 2){next;};	
	
	s/\&\#([0-9]+)\;/pack("C",$1)/ge;
	s///ge;

	s/<A HREF=\"http:\/\/localhost\/cgi-bin\/man\/man2html\">Return to Main Contents<\/A>/$MAIN/ge;
	s/<A HREF=\"http:\/\/localhost\/cgi-bin\/man\/man2html\">man2html<\/A>/$MAN2HTMLPAGE/ge;
	s/<\/HEAD><BODY>/$heads/ge;
	
	while (m/<A HREF=\"http:\/\/localhost\/cgi-bin\/man\/man2html\?([1-9])[a-zA-Z]?[1-9a-zA-Z]*\+([^\"]+)/){

	    if ($roffs{$2,$1} eq undef){
		s/<A HREF=\"http:[^>]+>([^<]+)<\/A>/$1/;

	    } else {
		($lname, $lsec) = split /,/, $roffs{$2,$1};

		$lsecx = $lsec . "x";
		$repl = "../man$lsec/$lname.$lsecx.html";

		s/http:\/\/[^\"]+/$repl/;
		
	    };

	    while (m/<A HREF=\"http:\/\/localhost\/cgi-bin\/man\/man2html\?/){
		s/<A HREF=\"http:[^>]+>([^<]+)<\/A>/$1/;
	    };
	} #end of while(m/../).
	print WL;
    }
    close M2H;
    close WL;
    print "done\n";
}

