#!/usr/local/bin/jperl
# 
# find2html: find による HTML ファイル名リストを整形する
# 
#     Usage: (cd ~/public_html; find . -name \*.html -print | ~/bin/find2html > contents.html)
#
#     Jan.30,'96. OSHIRO Naoki.
#     [1996/06/13] OSHIRO Naoki. HTML 以外のファイルのタイトル取得
#     [1996/06/14] OSHIRO Naoki. ディレクトリによるソート
#     [1996/08/15] オプションによるコンパクト表示 
#
#     $Log:$
#

#
# ToDo:
#     [1996/10/29] オプションで refdate による更新日付出力に対応
#

require "getopts.pl";
&Getopts('crb:'); # b: bgcolor
$bgcolor=" BGCOLOR=\"$opt_b\"" if $opt_b;

#
#  Read find list.
#
while (<>) {
    chop;
    s/^\.\///;
    ($dir, $file)=('.', $_);
    ($dir, $file)=($`, $1) if (/\/([^\/]+)$/);
    $dir{$dir}.=$file . " ";
}

#
#  Produce contents.
#
$indent="   ";

print "<HTML>\n";
print "<HEAD>\n";
print "<TITLE>Contents</TITLE>\n";
print "</HEAD>\n";

print "<BODY$bgcolor>\n";
print "<H1>\n";
print "<IMG SRC=\"/~oshiro/gif/neco-logo.gif\" ALT=\"\">\n";
print "Contents</H1>\n";
print "<H5>\n";
print "Last update:[19??/??/??]<!-- #REFDATE -->\n";
print "</H5>\n";
print "<HR>\n\n";

print "<UL>\n" if ($opt_c); 
@dir=sort(keys(%dir));
@dir=reverse @dir if ($opt_r);
foreach $dir (@dir) {
    unless ($opt_c) {
	print "<H3>$dir</H3>\n";
	print "<PRE>\n";
    }
    @path=split('/', $dir);
    $dirdepth=$#path;
    print $indent x $dirdepth;
    foreach $file (sort(split(' ', $dir{$dir}))) {
	$href=$file;
	if ($href=~s/\.html$//) {
	    $title=&get_title("$dir/$file");
	} else {
	    $title=&get_title_shellscript("$dir/$file");
	}
	unless ($opt_c) {
	    print $indent x ($dirdepth+1);
	    print "<A HREF=\"$dir/$file\">[$href]</A>\n";
	    print $indent x ($dirdepth+2);
	    print "$title\n";
	} else {
	    print "<LI><A HREF=\"$dir/$file\">$href</A>: $title\n";
	}
    }
    unless ($opt_c) {
	print "</PRE>\n";
	print "<HR>\n";
    }
}
print "</UL>\n" if ($opt_c); 

print "End of Contents...\n";
print "<HR>\n";
print "</BODY>\n";
print "</HTML>\n";

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

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

    return $title;
}

sub get_title_shellscript {
    local($_)=@_;
    local($title);

    open(F, "<$_") || return "";
    while (<F>) {
	last unless (/^\s*[#;%]+/ || /^\s*$/);
	chop;
	if (/[#;%]+\s*[^:]+:\s*/) {
	      $title=$';
	      last;
	}
    }
    close(F);

    return $title;
}

# major-mode: perl
