#!/usr/bin/jperl
# jptexindex -- 日本語対応 texindex
# Copyright (C) 1996, 1997 Mikio Nakajima <gy2m-nkjm@asahi-net.or.jp>
 
# Author: Mikio Nakajima <gy2m-nkjm@asahi-net.or.jp>
# Created: Dec 12, 1996
# Last Modified: Sat Jan 25 18:15:08 1997
# Keywords: Texinfo, TeX, japanese
$Version = 0.5;
# 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 versions 2, 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 SKK, see the file COPYING.  If not, write to the Free
# Software Foundation Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.
 
# Commentary:
# 
# 日本語対応 (というか Perl が split 関数を対応させている言語なら多分どんな言
# 語でも対応) の texindex です。
# Texinfo のインデクスコマンド (@cindex など) の引数に日本語を使うときは、
#
#   @cindex インデクス[いんてくす]
#   @cindex 言語[けんこ]
#
# などのように `@cindex 項目[よみ]' の形式で書くとこの texindex でソートして索
# 引を作ってくれます。

# texindex.c の仕様を良く読んだわけではないので、何か忘れている仕事があるかも
# しれません。お気付きの点は是非お知らせ下さい。特に TeX コマンドでクォートさ
# れる文字は、
# 
#   `_' -> {\_}
#   `^' -> {\tt\hat}
#   `>' -> {\tt\gtr}
#   `<' -> {\tt\less}
#   `"' -> {\tt\char '042}
#   `+' -> {\tt\char 43}
#   `=' -> {\\tt\\char 61} #NTT jTeX only?
#   `@' -> {\\tt\\char '100} #NTT jTeX only?
#   `{' -> {\tt\char '173}
#   `|' -> {\tt\char '174}
#   `}' -> {\tt\char '175}
#   `~' -> {\tt\char '176}
#   `\' -> {\tt\indexbackslash }   
#   
#
# しか知らないので、漏れがあるかもしれません。

# Change log:

# Code:

$CONVERTER = "nkf -e";
$Version .= " (January 25, 1997)";

require "getopts.pl";
&Getopts(':v');

die "jptexindex $Version\n" if $opt_v;

foreach $rawfile (@ARGV){
    $processedfile = $rawfile . "s";
    # nkf を使う必要のない方は、下記 2 行をコメントアウトの上、85 行目の
    # while(<IN>) を while(<>) に変更して下さい。
    open(IN, "$CONVERTER $rawfile |") || die "Cannot run $CONVERTER: $!\n";
    open(OUT, "> $processedfile") || die "Cannot open $processedfile: $!\n";

#
# \entry{Makefile}{10}{\file {Makefile}}
#   -> $item := Makefile, $page := 10, $realitem := {\file {Makefile}}
#
    #initialize
    %entries = ();
    while(<IN>){
    #while(<>){
        ($item, $page, $realitem) = m/^\\entry(.+){([0-9]+)}(.+)$/o;
        $item =~ s/{\\_}/_/o || $item =~ s/{\\tt\\hat}/^/o || $item =~ s/{\\tt\\gtr}/>/o ||
        $item =~ s/{\\tt\\less}/</o || $item =~ s/{\\tt\\char *'042}/"/o ||
        $item =~ s/{\\tt\\char *43}/+/o ||
        $item =~ s/{\\tt\\char *61}/=/o || # NTT/jTeX only?
        $item =~ s/{\\tt\\char *'100}/@/o || # NTT/jTeX only?
        $item =~ s/{\\tt\\char *'173}/{/o ||
        $item =~ s/{\\tt\\char *'174}/|/o || $item =~ s/{\\tt\\char *'175}/}/o ||
        $item =~ s/{\\tt\\char *'176}/~/o || $item =~ s/{\\tt\\indexbackslash }/\\/o;
        # {hogehoge} -> hogehoge
        substr($item, 0, 2) = "";
        chop($item);
        # hogehoge -> HOGEHOGE
        $item =~ tr/a-z/A-Z/;
        # HOGEHOGE[ほげほげ] -> ほげほげ
        if ($item =~ /^[^[]+\[([^]]+)\]$/o) {
            $item = $1;
        }
        #print "\$item is ", $item, "\n";
        if ($entries{$item} ne "") {
            chop($entries{$item});
            # \\entry{HOGEHOGE}{page1, page2}
            $entries{$item} .= ", " . $page . "}";
        }else {
            if ($realitem =~ /^{([^[]+)\[[^]]+\]}$/o) {
                # {HOGEHOGE[ほげほげ]} -> {HOGEHOGE}
                $realitem = "{" . $1 . "}";
            }
            # \\entry{HOGEHOGE}{page}
            $entries{$item} = "\\entry " . $realitem . "{" . $page . "}";
        }
    }
    @chars= ();
    # 見出し語でソート
    foreach $key (sort keys(%entries)) {
        # $key == HOGEHOGE
        @chars = split(//, $key);
        # $thisheadchar == $chars[0] == H
        #print "\$chars[0] is ", $chars[0], "\n";
        $thisheadchar = $chars[0];
        $thisheadchar =~ s/_/{\\_}/o || $thisheadchar =~ s/\^/{\\tt\\hat}/o ||
        $thisheadchar =~ s/>/{\\tt\\gtr}/o || $thisheadchar =~ s/</{\\tt\\less}/o ||
        $thisheadchar =~ s/"/{\\tt\\char '042}/o || $thisheadchar =~ s/\+/{\\tt\\char 43}/o ||
        $thisheadchar =~ s/=/{\\tt\\char 61}/o || $thisheadchar =~ s/@/{\\tt\\char '100}/o ||
        $thisheadchar =~ s/{/{\\tt\\char '173}/o || $thisheadchar =~ s/\|/{\\tt\\char '174}/o ||
        $thisheadchar =~ s/}/{\\tt\\char '175}/o || $thisheadchar =~ s/~/{\\tt\\char '176}/o ||
        $thisheadchar =~ s/\\/{\\tt\\indexbackslash }/o;
        #print "\$thisheadchar is ", $thisheadchar, "\n";
        #print "\$headchar is", $headchar, "\n";
        if ($thisheadchar ne $headchar){
            print OUT "\\initial {", $thisheadchar, "}", "\n";
            $headchar = $thisheadchar;
        }
        print OUT $entries{$key}, "\n";
      }
}
# end of jtexindex
