#!/usr/bin/env perl
# $Id: fixtexindexpre 615 2010-09-30 15:49:42Z florenz@TU-BERLIN.DE $

# this script is called with the same arguments as texindex and 
# tries to fix some bugs and flaws wrp to the dosfop generated indices

# Flaw 1) the inserted \penalty 10000 are removed to allow linebreaking of 
# functionalities 

# this also shortens the index files a lot which is important since texindex
# cannot deal with very large index files

# Bug 1) backslash character in entries is not (always?) handled correctly
# entries of \ are changed to entries of {\fam \ttfam \tentt \rawbackslashxx }
# and the corresponding code field likewise


print "fixtexindexpre v 1.0\n";

# what the backslash is translated to
$BSLreplacement = "{\\fam \\ttfam \\tentt \\rawbackslashxx }";

while (<@ARGV>) 
{  $indexfile = $_ ;
   print "now fixing(pre) $indexfile ...";
   $oldindexfile = $indexfile . ".out";

   rename($indexfile, $oldindexfile) || die "backup of $indexfile failed";
   open(IN, $oldindexfile) || die "cannot open $oldindexfile";
   open(OUT, ">" . $indexfile)  || die "cannot open file $indexfile";
   
   while(<IN>) 
   { s/\\penalty 10000//g;
     if(/^\\entry {\\/)
     { s/(\\entry {)\\/\1$BSLreplacement/;
       s/(\\code {)\\/\1$BSLreplacement/;
     };
     print OUT;
   };

   close IN || die "trouble closing $oldindexfile";
   close OUT || die "trouble closing $indexfile";
   unlink $oldindexfile;
   print "done\n";
 
}
