#!/usr/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# cmdtext720 src/cmdtext/usr/bin/vgrind/vgrind.sh 1.19 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1989,1994 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)44      1.19  src/cmdtext/usr/bin/vgrind/vgrind.sh, cmdtext, cmdtext720 9/16/08 05:07:15
#
# COMPONENT_NAME: (CMDTEXT) Text Formatting Services
#
# FUNCTIONS:
#
# ORIGINS: 10,26,27,28
#
# (C) COPYRIGHT International Business Machines Corp. 1989, 1998
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#
# Copyright (c) 1980 Regents of the University of California.
# All rights reserved.  The Berkeley software License Agreement
# specifies the terms and conditions for redistribution.
#
#	Converted from csh to bsh 3/90
#	@(#)vgrind.sh	5.3 (Berkeley) 11/13/85
#
# vgrind
#
b="/usr/lib"
voptions=""
options=""
Files=
f=''
head=""
print_prog="/usr/bin/qprt -dp"
pproc="/usr/bin/ibm3816"
poptions=""
saw_type=""
saw_print=""
temp_list=""
troff="/usr/bin/troff"

CAT=/usr/bin/cat
SED=/usr/bin/sed
RM=/usr/bin/rm
XPREVIEW=/usr/bin/xpreview

umask 077
TMPDIR=${TMPDIR:-$HOME/tmp}
[[ ! -d $TMPDIR ]] && TMPDIR=/tmp
TMPDIR=$TMPDIR/${0##*/}.$$

/usr/bin/mkdir $TMPDIR || {
        print -u2 "${0##*/}: Could not create temporary files"
        exit 1
}
trap "/bin/rm -rf $TMPDIR 2>/dev/null" EXIT INT TERM QUIT HUP

tmp="$TMPDIR/vf$$"
tmp1="$TMPDIR/pp$$"
to_stdout="false"

#process all command line arguments
while [ $# -gt 0 ]
do
    case $1
    in
    -f) f='filter'
	options="$options $1"
	;;
    -t) voptions="$voptions"
	print_prog="cat"
	to_stdout="true"
	;;
    -P*) poptions="$poptions $1"
         saw_print=$1
	;;
    -T*) voptions="$voptions $1"
        saw_type="$1"
        pproc="`echo $1 | $SED 's/^-T//'`"
	;;
    -o*) voptions="$voptions $1"
	;;
    -d) if [ "$#" -lt 2 ]
	then
	    /usr/bin/dspmsg vgrind.cat 1 "vgrind: %s option must have argument\n" $1
	    exit
	else
	    options="$options $1 $2"
	    shift
	fi
	;;
    -h) if [ "$#" -lt 2 ]
	then
	    /usr/bin/dspmsg vgrind.cat 1 "vgrind: %s option must have argument\n" $1
	    exit
	else
	    head="$2"
	    shift
	fi
	;;
    -*) options="$options $1"
	;;
    *) Files="$Files $1"
	;;

    esac
    shift

done
#endwhile

#if postprocessor is not specified
if [ ! "$saw_type" ]
    then
	#check for env variable TYPESETTER
    if [ $TYPESETTER ]
	then
        voptions="$voptions -T${TYPESETTER}"
	pproc="${TYPESETTER}"
    else
	#else use default
        voptions="$voptions -Tibm3816"
    fi
fi
if [ "$print_prog" != "cat" ]
    then
    for i in $poptions
	do
	temp_list="$temp_list $i"
    done

    poptions="$temp_list"

	#check for specified printer to print spooler
	if [ ! "$saw_print" ]
	then
		if [ "$PRINTER" ]
		then
			poptions="$poptions -P${PRINTER}"
		else
			poptions="$poptions"
		fi
	fi
	if [ "$pproc" = "psc" ]
	then
		print_prog="qprt -ds"
	fi
else
	poptions="-"
fi
if [ -r index ]
    then
    echo > $TMPDIR/nindex
    for i in $Files
	do
	echo "? $i ?d" | $SED -e "s:/:\\/:g" -e "s:?:/:g" >> $TMPDIR/nindex
    done
    $SED -f $TMPDIR/nindex index >$TMPDIR/xindex
    if [ "$f" = 'filter' ]
	then
	if [ "$head" != "" ]
	    then
	    $b/vfontedpr $options -h "$head" $Files | $CAT $b/tmac/tmac.vgrind -
	else
	    $b/vfontedpr $options $Files | $CAT $b/tmac/tmac.vgrind -
	fi
    elif [ "$head" != "" ]
	    then
	    $b/vfontedpr $options -h "$head" $Files > $tmp
	    $troff -rx1 $voptions -mvgrind $tmp 2>> $TMPDIR/xindex > $tmp1
	    if [ $pproc = "X100" -o $pproc = "X100K" ]
	    	then
		if [ $to_stdout = "true" ]
		    then
		    $CAT $tmp1
		else
		    $XPREVIEW $tmp1
		fi
	    else 
		$CAT $tmp1 | $pproc | $print_prog $poptions
	    fi
	else
 	    $b/vfontedpr $options $Files > $tmp
  	    $troff -rx1 $voptions -mvgrind $tmp 2>> $TMPDIR/xindex > $tmp1
	    if [ $pproc = "X100" -o $pproc = "X100K" ]
		then
		if [ $to_stdout = "true" ]
		    then
		    $CAT $tmp1
		else
		    $XPREVIEW $tmp1
		fi
	    else
		$CAT $tmp1 | $pproc | $print_prog $poptions
	    fi
    fi
    $RM -f $tmp $tmp1
    /usr/bin/sort -df +0 -2 $TMPDIR/xindex >index
    $RM $TMPDIR/nindex $TMPDIR/xindex
elif [ "$f" = "filter" ]
	then
	if [ "$head" ]
	    then
	    $b/vfontedpr $options -h "$head" $Files | $CAT $b/tmac/tmac.vgrind -
	else
	    $b/vfontedpr $options $Files | $CAT $b/tmac/tmac.vgrind -
	fi
    elif [ "$head" != "" ]
	    then
	    $b/vfontedpr $options -h "$head" $Files > $tmp
	    $troff $voptions -mvgrind $tmp > $tmp1
	    if [ $pproc = "X100" -o $pproc = "X100K" ]
		then
		if [ $to_stdout = "true" ]
		    then
		    $CAT $tmp1
		else
		    $XPREVIEW $tmp1
		fi
	    else
		$CAT $tmp1 | $pproc | $print_prog $poptions
	    fi
	else
	    $b/vfontedpr $options $Files > $tmp
	    $troff $voptions -mvgrind $tmp > $tmp1
	    if [ $pproc = "X100" -o $pproc = "X100K" ]
		then
		if [ $to_stdout = "true" ]
		    then
		    $CAT $tmp1
		else
		    $XPREVIEW $tmp1
		fi
	    else
		$CAT $tmp1 | $pproc | $print_prog $poptions
	    fi
    fi
    $RM -f $tmp $tmp1
exit