#!/bin/sh

# Move the table of contents in a DVI file generated by texi2dvi to
# the beginning.
#
# NOTE: This might produce strange results when applied to DVI files
# not generated by texi2dvi. @@ I don't know of an automatic test
# whether a given DVI file was generated by texi2dvi, so the script
# cannot check this...
#
# Copyright (C) 2000-2002 Free Software Foundation, Inc.
#
# Author: Frank Heckenbach <frank@pascal.gnu.de>
#
# This file is part of GNU Pascal.
#
# GNU Pascal 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 2, or (at your option)
# any later version.
#
# GNU Pascal 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.
#
# You should have received a copy of the GNU General Public License
# along with GNU Pascal; see the file COPYING. If not, write to the
# Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

if [ $# != 1 ]; then
  echo "Usage: `basename "$0"` DVI-file-name" >&2
  exit 1
fi

file="$1"

rm -rf "$file"-tmp.1 "$file"-tmp.2 "$file"-tmp.3 "$file"-tmp.4 "$file"-tmp.5 || exit 1

if dviselect -s -i "$file" -o "$file"-tmp.1 =1:2 &&
   dviselect -s -i "$file" -o "$file"-tmp.2 :_1 &&
   dviselect -s -i "$file" -o "$file"-tmp.3 =3: &&
   dviselect -s -i "$file"-tmp.3 -o "$file"-tmp.4 1: &&
   dviconcat -o "$file"-tmp.5 "$file"-tmp.1 "$file"-tmp.2 "$file"-tmp.4; then
  mv "$file"-tmp.5 "$file"
else
  echo '' >&2
  echo 'Could not run dviselect and dviconcat. Therefore, the table of' >&2
  echo 'contents of the printed documentation will be at the end.' >&2
  echo '' >&2
fi

rm -rf "$file"-tmp.1 "$file"-tmp.2 "$file"-tmp.3 "$file"-tmp.4 "$file"-tmp.5
