#!/bin/sh
#	$5/w4wtomif 0071 source-file dest-file
case " $FMDEBUG " in
*' import '*)
	echo "-> $0 $*" >&2
	set -x
	;;
esac

: ${3:?"Usage: $0 format-key source-file dest-file"} ${FMHOME:?} ${FMARCH:?}
rawcode=$1 ss=$2 dd=$3
set -- `echo $1 | sed 's/\([^ ]\)/\1 /g'`
case $1 in
0)
	fc=$2$3 ver=$4
	;;
*)
	fc=$1$2$3 ver=$4
	;;
esac

TF=${TMPDIR:=$HOME}/w4w$$
ifilt=$FMHOME/bin/$FMARCH/w4w${fc}f
tomif=$FMHOME/bin/$FMARCH/w4w42t
# pass through formats:
#	502=TIFF 512=gif 521=jfif 602=pict 603=cgm 606=wpg1 610=wmf 615=wpg
fswit="-grr -gtf502 -gtf512 -gtf521 -gtf602 -gtf603 -gtf610 -gtf615"
mifver=2	# write mifver 2 (FrameMaker 5)
[ -x $ifilt -a -x $tomif ] || exit 32
case $rawcode in
AW4W049m)	# Word 6.0 for Macintosh
	ver=-v0m
	;;
esac

# exit code 12 is only a warning.
# Can't use Shell functions here; some shells (in 1997!) still maul
# caller's $@
(
	PATH="$FMHOME/bin/$FMARCH:$PATH" $ifilt "$ss" -v$ver -n -t=$TF $fswit
	rc=$?
	[ $rc -eq 12 ] && {
		echo "Warning: $ifilt detected unprocessed graphics in $ss." >&2
		rc=0
	}
	exit $rc
) && (
	PATH="$FMHOME/bin/$FMARCH:$PATH" $tomif "$dd" -v$mifver -n -t=$TF
	rc=$?
	[ $rc -eq 12 ] && {
		echo "Warning: $ifilt detected unprocessed graphics in $dd." >&2
		rc=0
	}
	exit $rc
)
rc=$?

case " $FMDEBUG " in
*' import '*)
	cp "$dd" $TMPDIR/FM$$_preserve.mif
	;;
esac

exit $rc
