#!/usr/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# cmdtext720 src/cmdtext/usr/bin/subndx/ndx.sh 1.13 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1989,1994 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)93    1.13  src/cmdtext/usr/bin/subndx/ndx.sh, cmdtext, cmdtext720 3/30/07 03:26:13
#
# COMPONENT_NAME: (CMDTEXT) Text Formatting Services
#
# FUNCTIONS:
#
# ORIGINS: 54,27,28
#
# (C) COPYRIGHT International Business Machines Corp. 1989,1998
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#
DSPMSG=/usr/bin/dspmsg
SED=/usr/bin/sed
RM=/usr/bin/rm
SORT=/usr/bin/sort

UL=/usr/lib/dwb
#
#  test for the correct number of arguments and
#  that the subject file exists and is readable
#
if [[ $# -lt 2 || $# -gt 2 || "$1" = "-" ]]
then
    $DSPMSG ndx.cat 1 'Usage: ndx subject_file "formatter_command_line"\n' >&2
    exit 1
elif [[ ! -r "$1" ]]
    then
    $DSPMSG ndx.cat 3 "%1$s: Cannot find or open %2$s.\n" ${0##*/} $1 >&2
    $DSPMSG ndx.cat 4 "Please check to see that %s exists and is readable.\n" $1 >&2
    exit 1
fi
$DSPMSG ndx.cat 5 "ndx running\n" >&2

umask 077
TMPDIR=${TMPDIR:-$HOME/tmp}
[[ ! -d $TMPDIR ]] && TMPDIR=/tmp
TMPDIR=$TMPDIR/${0##*/}.$$
/usr/bin/mkdir $TMPDIR || {
        print -u2 "${0##*/}: Could not create temporary files"
        exit 1
}
trap "/bin/rm -rf $TMPDIR 2>/dev/null" EXIT INT TERM QUIT HUP

TEXT=$TMPDIR/cpy
SUBJS=$TMPDIR/sb
SUJS=$TMPDIR/sj

#store name(s) of the textfile(s) in "textlist" and the options
#to the formatter (nroff, troff, mmt, mm) in "optlist"
#
textlist=
optlist=
opt=n    	#formatter option, default=nroff
pgopt=n    		#section-page numbering, default = none
pagereg=n    	#formatter page register, default=nroff
arg=$2
# This awk script assumes that the '!%@' sequence will never
# be on the command-line.
options=$(echo $arg | /usr/bin/awk '
   BEGIN {
	cmd = ""; pagereg = "n"; pgopt = "n"; optlist = ""; textlist = "";
   }
   {
	if ($1 == "troff" || $1 == "nroff" || $1 == "mmt" || $1 == "mm") {
		cmd = $1;
		if ($1 == "mm") pagereg = "m";
	} else {
		exit 1
	} # endif
	i = 2
	while (i <= NF) {
		if (match($i,/-rN[35]/)) {
			pgopt = "s";
			optlist = optlist " " $i;
		} else {
			if (match($i,/-z/)) {
				# do nothing
			} else {
				if (match($i,/-.*/)) {
					optlist = optlist " " $i
				} else {
					# Must be a document name.
					textlist = textlist " " $i
				} # endif
			} # endif
		} # endif
		i++
	} # endwhile
   }
   END {
	print cmd "!%@" pagereg "!%@" pgopt "!%@" optlist "!%@" textlist
   }'   # endawk
)
[[ $? != 0 ]] && { 
	$DSPMSG ndx.cat 6 "%s: %s is not a valid formatter.\n" ${0##*/} $arg >&2;
	$DSPMSG ndx.cat 7 "%s: Valid formatters are: troff, nroff, mmt, mm.\n" ${0##*/} >&2
	exit 1;
}

cmd=${options%%\!\%@*}
pagereg=${options#*\!\%@}; pagereg=${pagereg%%\!\%@*}
pgopt=${options#*\!\%@}; pgopt=${pgopt#*\!\%@}; pgopt=${pgopt%%\!\%@*}
optlist=${options%\!\%@*}; optlist=${optlist##*\!\%@}
textlist=${options##*\!\%@}

case $cmd in
	troff) opt=t;;
	nroff) opt=n;;
	mmt)   opt=z;;
	mm)    opt=m;;
esac

#
$SED -e "/^$/d" -e "/^[ 	][ 	]*$/d" $1 > $SUBJS$$
$SORT -bu -o $SUBJS$$ $SUBJS$$
/usr/bin/pr -t -n1 $SUBJS$$ > $SUJS$$
    
    #
    #  begin inner loop: it repeats for each textfile
    #                    this is where the real business of this shell
    #                    program is begun.  The program ndexer, a
    #                    lex program, is run on each textfile in the
    #                    "textlist" seaching for the subjects listed in
    #                    the current subject list
    #                    save the marked up textfile in $TEXT$COUNT$$
    #
    
    COUNT=0
    for text in $textlist
    do
    	if [[ ! -r $text ]]
    	then
    	   $DSPMSG ndx.cat 3 "%s: Cannot find or open %s.\n" ${0##*/} $text >&2
    	   $DSPMSG ndx.cat 4 "Please check to see that %s exists and is readable.\n" $text >&2
    	    $RM -f $TEXT*$$ $SUBJS$$.* $SUJS$$.*
    	    exit 1
    	fi
    	$UL/ndexer $SUJS$$  $text $TEXT$COUNT$$ $pagereg > /dev/null
    	if [[ $? -ne 0 ]]
    	then
              $DSPMSG ndx.cat 8 "%s: %s aborted, files were %s %s.\n" ${0##*/} ${0##*/} $1 $text >&2
              $RM -f $TEXT*$$ $SUBJS$$.* $SUJS$$.*
              exit 2
    	fi      
    	COUNT=`/usr/bin/expr "$COUNT" + 1`
   done
    
    FILE10=
    FILE100= 
    FINAL=$TMPDIR/ndx0$$
    IFILE2=$TMPDIR/ndx2$$
    SFILE=$TMPDIR/ndx3$$
    
    #
    #  format the textfiles using the options saved in "optlist" and the user-
    #  named formatter.  Save only the subject numbers and page numbers in the
    #  file $FINAL
    #
    if [[ "$COUNT" -gt "9" ]] ; then FILE10="$TEXT??$$"; fi;
    if [[ "$COUNT" -gt "99" ]] ; then FILE100="$TEXT???$$"; fi;
    case $opt in
     t|n|z) $cmd -z $optlist  $TEXT?$$ $FILE10 $FILE100 1>/dev/null 2> $FINAL ;;
     m)     $cmd  $optlist  $TEXT?$$ $FILE10 $FILE100 1>/dev/null 2> $FINAL ;;
    esac
    
    if [[ $? -ne 0 ]]
    then
    	$DSPMSG ndx.cat 10 '  Error message from %s.\n' $cmd >&2
    
	$SED -n "/^[^0-9]/p" $FINAL >&2
	$DSPMSG ndx.cat 14 "  Please check your formatter options.\n"
	exit 3
    fi
    
    #
    #  Process $FINAL replacing subject numbers
    #  with actual subjects and collating page numbers
    #  Append the index to INDEX$$
    #
    
    $SED -n "/^[^0-9]/p" $FINAL >&2
    $SED "/[^0-9] [^0-9] */d" $FINAL |
        $SORT  -u +0n +1n +2n | $UL/pages $pgopt >$IFILE2
    $UL/sbjprep $SUJS$$ >$SFILE
    paste -d] $SFILE $IFILE2 >> $TMPDIR/INDEX$$ 
    $RM -f $TEXT*$$
        $RM -f $FINAL


#
#  if all the subject lists have been processed, sort the file
#  INDEX$$, make it look good, and output it to the user.
#  remove temporary files and exit.
#

        $SED "s/,/ ,/" $TMPDIR/INDEX$$ |
    $SORT -u +1fib -t] -2 +1 -2  | 
    $SED "s/ ,/,/" |
    $UL/ndxformat  
    $RM -f $TMPDIR/slist.$$ $SUBJS$$* $SUJS$$  $IFILE2 $SFILE  INDEX$$
$DSPMSG ndx.cat 15 "ndx has completed\n" >&2