#!/bin/sh
#
#  filterbatch
#
#  Copyright (c) 1989-1993 Frame Technology Corp. 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 wordtomif trofftomif wptomif miftowp \
	c960toepsi c960toimage c960tomif igestoepsi igestoimage igestomif \
	dxftoepsi dxftoimage dxftomif hpgltoepsi hpgltoimage hpgltomif \
	cgmtomif epsitoimage g4toimage imagetog4 picttomif"

# If filterbatch is called with no arguments print a usage statement and exit.
usage="filterbatch usage:
    filterbatch [-d output-dir] filter [options] file1...filen 
    The filter options are:"
    
dcatomif="dcatomif	-V"
iaftomif="iaftomif	-c -v -V -h -help"
wordtomif="wordtomif	-d -V -h -help"
trofftomif="trofftomif	[-man|-ms|-me]"
wptomif="wptomif	-V -h -help"
miftowp="miftowp	-V -h -help"
c960toepsi="c960toepsi	-f -v -V -h -help"
c960toimage="c960toimage	-v -V -h -help"
c960tomif="c960tomif	-f -v -V -h -help"
igestoepsi="igestoepsi	-f -v -V -h -help"
igestoimage="igestoimage	-c -v -V -h -help"
igestomif="igestomif	-f -v -V -h -help"
dxftoepsi="dxftoepsi	-f -v -V -h -help"
dxftoimage="dxftoimage	-c -v -V -h -help"
dxftomif="dxftomif	-f -v -V -h -help"
hpgltoepsi="hpgltoepsi	-f -s -v -V -h -help"
hpgltoimage="hpgltoimage	-s -v -V -h -help"
hpgltomif="hpgltomif	-f -s -v -V -h -help"
cgmtomif="cgmtomif	-M -s w,h -f fontfile -p picture -d -u unit -V"
epsitoimage="epsitoimage	-V"
g4toimage="g4toimage	-rawin -width n -height n -standard -V -help"
imagetog4="imagetog4	-docid docname -figid string -notes string -orient c -rawout -dpi n -srcgph string -V -help"
picttomif="picttomif	-l -s -V -h -help"

[ $# = 0 ] && {
   echo "$usage"
   for i in $filterlist
   do
      [ -x $FMHOME/bin/$i ] && eval "echo '	' \$$i"
   done
   exit 1
}

# See if the outputdir was specified on the command line.
[ "$1" = "-d" ] && {
   shift
   outputdir="$1"
   shift
   [ ! -d  "$outputdir" ] && {
      echo "No such directory:" $outputdir
      exit 1
   }
}

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

# If the filter isn't installed - print usage and exit.
[ ! -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 in "$@"
do
  case $arg in
    -*) options="$options $arg"
        shift ;;
     *) break ;;
  esac
done

# Grab the filenames to be filtered
filelist="$*"

# For each file, call the filter with the command line options.
[ -z "$outputdir" ] && outputdir=.
for file in $filelist
do
   name=`basename $file`
   noext=`echo $name | sed 's/\.[^.][^.]*$//'`
   case $filter in
      *toepsi) $bindir/$filter $options < $file > $outputdir/$noext.epsi ;;
      *toimage) $bindir/$filter $options < $file > $outputdir/$noext.image ;;
      *tog4) $bindir/$filter $options < $file > $outputdir/$noext.g4 ;;
      *towp) $bindir/$filter $options < $file > $outputdir/$noext.wp ;;
      *tomif) $bindir/$filter $options < $file > $outputdir/$noext.mif ;;
      *) echo "Unknown filter:" $filter; exit 1 ;;
   esac
done
