#! /usr/bin/perl

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use utf8;
use encoding 'utf8';
use open ':utf8'; # input and output default layer will be UTF-8
use POSIX;
my @words;
my $usage="Usage: $0 [-r | -k | -f | -l]\n\tto sort by root, keyword, freq, keyword-length\n";
if ($#ARGV!=0) {die $usage;}
my $by=shift;
if ($by ne "-r" and $by ne "-k" and $by ne "-f" and $by ne "-l") {die $usage;}
sub cd_th {
  my $pwd=getcwd();
  do {
    if (-d "+") {return $pwd;}
    chdir("..");
    $pwd=getcwd();
  }until ($pwd eq "/");
  return 0;
}

my $prefix;
$prefix=cd_th() or die "Could not found Thwab control + directory";
printf "@<%s>\n",$prefix;
open IX, $prefix."/+/4";
print "loading search cache\n";
my $i=0;
while(<IX>) {
  chomp;
  if (/^\s*#/) {next}
  elsif (/^([^\t]+)\t+([^\s]+)\s+(\d+)\s+(.*)$/) {
    # $words{index} = root keyword freq XX/YY ...
    $words[$i]=["$1", "$2", $3, "$4"];
    $i=$i+1;
  } else {
    printf stderr "Skipping line [$_]\n";
  }
}
close IX;
print "sorting search cache\n";
if ($by eq "-r") {@words=sort {return $$a[0] cmp $$b[0]} @words;}
elsif ($by eq "-k") {@words=sort {return $$a[1] cmp $$b[1]} @words;}
elsif ($by eq "-f") {@words=sort {return $$a[2] <=> $$b[2]} @words;}
elsif ($by eq "-l") {@words=sort {return length($$a[1]) <=> length($$b[1])} @words;}
else {die "unknown sorting method $by\n$usage"}
print "generating new search cache\n";
open IX, ">".$prefix."/+/4.tmp";
for ($i=0;$i<=$#words;$i+=1) {
  printf IX "%s\t%s %d %s\n",$words[$i][0], $words[$i][1], $words[$i][2], $words[$i][3];
}
close IX;
print "replacing search cache\n";
rename $prefix."/+/4.tmp", $prefix."/+/4" or die "can't overwrite files\n";
