#!/usr/local/bin/perl
#-*- perl -*-
#######################################################################
# Index Maker for IISA
#                                   Copyright (C) 1998, Takuya NAKAYAMA
#######################################################################
$NKF="/usr/local/bin/nkf";
$MKARY="/home/nltools/bin/mkary";
#----------------------------------------------------------------------
# ץȤ֤Ƥǥ쥯ȥμ
#----------------------------------------------------------------------
$HERE = $0;
$HERE =~ s/[^\/]+$//;
$HERE =~ s/\/+$//;
$HERE = "." if !$HERE;
#----------------------------------------------------------------------
# ǥ쥯ȥɤ߹
#----------------------------------------------------------------------
sub read_dir0 {
    my $dname = shift;

    print STDERR "[$dname] ";

    opendir LS, $dname;
    my @files = grep !/^\.+$/, readdir LS;
    closedir(LS);

    my ($f,$count,$fname);
    $count = 0;
    foreach $f (@files) {
	$fname = "$dname/$f";
	if (-d $fname) {	# ǥ쥯ȥξ
	    push(@DIRs, $fname);
	} elsif (-f $fname) {
	    next unless $f =~ /$OPT{'mask'}$/;
	    &MakeIndex($f, $dname);
	}
	print STDERR '.' if $count++ % 50 == 0;
    }
    print STDERR "\n";
}

sub ReadDir {
    @DIRs = @_;

    while ($#DIRs >= 0) {
	my $name = pop(@DIRs);
	&read_dir0($name);
    }
}
#----------------------------------------------------------------------
# ǥåκ
#----------------------------------------------------------------------
sub MakeIndex {
    my ($filename,$dirname) = @_;

    $filename = "$dirname/$filename";
    $FNAME[++$ID] = $filename;
    open(FD, "$NKF -me $filename | $OPT{parser} |");
#    open(FD, "cat $filename | $OPT{parser} |");

    undef %buf_checker;
    my %tmp;
    while (<FD>) {
	chop; next if $_ eq '';
	print OUTPUT "$ID\t$_\n";
    }
    close(FD);
}

#----------------------------------------------------------------------
# ץν
#----------------------------------------------------------------------
$OPT{'dir'} = "";
$OPT{'output'} = "MKIND";
$OPT{'parser'} = "$HERE/splitter";
$OPT{'linemode'} = 0;
$OPT{'mask'} = ".";
# ץ
while ($_ = shift @ARGV) {
    /^\s*$/ && next;
    if (s/^-h//) { $HELP_MODE = 1; next; }
    if (s/^-m//)    { $OPT{'mask'} = shift @ARGV; next; }
    if (s/^-p//)    { $OPT{'parser'} = shift @ARGV; next; }
    if (s/^-o//)    { $OPT{'output'} = shift @ARGV; next; }
    if (s/^-l//)    { $OPT{'linemode'} = 1; next; }
    if (s/^-D//)    { $DEBUG_MODE = 1; next; }
    push(@{$OPT{'dir'}},$_);
}
# إפν
if ($HELP_MODE || $#{$OPT{'dir'}} < 0) {
    print STDERR
	"mkind : MaKe INDex files for IISA\n",
	"   usage: mkind (options..) <dir>\n",
	" options:\n",
	"   -m ...   : set input file name mask [$OPT{'mask'}]\$\n",
	"   -o ...   : set output file name [$OPT{'output'}]\n",
	"   -p ...   : set parser [$OPT{'parser'}]\n",
	"   -l       : output .ary for prefix search [$OPT{'linemode'}]\n",
	"   -help    : show this help\n",
	" <dir> = [@{$OPT{'dir'}}]\n";
    exit(1);
}

if ($OPT{linemode}) {
    open(OUTPUT, "| $HERE/mkind0 | $HERE/indc -l -S $OPT{output}");
} else {
    open(OUTPUT, "| $HERE/mkind0 | $HERE/indc -S $OPT{output}");
}
#open(OUTPUT, "> /dev/null");
$ID = -1;
foreach $dir (@{$OPT{dir}}) {
    if (!(-r $dir)) {
	print STDERR "cannot read [$dir]\n";
	next;
    }
    # 
    &ReadDir($dir);
}
close(OUTPUT);

#----------
open(XNUM, ">$OPT{output}.num");
print XNUM "#LAST_ID=$ID\n";
for ($i = 0; $i <= $#FNAME; $i++) {
    print XNUM "$FNAME[$i]\t$i\n";
}
close(XNUM);
#----------
#`$MKARY -D $OPT{output}.ind`
`$MKARY -so $OPT{output}.ix`
