#!/usr/bin/env perl
# $Id: makeindex4,v 1.9 2005/05/02 20:19:34 jschrod Exp $
#----------------------------------------------------------------------
#
# This is a perl-script that wraps xindy into a makeindex-like
# interface.
#


# NOTE: Usage of this script is depreciated. Use texindy instead.
# Yes, this is Perl 4 code -- it is as old. I don't plan to change
# that, as there's a better replacement available anyhow. Let's tell
# this with each usage... ;-)

print STDERR <<'_EOF_'

NOTE: Usage of makindex4 is depreciated. This script is not supported
any more. Please consider switching to the supported command texindy.

_EOF_
  ;


($progname = $0) =~ s#.*/##;

$Date = `date`;

require "getopts.pl";

# $debug = 1;

$numArgs = $#ARGV;          # save number of Commandline-Arguments

# parse for makeindex command-line options
&Getopts( "dcgilo:p:qrs:t:" );

$debug = $opt_d;

# some debug-output
print STDERR "\@ARGV:  @ARGV\n"  if $debug;
print STDERR "\$#ARGV: $#ARGV\n" if $debug;

sub welcome {
   print STDERR "\nusage:\t$progname [makeindex options] tex-index-file\n";
   print STDERR <<EOMSG;

This file is a wrapper that makes `xindy' somehow behave as
`makeindex' does. Currenly it does not parse all command-line
arguments and some of the flags are differently implemented in the
xindy system. So don't expect it to be a plug-in replacement for the
makeindex-system.
EOMSG
   exit(1);
}

$numArgs = $#ARGV;          # number of Commandline-Arguments
if ( $numArgs eq 0 )        # only one argument
  { $Index = $ARGV[0]; }    # the index
else { &welcome; }

print "Index = $Index\n" if $debug;
$IndexBase = $Index;
$IndexBase =~ s#\.[^\.]*$##;
print "IndexBase = $IndexBase\n" if $debug;
$TmpStyle = $IndexBase . ".xdy";
open(STY, ">$TmpStyle");

print STY <<EOSTY;
;; This file is generated automatically by `makeindex4'.
;; Date: $Date
;;
;; It is a temporary style-file that is necessary to run `xindy' properly.

EOSTY

if ($opt_c) { print STY <<EOSTY;

;; These lines implement the blank compression defined by
;; the -c switch of makeindex.

(merge-rule " +"  " " :eregexp)
(merge-rule "^[ \t]+" ""   :again :eregexp)
(merge-rule "[ \t]+$" "" :again :eregexp)
EOSTY
}

if ($opt_g) { print STDERR "The option -g not supported!\n"; }

if ($opt_i) { $UseStdin = 1; }

if ($opt_l) { print STY <<EOSTY;
(sort-rule \" \" \"\")
EOSTY
}

if ($opt_q) { print STDERR "The option -q not supported!\n"; }

if ($opt_r) { print STDERR "The option -r not supported!\n"; }

if ($opt_o) { $OutputFile = $opt_o; }

if ($opt_p) { print STDERR "The option -p not supported!\n"; }

if ($opt_s) { print STDERR "The option -s not supported!\n"; }

if ($opt_t) { $LogFile = $opt_t; }

if ( $Index eq $IndexBase ) { $Index = $IndexBase . ".idx"; }
print "Index = $Index\n" if $debug;

unless ($OutputFile) { $OutputFile = $IndexBase . ".ind"; }
print "Outputfile = $OutputFile\n" if $debug;

unless ($LogFile) { $LogFile = $IndexBase . ".ilg"; }
print "Logfile = $LogFile\n" if $debug;

$RawIndex = $IndexBase . ".raw";
$RawIndexStat = $IndexBase . ".sta";

$CmdLine = "tex2xindy $RawIndexStat < $Index > $RawIndex";
print "\nRunning tex2xindy...\n";
print "+ $CmdLine\n";
system($CmdLine);

open(STA, "sort $RawIndexStat | uniq |");

$Attr = "(define-attributes ((";
while( <STA> ) {
    chop;
    ($attr, $type) = split(/\t/);
    if ($type eq "locref") {
	unless ($attr eq "") {
	    $LocAttrs .= "\"$attr\" ";
	    $LocMarkup .= "(markup-locref :open \"\\$attr" .
		"{\" :close \"}\" :attr \"$attr\")\n";
	}
    } else {
	if (! $attr eq "see" ) {
	    $XrefAttrNames .= "\"$attr\" ";
	    $XrefAttrs .= "(define-crossref-class \"$attr\")\n";
	    $XrefMarkup .= "(markup-crossref-list :open \"\\$attr" .
		"{\" :close \"}{}\")\n";
	}
    }
}

print STY <<EOSTY;

;; The following attributes were detected.

(define-attributes (($LocAttrs\"default\")))

;; This is the markup as generated by makeindex.

$LocMarkup

;; The following cross-referernces were detected.

(define-crossref-class "see")

$XrefAttrs

;; This is the markup as generated by makeindex.

(markup-crossref-list :open "\see{" :close "}{}" :class "see")

$XrefMarkup

;; Here are the location-classes

(define-location-class "arabic-page-numbers" ("arabic-numbers"))
(define-location-class "roman-page-numbers"  ("roman-numbers-lowercase"))
(define-location-class "Roman-page-numbers"  ("roman-numbers-uppercase"))
(define-location-class "alpha-page-numbers"  ("alpha"))
(define-location-class "Alpha-page-numbers"  ("ALPHA"))

(define-location-class-order ("roman-page-numbers"
			      "arabic-page-numbers"
			      "alpha-page-numbers"
			      "Roman-page-numbers"
			      "Alpha-page-numbers"
			      "see" $XrefAttrNames))

;; Now load the rest of the makeindex-module...

(require "tex/makeidx4.xdy")

;; End
EOSTY

close(STY);

$CmdLine = "xindy.v2 -o $OutputFile -l $LogFile $TmpStyle $RawIndex";
print "\nRunning xindy...\n";
print "+ $CmdLine\n";
exec($CmdLine);


# ============================================================
#
# $Log: makeindex4,v $
# Revision 1.9  2005/05/02 20:19:34  jschrod
#     Old xindy command is called xindy.v2.
#
# Revision 1.8  2004/10/05 21:13:21  jschrod
#     Notes that its usage is depreciated. Doesn't reference the Quick
# Guide any more.
#     Assumes /usr/bin/perl to exist.
#
# Revision 1.7  1999/04/27 08:56:59  kehr
# Another checkin.
#
# Revision 1.6  1997/04/03 13:49:05  kehr
# Removed a UNIX platform dependency from the code.
#
# Revision 1.5  1997/02/13 14:42:47  kehr
# Minor changes.
#
# Revision 1.4  1997/02/13 13:57:54  kehr
# makeindex4 now uses the module `makeidx4.xdy'.
#
# Revision 1.3  1997/01/21 16:44:33  kehr
# Bugfix: State <print> was not changed back to <key> if {LEVEL} was found.
#
# Revision 1.2  1997/01/17 16:30:35  kehr
# Rewrote makeindex4 and heavily modified tex2xindy.
#
#
