#!/bin/sh
#
# "mktex"	K. J. Turner	11/09/93
#
# This "sh" script takes a list of chapter/section names to be
# processed by LaTeX. The "-f" option gives the name of the LaTeX
# file, without a ".tex" extension; the default name is "main".
#
# The command line for "mktex" gives a list of the chapter/section
# files to be included. (The list may be empty.) "mktex" scans the
# LaTeX file, looking for lines which begin with "\include{". If
# the file name in any such line matches a name given on the
# command line, the file is added to an "\includeonly" list. This
# list is output to a file "mktex.sty". The LaTeX file must
# therefore include "mktex" as a style file.
#
# The option "-a" includes all LaTeX files
#
# Chapter/section names are matched with "egrep". If only the
# first part of a name is given, multiple matches may arise. (This
# is useful if there are a number of files in a directory to be
# included.)
#
# "mktex" then runs LaTeX on the main file. If an index file is
# produced, it is moved to the first chapter/section file in the
# "\includeonly" list. (This is really only useful if one file is
# being processed.)
#
# Finally, "mktex" prints the file if required (on an HP system).
#
# The exit code is 0 if there are no errors, otherwise 1.
#
# A LaTeX file for use with "mktex" might be:
#
#   \documentstyle[a4wide,eurodate,mktex,12pt]{article}
#   
#   \begin{document}
#  
#   \tableofcontents
#  
#   \include{0/pref}
#  
#   \include{1/intro}
#  
#   \include{2/ex-1}
#   \include{2/ex-2}
#   \include{2/ex-3}
#  
#   \include{3/explan}
#  
#   \include{4/trial-e}
#   \include{4/trial-l}
#   \include{4/trial-z}
#  
#   \include{5/test}
#  
#   \include{6/concl}
#  
#   \end{document}
#
# To include all of chapters 0 and 4 do:
#
#   mktex 0 4
#
# To include only 2/ex-1 and 2/ex-3 do:
#
#   mktex 2/ex-1 2/ex-3
#
# If the file were called "trials.tex", the call on "mktex" to
# process chapter 5 would be:
#
#   mktex -f trials 5

code=0
prog=`basename $0`
file="main"
tex=".tex"
idx=".idx"
dvi=".dvi"
mkfile="mktex.sty"
sections=""
system=""

while [ $# -gt 0 ]
  do
    case $1 in
      -a)
        sections="^\\\\include{.\\\\*"; shift;;
      -f)
	file=$2; shift 2;;
      -f*)
        file="`expr $1 : '-f\(.*\)'`"; shift;;
      -*)
        echo "$prog: illegal option $1"
        exit 1;;
      *)
        break;;
    esac
  done

file=`basename $file $tex`

if [ ! -f $file$tex -o ! -r $file$tex ]
  then
    echo "$prog: cannot read $file$tex"
    exit 1
fi

while [ $# -gt 0 ]
  do
    if [ "$sections" = "" ]
      then sections="^\\\\include{$1"
      else sections="$sections|^\\\\include{$1"
    fi
    shift
  done

echo "\\\\includeonly{%" > $mkfile

if [ "$sections" != "" ]
  then
    egrep $sections $file$tex |
      sed 's/\\include{\([^}]*\)}/  \1,%/' >> $mkfile
fi

echo "}" >> $mkfile

if latex -V $file
  then
    indfile=`sed -n '2s/  \([^,%]*\),%/\1/p' $mkfile`
    if [ "$indfile" != "" -a -r $file$idx ]
      then
        if [ -f $indfile$idx ]
          then
	    echo -n "$prog: remove $indfile$idx (y/n)? "
	    read ans
	    if [ "$ans" != "n" -a "$ans" != "N" ]
	      then mv $file$idx $indfile$idx
	    fi
          else mv $file$idx $indfile$idx
        fi
    fi
    if [ "$system" = "HP" ]
      then
	echo -n "$prog: print (n=no, y=yes, or page numbers)? "
	read pages
	case $pages in
	  n|N|"")
	    ;;
	  y|Y)
	    dvips $file; code=$?;;
	  *)
	    dvips -p $pages $file; code=$?;;
	esac
    fi
  else code=1
fi

exit $code
