#!/bin/sh
#
# xdvies talk reference manual.
#
#	Usage:	mantalk [entityname]
#
# entityname is a grep regular expression. The pages containing the
# documentation about the matching entities are automatically selected.
#
# When called with no argument, mantalk displays the index of concepts.
#
# This script should be made much more sophisticated, but at the expense of
# inefficiency.
#
# Examples:
#    $ mantalk                    manual index
#    $ mantalk ".*"               mantalk index
#    $ mantalk filenamep          any function/macro/... exactly named "filename"
#    $ mantalk ".*filename.*"     any function/class/... containing "filename", 
#                                         or method dispatching on such class
#    $ mantalk "<filename>"       the class <filename>
#    $ mantalk ".* <filename>.*"  any method dispatching on <filename>

# Parameter
#   please change xdvi to the appropriate command (with or w/o path)
xdvi=xdvi

# Usage check
#
if [ $# != 0 -a $# != 1 ]
then
   echo "Usage: `basename $0` [regexp]"
   exit 1
fi

local_which(){
WHICH_ARG="$1"
case $WHICH_ARG in
*/*) WHICH_RESULT=$WHICH_ARG 
     ;;
*)      
#    Intent:   WHICH_RESULT=`which $WHICH_ARG`
#    BUT, I don't trust which: some csh users have echoing .cshrc,
#    breaking which csh script for sh scripts.
     WHICH_RESULT=""
     case $PATH in
     :*) fullpath=.$PATH  ;;
     *:) fullpath=$PATH.  ;;
     *)  fullpath=$PATH   ;;
     esac
     for p in `echo $fullpath | sed -e 's/::/:.:/' | tr ':' ' '`
     do if [ -x $p/$WHICH_ARG -a -f $p/$WHICH_ARG ]
        then WHICH_RESULT=$p/$WHICH_ARG
             break
        fi
     done
     ;;
esac
case x$WHICH_RESULT in
x)    ;;
x/*)  ;;
x*)   WHICH_RESULT=`pwd`/$WHICH_RESULT 
      ;;
esac
}

#######################################################################
# Variables that may be changed
#

# Page number of the index of concepts
#
indexpage=115

# Offset of the page number 1 in the .dvi
#
pageoffset=1

# Localization of the manual .dvi and .lot
#

# find this script's filename
local_which "$0"
SCRIPTFILE=$WHICH_RESULT
if [ "$SCRIPTFILE" = "" ]
then echo                        >&2
     echo "** $RUNTALK error: something's wrong with your PATH variable." >&2
     echo "   Please call $RUNTALK specifying its path, e.g. talk32_beta/$RUNTALK" >&2
     exit 1
fi
# get the real script filename, not a symbolic link to it
# hack: "grep ->" instead of non-standard "test -h"
while ( ls -l $SCRIPTFILE | grep " -> " > /dev/null )
do
     SCRIPTFILE=`ls -l $SCRIPTFILE | sed -e "s/.* -> //"`
     SCRIPTFILELINKED=yes
done
SCRIPTDIR=`dirname $SCRIPTFILE`

docdir=$SCRIPTDIR

# Options for xdvi
#
xdviopts="-geometry 920x1020+-90+-140 -s 3"

#
#######################################################################


# A one argument function for displaying the manual at a given page
#
xdvipage()
($xdvi +`dc << !
$1 $pageoffset + p
!` $xdviopts $docdir/part$2.dvi) 2> /dev/null


# No argument given
#
if [ $# = 0 ]
then
  xdvipage $indexpage 1
  exit
fi


# Temporary file
#
entries=/tmp/entries-$$

# Scanning of the .lot file (which actually contains the entities index)
#
# grep ".large .tt [^{]*$1" $lot | sed -n "s/.*.large .tt [(]*\([^{]*$1[^}]*\)\}.*\/\(.*\).\/.*{\([0-9]*\)}$/\3: \1 (a \2)/p" | sort -n > $entries
cat $docdir/part[123].aux | sed -e "s/[.]SHARP[.]/#/g" -e "s/[.]PERCENT[.]/%/g" -e "s/[.]UNDERSCORE[.]/_/g" -e "s/[.]AMPERSAND[.]/\&/g" -e "s/[.]CHAR[.]'134/\\\\/g" | grep "^[\]newlabel{[^-]*--$1}{{.*}{.*}}$" | sed -e "s/^[\]newlabel//" -e "s/{\([^-]*\)--\([^}]*\)}{{\([^}]*\)}{\(I*\)-\([^}]*\)}}/\4 \5 \3 \1 \2/" | sort +0 -1 +1 -n -u > $entries

if [ ! -s $entries ]
then
  echo "No entry found"
  exit 0
fi

echo
echo "Found :"
echo
sed -e "s/\([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) \(.*\)/p.\1-\2 section \3: \4 \5/" < $entries

# Interaction loop
#
echo
# rm before interaction in case of ^C
for page in `sed "s/\([^ ]*\) \([^ ]*\).*/\1-\2/" < $entries | uniq ; rm $entries`
do
 v=`echo $page | sed -e "s/^\(I*\)-.*/\1/" -e "s/III/3/" -e "s/II/2/" -e "s/I/1/"`
 p=`echo $page | sed -e "s/^I*-//"`
 while true
 do
   echo "See page $page (ynq)[y] ?" | tr '\012' ' '
   read ans
   case $ans in
   q) break 2
      ;;
   n) continue 2
      ;;
   y|"")
     xdvipage $p $v
     continue 2
     ;;
   [0-9]*)
     xdvipage $ans $v
     break 2
     ;;
   *)
     echo "Incorrect answer"
   esac
 done
done
