#!/usr/local/bin/perl

# 
# find2link: find による HTML ファイルのリストからリンクリストを生成する．
# 
#     [1998/11/04] OSHIRO Naoki.
# 
#     $Log:$
#

require "jcode.pl";
$jcode='euc';

print "# FORHTML: リンクリスト,リンクリスト\n";
print "---\n";
print "リンクリスト\n";
while (<>) {
    chop;
    $file=$_;
    $title=get_title($_);
    $title=~s/\n/ /g;
    ($null,$title)=&basename($file) if ($title=~/^index(.html?)?$|^\s*$/);
    print "  http:$file\n    $title\n";
}
print "---\n";

##################################################
#

sub basename {
    local($_)=@_;
    local($path, $base);

    s#/$##;
    ($base, $path)=('', $_);
    if (m#[^/]+$#) {
	$base=$&;
	$path=$`;
    }
    $path=~s#/$##;
    return ($base, $path);
}

sub get_title {
    local($file)=@_;
    local($title);

    open(F, "<$file") || return "";
    while (<F>) {
	chop;
	&jcode'convert(*_, $jcode) if $jcode;
	if (/<TITLE>/i) {
	    $title=$';
	    last;
	}
    }
    if ($title=~s/<\/TITLE>.*//i) {
	close(F);
	return $title;
    }
    while (<F>) {
	chop;
        &jcode'convert(*_, $jcode) if $jcode;
	if (/<\/TITLE>/i) {
	    $title.=$`;
	    last;
	}
	$title.=$_;
    }
    close(F);

    return $title;
}
