#!/bin/sh
#	$5/miftow4w 0071 source-file dest-file
case " $FMDEBUG " in
*' export '*)
	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:=/tmp}/w4w$$
ofilt=$FMHOME/bin/$FMARCH/w4w${fc}t
frommif=$FMHOME/bin/$FMARCH/w4w42f
# Install -gtf flags here
fswit=
mifver=2	# read mif version 2
[ -x $ofilt -a -x $frommif ] || exit 32
case $rawcode in
AW4W049m)	# Word 6.0 for Macintosh
	ver=0m
	;;
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" $frommif "$ss" -v$mifver -n -t=$TF $fswit
	rc=$?
	[ $rc -eq 12 ] && {
		echo "Warning: $frommif detected unprocessed graphics in $ss." >&2
		rc=0
	}
	exit $rc
) && (
	PATH="$FMHOME/bin/$FMARCH:$PATH" $ofilt "$dd" -v$ver -n -t=$TF
	rc=$?
	[ $rc -eq 12 ] && {
		echo "Warning: $ofilt detected unprocessed graphics in $dd." >&2
		rc=0
	}
	exit $rc
)
rc=$?

case " $FMDEBUG " in
*' export '*)
	cp $ss ${TMPDIR:-/tmp}/w4wpreserve_$$.mif
	echo "$0 done." >&2
	;;
esac

exit $rc
