#!/bin/sh
#
# latex2dvi - prepare LaTeXinfo files for printing.  
#
# Copyright (C) 1990, 1991 Free Software Foundation.
#
# Roland McGrath <roland@gnu.ai.mit.edu>
# Converted To LaTeXinfo by Mike Clarkson <mike@apl.ists.ca>
# Version 0.10
# 24 Sep 91
#
# 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 version 1, 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.
#
# A copy of the GNU General Public License can be obtained from this
# program's author (send electronic mail to roland@ai.mit.edu) or from
# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
# 02139, USA.

if [ $# -eq 0 ]; then
  echo "Usage: `basename $0` FILE ..." >&2
  exit 1
fi

# The names of your LaTeX commands.
LATEXINDEX=latexindex
BIBTEX=bibtex
LATEX=latex

for file in $*; do
  if [ ! -f $file ] ; then
	if [ ! -f $file.tex ] ; then
		echo `basename $0`: File not found - $file
		echo Please give the full file name.
		exit 1
	else
	   file=$file.tex
	   fi
        fi
  base="`basename $file .latexinfo | sed -e 's/\.texi$//' -e 's/\.tex$//'`"

  # Find all existing index files corresponding to FILE.
  idx_files="`echo ${base}.??`"
  if [ "$idx_files" = "${base}.??" ] ; then
    # Maybe it's new!
    if grep '^\\newindex{' $file > /dev/null ; then
	changed=yes
    else
   	idx_files=''
    fi
  else
    # Ignore files with two-letter extensions that don't look like index files.
    oidx_files="$idx_files"
    idx_files=''
    for idx_file in $oidx_files; do
      if [ "`sed -n '1s/^\(.\).*$/\1/p' $idx_file`" = \\ ]; then
        # It starts with a backslash, so it's probably an index file.
	idx_files="$idx_files $idx_file"
      fi
    done
  fi

  if grep '^\\bibliography{' $file > /dev/null ; then
    bibtex=yes
    bib_file=$base.bbl
    if [ -f $bib_file ] ; then
	    # Save a copy of the old bbl file.
	    cp ${bib_file} ${bib_file}O
    else
             # There wasn't a bbl before so run it again anyway.
             changed=yes
    fi
  fi

  for idx_file in $idx_files; do
    # Save a copy of the old index file.
    cp ${idx_file} ${idx_file}O
  done

  if [ "$idx_files" != "" ]; then
    # Run LATEXINDEX on the index files.
    echo ${LATEXINDEX} $idx_files
    ${LATEXINDEX} $idx_files
  fi

  aux_file=$base.aux
  if [ -f $aux_file ] ; then
    # Save a copy of the old aux file.
    cp ${aux_file} ${aux_file}O
  else
    # There wasn't a aux before so run it again anyway.
    changed=yes
  fi

  # Run LaTeX on FILE.
  echo ${LATEX} $file
  if ${LATEX} $file; then

    # Find all the index files that exist now,
    # so we can see if there are any new ones.
    new_idx_files="`echo ${base}.??`"
    if [ "$new_idx_files" = "${base}.??" ]; then
      new_idx_files=''
    else
      oidx_files="$idx_files"
      new_idx_files=''
      for idx_file in $oidx_files; do
	if [ "`sed -n '1s/^\(.\).*$/\1/p' $idx_file`" = \\ ]; then
	  # It starts with a backslash, so it's probably an index file.
	  new_idx_files="$new_idx_files $idx_file"
	fi
      done
    fi

    if [ "$new_idx_files" != "$idx_files" ]; then
      # There are some new index files.
      changed=yes
      idx_files="$new_idx_files"
    else
      # Run through all the index files, comparing them to the old ones.
#      changed=no
      for idx_file in $idx_files; do
	# Compare the old and new index files.
	cmp -s ${idx_file}O ${idx_file}
	status=$?

	# Remove the old index file.
	rm -f ${idx_file}O

	if [ $status -ne 0 ]; then
	  # The index file has changed.
	  changed=yes
	fi
      done	# for idx_file
    fi

    if [ "$bibtex" = "yes" ] ; then
       echo ${BIBTEX} $base
       if ${BIBTEX} $base ; then
          if [ -f $bib_file ] ; then
             # Compare the old and new index files.
             cmp -s ${bib_file} ${bib_file}O
             status=$?
     
             # Remove the old index file.
             rm -f ${bib_file}O
     
             if [ $status -ne 0 ]; then
               # The index file has changed.
               changed=yes
             fi
          fi
       else
         # BibTeX failed.  Remove the copies of the backup bbl file.
         if [ -f ${bib_file}O ] ; then
             rm -f ${bib_file}O
         fi
       fi
    fi
  
    # Compare the old and new aux files.
    if [ -f ${aux_file}O ] ; then
	cmp -s ${aux_file}O ${aux_file}
	status=$?

	# Remove the old aux file.
	rm -f ${aux_file}O

	if [ $status -ne 0 ]; then
	  # The aux file has changed.
	  changed=yes
	fi
    else
	 # The aux file has changed.
         changed=yes
    fi

   if [ "$changed" = "yes" ]; then
      # Some index file changed.  Run LATEXINDEX and LaTeX again.

      # Run LATEXINDEX on the index files.
      echo ${LATEXINDEX} $idx_files
      if ${LATEXINDEX} $idx_files; then
	# Run LaTeX2dvi on FILE.
	echo $0 $file
	$0 $file
      fi
    fi
  else
    # LaTeX failed.  Remove the copies of the index files.
    for idx_file in $idx_files; do
      rm -f ${idx_file}O
    done
  fi

done	   # for file