#!/bin/sh
#
#  filterbatch
#
#  Copyright (c) 1989-1996 Adobe Systems, Incorporated. All rights reserved.
#
#  Script to run a filter in batch mode.
#
#  Method
#    If filterbatch is called with no arguments print a usage 
#    statement and exit.
#    Grab the name of the filter to be run
#    If the filter isn't installed - print usage and exit.
#    Grab the command line options
#    Grab the filenames to be filtered
#    For each file, call the filter with the command line options.


bindir=$FMHOME/bin
initdir=$FMHOME/fminit
options=' '
outputdir=.
filterlist="dcatomif iaftomif pict2mif trofftomif word2mif wptomif
""	is1totxt rm8totxt txttois1 txttorm8
""	miftowp miftoiaf miftortf
""	miftow4w w4wtomif
""	cdr2fmvect cgm2fmvect drw2fmvect dxf2fmvect gem2fmvect
""	hpgl2fmvect iges2fmvect pict2fmvect wmf2fmvect wpg2fmvect
""	fmimagetoimage"

# If filterbatch is called with no arguments print a usage statement and exit.
usage="filterbatch usage:
""    filterbatch [-d output-dir] [-s suffix] filter [options] file1...filen
""    The filter options are:"

dcatomif="dcatomif	-V"
iaftomif="iaftomif	-c -v -V -h -help"
pict2mif="pict2mif	-l -s -V -h -help"
trofftomif="trofftomif	[-man|-ms|-me]"
word2mif="word2mif	-d -V -h -help"
wptomif="wptomif	-V -h -help"
miftowp="miftowp	-V -h -help"
miftortf="miftortf      -V -h -help"
miftoiaf="miftoiaf -M -b -n -m -s -xx -x flowname -f flowname -c miftoiaf.fonts -p patterns -t type -V -h -help"
is1totxt="is1totxt";
rm8totxt="rm8totxt";
txttois1="txttois1";
txttorm8="txttorm8";
miftow4w="miftow4w format-key"
w4wtomif="w4wtomif format-key"

cdr2fmvect="cdr2fmvect			-V -h"
cgm2fmvect="cgm2fmvect			-V -h"
drw2fmvect="drw2fmvect			-V -h"
dxf2fmvect=" dxf2fmvect			-V -h"
gem2fmvect=" gem2fmvect			-V -h"
hpgl2fmvect="hpgl2fmvect 		-V -h"
iges2fmvect="iges2fmvect		-V -h"
pict2fmvect="pict2fmvect		-V -h"
wmf2fmvect="wmf2fmvect			-V -h"
wpg2fmvect="wpg2fmvect			-V -h"

fmimagetoimage="fmimagetoimage	-V -h -help"

while :; do
	case $1 in
	-d)	# -d outputdir
		shift
		outputdir=$1
		shift
		[ -z "$outputdir" -o -d "$outputdir" ] || {
			echo "No such directory:" $outputdir
			exit 1
		}
		;;
	-s)	# -s suffix (otherwise we choose a default based on output format)
		shift
		suf=$1
		shift
		case $suf in
		.*)
			;;
		*)
			suf=".$suf"
			;;
		esac
		;;
	*)
		break
		;;
	esac
done

# Grab the name of the filter to be run
filter=$1
shift

# If the filter isn't installed - print usage and exit.
[ -n "$filter" -a -x "$bindir/$filter" ] || {
	echo "$usage"
	for i in $filterlist
	do
		[ -x $FMHOME/bin/$i ] && eval "echo ' ' \$$i"
	done
	exit 1
}

# Grab the command line options
for arg do
	case $arg in
	-*)
		options="$options $arg"
		shift
		;;
	*)
		break
		;;
	esac
done

case $filter in
w4wtomif|miftow4w)
	options="$options $1"
	shift
	;;
esac

# For each file, call the filter with the command line options.
for file do
	noext=`echo $file | sed 's#.*/\(.*\)\.[^.][^.]*#\1#'`
	case $filter in
	*toimage)
		$bindir/$filter $options < $file > $outputdir/$noext${suf:-.image}
		;;
	*towp)
		$bindir/$filter $options < $file > $outputdir/$noext${suf:-.wp}
		;;
	*toiaf)
		$bindir/$filter $options < $file > $outputdir/$noext${suf:-.iaf}
		;;
	*tortf)
		$bindir/$filter $options < $file > $outputdir/$noext${suf:-.rtf}
		;;
	w4wtomif)
		$bindir/$filter $options $file $outputdir/$noext${suf:-.mif}
		;;
	*tomif)
		$bindir/$filter $options < $file > $outputdir/$noext${suf:-.mif}
		;;
	miftow4w)
		$bindir/$filter $options $file $outputdir/$noext${suf:-.w4w}
		;;
	*2mif)
		$bindir/$filter $options < $file > $outputdir/$noext${suf:-.mif}
		;;
	*2fmvect)
		$bindir/$filter $options $file $outputdir/$noext${suf:-.fmv}
		;;
	*txt)
		$bindir/$filter $file $outputdir/$noext${suf:-.txt}
		;;
	*is1)
		$bindir/$filter $file $outputdir/$noext${suf:-.is1}
		;;
	*rm8)
		$bindir/$filter $file $outputdir/$noext${suf:-.rm8}
		;;
	*)
		echo "Unknown filter:" $filter >&2
		exit 1
		;;
	esac
done
