#!/usr/local/bin/jperl
#-*- perl -*-
BEGIN {
    $lib = "/tmp_mnt/home/tatuo-y/work/sufary/sufary1921/perl/tool/../SUFARY";
    unshift(@INC, $lib, "$lib/blib/lib", "$lib/blib/arch");
}

$HERE = $0;
$HERE =~ s/\/[^\/]+$//;
$HERE = "./" if !$HERE;

use SUFARY;

$OPT{'fname'} = "MKIND";
$key = "";

while ($_ = shift @ARGV) {
    /^\s*$/ && next;
    if (s/^-h//)    { $HELP_MODE = 1; next; }
    if (s/^-f//)    { $OPT{'fname'} = shift @ARGV; next; }
    $key = $_;
}

if ($HELP_MODE || $OPT{'fname'} eq ""|| $key eq "") {
    print STDERR "fsearch : File SEARCH\n";
    print STDERR "   usage: fsearch (options..) <key>\n";
    print STDERR " options:\n";
    print STDERR "   -f ...   : sets index file name [$OPT{fname}]\n"; 
    print STDERR "   -help    : shows this help\n"; 
    print STDERR " <key> = [$key]\n";
    exit(1);
}

open (KEY_SPL, "echo $key | $HERE/spliter|");
while (<KEY_SPL>) {
    chomp;
    push(@keys, $_);
}
close (KEY_SPL);

open(XNUM, "$OPT{fname}.num") || die;
while (<XNUM>) {
    chomp;
    @e = split(/\t/);
    $FILE_LOC[$e[1]] = $e[0];
}
close(XNUM);

$suf = SUFARY->new("$OPT{fname}.ind");

undef @LINKS;
foreach $key (@keys) {
    $num = $suf->search($key);
    $result = $suf->get_all_line();
    
    undef %checker;
    foreach $x (@$result) {
	$x =~ /\t/; $link_code = $';
	@links = split(/ /, $link_code);
	foreach $i (@links) {
	    next if (defined $checker{$i});
	    $checker{$i} = 1;
	    $LINKS[$i]++;
	}
    }
}
print join(' & ', @keys), "\n";
@keys2 = split(/,/, $key);
$key = join('|',@keys2);
$key = "\'$key\'";
for ($i = 0; $i <= $#LINKS; $i++) {
	next if ($LINKS[$i] <= $#keys);
	print "========  $FILE_LOC[$i]($i)\n";
#	print `nkf -e $FILE_LOC[$i] | grep $key`;
    }				# 
print "========= ʾ ========\n";
