#!/usr/local/bin/jperl
#-*- perl -*-
#----------------------------------------------------------------------
# ץȤ֤Ƥǥ쥯ȥμ
#----------------------------------------------------------------------
$NKF="nkf";
$MKARY="/tmp_mnt/home/tatuo-y/work/sufary/sufary1921/perl/tool/../../mkary/mkary";
$HERE = $0;
$HERE =~ s/[^\/]+$//;
$HERE = "./" if !$HERE;
#----------------------------------------------------------------------
# ǥ쥯ȥɤ߹
#----------------------------------------------------------------------
sub ReadDir {
    my ($dirname) =@_;
    my $dirname2 = $dirname;

    open(LS, "ls -R $dirname |") || die;
    print STDERR "[$dirname] ";
    while (<LS>) {
	chop;
	next if $_ eq "";
	next if (-d $_);		# ǥ쥯ȥξ
	if (/:$/) {
	    s/:$//;
	    $dirname = "$_";
	    print STDERR "\n[$dirname] ";
	    next;
	}
	next unless /$OPT{'mask'}$/;
	&MakeIndex($_, $dirname);
	print STDERR ".";
    }
    print STDERR "\n";
}
#----------------------------------------------------------------------
# ǥåν
#----------------------------------------------------------------------
sub PrintBUF {
    my ($i);

    open(OUTPUT, ">$OPT{output}.ind");

    while (($str, $list) = each %BUF) {
	next if $str eq "";	# 
	print OUTPUT "$str\t$list\n";
    }
    close (OUTPUT);
}
#----------------------------------------------------------------------
# ǥåκ
#----------------------------------------------------------------------
sub MakeIndex {
    my ($filename,$dirname) = @_;

    $filename = "$dirname/$filename";
    $NUM{$filename} = ++$ID;
    open(FD, "$NKF -e $filename | $HERE/spliter |");

    undef %buf_checker;
    while (<FD>) {
	chop;
	next if (defined $buf_checker{$_});
	$buf_checker{$_} = 1;
	$BUF{$_} .= "$ID ";
    }
    close(FD);
}

#----------------------------------------------------------------------
# ץν
#----------------------------------------------------------------------
$OPT{'dir'} = "";
$OPT{'output'} = "MKIND";
$OPT{'mask'} = ".";
# ץ
while ($_ = shift @ARGV) {
    /^\s*$/ && next;
    if (s/^-h//) { $HELP_MODE = 1; next; }
    if (s/^-m//)    { $OPT{'mask'} = shift @ARGV; next; }
    if (s/^-o//)    { $OPT{'output'} = shift @ARGV; next; }
    if (s/^-D//)    { $DEBUG_MODE = 1; next; }
    $OPT{'dir'} = $_;
}
# إפν
if ($HELP_MODE || $OPT{'dir'} eq "") {
    print STDERR "mkind : MaKe INDex files for fsearch\n";
    print STDERR "   usage: mkind (options..) <dir>\n";
    print STDERR " options:\n";
    print STDERR "   -m ...   : sets input file name mask [$OPT{'mask'}]\$\n";
    print STDERR "   -o ...   : sets output file name [$OPT{'output'}]\n";
    print STDERR "   -help    : shows this help\n"; 
    print STDERR " <dir> = [$OPT{'dir'}]\n";
    exit(1);
}

if (!(-r $OPT{'dir'})) {
    print STDERR "cannot read [$OPT{dir}]\n";
    exit(1);
}

$ID = -1;
#----------------------------------------------------------------------
# 
#----------------------------------------------------------------------
&ReadDir($OPT{'dir'});
&PrintBUF;
close(LS);
open(XNUM, ">$OPT{output}.num");
while ( ($key,$num) = each %NUM ) {
    print XNUM "$key\t$num\n";
}
close(XNUM);
`$MKARY -D $OPT{output}.ind`
