#!/bin/sh
#
# era@iki.fi Fri Jul  2 14:18:54 1999
# $Id: vgrind.sh,v 1.8 1999/09/09 18:02:54 era Exp $
#
# Adapted from vgrind.sh	5.7 (Berkeley) 4/16/91
#
# Original Makefile mentions this address, but it's apparently
# just the coder of the Makefile:
#
#                             Frank Swasey
#                             32 Hickok Place
#                             Burlington, Vermont 05401-8515
#                             USA
#                             Email: Frank_Swasey@vnet.ibm.com
#
# The below copyright notice retained from the original (although 
# I guess I wouldn't really have to, since this is a substantial 
# rewrite of the csh version).
# Resume trying to understand at first blank line.
#
# Copyright (c) 1980 The Regents of the University of California.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	This product includes software developed by the University of
#	California, Berkeley and its contributors.
# 4. Neither the name of the University nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

#set -vx

PATH=/usr/local/bin:/usr/bin:/bin

voptions= options= files= f= head=
vf=/usr/lib/vgrind/vfontedpr
tm=/usr/share/groff/tmac

# Ugly old option processing

until [ $# = 0 ] ; do
	case $1 in
	   -f)
		f=filter
		shift
		;;
	   -t|-o*|-W)	
		voptions="$voptions $1"
		shift
		;;
	   -d)
		case $# in
		   0|1)
			echo "$0: -d option must have argument" >&2
			exit 1
			;;
		   *)
			options="$options $1 $2"
			shift
			shift
			;;
		esac
		;;
	   -h)
		case $# in
		   0|1)
			echo "$0: -h option must have argument" >&2
			exit 1
			;;
		   *)
			head=$2
			shift
			shift
			;;
		esac
		;;
	   -*)
		options="$options $1"
		shift
		;;
	   *)
		break
		;;
	esac
done


$vf $options ${head:+-h "$head"} "$@" |

if [ -r index ]; then

	test -w index || {
		echo "$0: index not writable" >&2
		exit 1
	}

	nindex=/tmp/nindex.$$
	xindex=/tmp/xindex.$$

	trap 'rm -f $nindex $xindex' 1 2 3 15

	for i in "$@" ; do

		# This is supposed to "disarm" file names but
		#  it will still break if a file name contains a newline
       		# ... The original Bezerkeley script only fixed slashes (!),
		#  and even that slightly badly

		# Create a sed script to remove index entries for 
		#  files we are processing now:

		echo "$i" | sed -e 's%[[./*$^\\]%\\&%g' -e 's%.*%/^&:/d%'

		# This is correct for my version of GNU sed (Debian 2.05-22)
		#  but will need to be tweaked for other seds
		#  (most notably, GNU sed doesn't have the regular expression
		#  (a) a{n,m} a? a+ constructs commonly found elsewhere)

	done >$nindex

	sed -f $nindex <index >$xindex

	case $f in
	    filter)
		cat $tm/tmac.vgrind -
		;;
	   *)
		# This -rx1 doesn't appear to do the right thing
		groff -rx1 $voptions -i -mvgrind 2>>$xindex
		;;
	esac

	sort -df +0 -2 $xindex >index

	rm -f $nindex $xindex

else
	case $f in
	   filter)
		cat $tm/tmac.vgrind -
		;;
	   *)
		groff -i $voptions -mvgrind
		;;
	esac
fi
