#!/usr/local/bin/perl

$| = 1;


# split line
$splitline = 30;

# index name
$base = "intra";


open (FILES,"find download -type f -name '*' -print |") || die "";
$line = 0;
$filenum = 0;
open (LIST, ">$base.$filenum.list") || die "Can't open $base.$filenum.list";
$filenum++;
while(<FILES>)
{
    s/(.*)/$1 $1/;
    s/ download/ http:\//;
    s/_default$//;
    print LIST $_;
    $line++;
    if ($line >= $splitline) {
	close (LIST);
	open (LIST, ">$base.$filenum.list") || die "Can't open $base.$filenum.list";
	$filenum++;
	$line = 0;
    }
}
close (LIST);

for ($count = 0; $count < $filenum; $count ++)
{
    system("/usr/local/bin/html2fdif -o $base.$count -f $base.$count.list");
    system("/usr/local/bin/findex -d /usr/local/lib/freya/dict/icot.dic -o $base.$count $base.$count.fdif");
}

$step = 5;
$count = 0;
while (1)
{
    $indexfiles = "";
    for ($i = 0; $i < $step; $i++)
    {
	if ($count >= $filenum) {
	    last;
	}
	($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	 $atime,$mtime,$ctime,$blksize,$blocks) = stat("$base.$count.idx");
	if ($size > 0) {
	    $indexfiles .= " $base.$count";
	}
	$count++;
    }
    if ($i == 1)
    {
	last;
    }
    print "/usr/local/lib/freya/bin/fmerge $base.$filenum $indexfiles\n";
    system("/usr/local/lib/freya/bin/fmerge $base.$filenum $indexfiles");
    $filenum++;
    foreach $indexfile (split(' ', $indexfiles)) {
  	unlink("$indexfile.fdif");
  	unlink("$indexfile.list");
  	unlink("$indexfile.dsc");
  	unlink("$indexfile.map");
  	unlink("$indexfile.lex");
  	unlink("$indexfile.idx");
    }
}
$lastnum = $filenum - 1;

print "rename $base.$lastnum.dsc $base.dsc\n";
unlink("$base.$lastnum.fdif");
unlink("$base.$lastnum.list");

rename("$base.$lastnum.dsc" ,"$base.dsc");
rename("$base.$lastnum.map" ,"$base.map");
rename("$base.$lastnum.lex" ,"$base.lex");
rename("$base.$lastnum.idx" ,"$base.idx");
