#!/bin/sh
#******************************************************************************
#*                                                                            *
#*  filterps - Adobe Filterps, version 1.0.1                                  *
#*                                                                            *
#******************************************************************************
#*                                                                            *
#* This script determines the document type of $1 and invokes an appropriate  *
#* filter that will convert the document to "better" PostScript language code.*
#*                                                                            *
#******************************************************************************
MAJOR_VERSION=1
MINOR_VERSION=0
REVISION=1
LAST_INSTALL_DATE=FilledInByPostinstall
#
filtbin=FilledInByPostinstall

showhelp=n
showfilters=n
replace=n

case "$1" in
-help|-usage)
	echo ""
	echo "filterps Version 1.0.1 - Adobe Systems conversion utility for documents"
	echo "with invalid DSC comments"
	echo ""
	showhelp=y
	showfilters=y
	exitstatus=0
	;;
*)
	case $# in
	1) in="$1"; out="$1" replace=y ;;
	2) in="$1"; out="$2" ;;
	*) echo "Invalid arguments."
	   showhelp=y; exitstatus=1; ;;
	esac
	;;
esac

case $showhelp in
n)
	if [ ! -r "$in" ] ; then
		echo "File $1 does not exist or is not readable."
		showhelp=y
		exitstatus=1
	fi

	if [ $replace = n -a -f "$out" -a ! -w "$out" ] ; then
		echo "File $out is not writable."
		showhelp=y
		exitstatus=1
	fi
	;;
esac

case $showhelp in
y)
	echo "Usage: filterps BadFile.ps GoodFile.ps"
	echo "       filterps BadFile.ps (replaces BadFile.ps)"
	echo "       filterps -help"
	case $showfilters in
	y)
	echo ""
	echo "Filters: filterps can currently convert documents generated by the"
	echo "         following programs:"
	echo ""
	echo "         FrameMaker"
	echo "            The FrameMaker document preparation package from Frame"
	echo "            Technology, version 2.0 and version 3.0"
	echo ""
	echo "         Asterix"
	echo "            A collection of document creation applications from"
	echo "            Applix, Inc."
	echo ""
	echo "         ditroff (device independent troff)"
	echo "            A troff-like utility that is sold and licensed by AT&T,"
	echo "            either alone or in the Documenter's Workbench package."
	echo ""
	echo "         dvi2ps"
	echo "            A utility that converts TeX documents into PostScript"
	echo "            language documents."
	echo ""
	;;
	esac

	exit $exitstatus
	;;
esac

filter=`awk '
BEGIN { filter = "none" }
/^\(dvi->PostScript Driver, / {
	filter = "filterps.fixDVI"
	exit
}
/^% Start of psdit.pro -- prolog for ditroff translator/ {
	filter = "filterps.fixDIT"
	exit
}
/^% FrameMaker PostScript Prolog 3.0, for use with FrameMaker 3.0/ {
	filter = "filterps.fixFM3.0"
	exit
}
/^% FrameMaker PostScript Prolog 2.0, for use with FrameMaker 2.0/ {
	filter = "filterps.fixFM2.0"
	exit
}
/^%%Creator: Aster\*x/ {
	filter = "filterps.fixAsterix"
	exit
}
END { print filter }
' "$in"`

case "$filter" in
""|none)
	echo "No filter being applied to $in"
	;;
*)
	filtprog=$filtbin/$filter
	if [ ! -x $filtprog ] ; then
		echo "Cannot find filter $filter"
		exit 1
	fi
	echo "Applying $filter to $in, output in $out... "
	case $replace in
	y)
		out="$out.TEMP"
		if $filtprog "$in" "$out" ; then
			if [ -s "$out" ] ; then
				mv "$out" "$in"
			else
				echo "no output from $filter"
				rm "$out"
			fi
		else
			echo ""
			echo "$filter error, $out not removed"
			exit 1
		fi
		;;
	n)
		$filtprog "$in" "$out"
		;;
	esac
	echo Done
	;;
esac
