#! /bin/sh

plat=i386-mingw32

if test $# -eq 0
then
    cat <<EOF
Usage:
	oztool c++ -c <cfile>
	oztool cc  -c <cfile>
	oztool ld  -o <target> <files>
	oztool platform
EOF
    exit 2
fi

cmd=$1
shift

case "$cmd" in
    c++)
	exec ${plat}-g++ -I${OZHOME}/include "$@"
	;;
    cc)
	exec ${plat}-gcc -I${OZHOME}/include "$@"
	;;
    platform)
	echo "win32-i486"
	exit 0
	;;
    ld)
	# Set up variables libname, libdirs, and args from command line
	while [ $# != 0 ]
	do
	    case "$1" in
		-o)
		    shift
		    libname=$1
		    ;;
		-s)
		    ;;   # just ignore it
		-lc)
		    ;;   # just ignore it
		-L*)
		    libdirs="$libdirs $1"
		    ;;
		*)
		    args="$args $1"
		    ;;
	    esac
	    shift
	done

	emulator_lib=libemulator.a
	defname=${libname}.def

	tail +2 ${OZHOME}/include/emulator.def > emulator0.def

	# Create the import stub library for emulator.dll
	${plat}-dlltool --as ${plat}-as \
			--def emulator0.def \
			--dllname emulator.dll \
			--output-lib ${emulator_lib} || exit 1
	# Create the library proper
	${plat}-dlltool --as ${plat}-as --output-def $defname \
			--dllname $libname $args || exit 1
	exec \
	${plat}-dllwrap --driver-name ${plat}-gcc \
			--dlltool-name ${plat}-dlltool \
			--as ${plat}-as \
			-s -o $libname --def $defname \
			--dllname $libname $args \
			${emulator_lib} \
			$libdirs
	;;
    *)
	echo "$0: unknown command $cmd" 1>&2
	exit 1
	;;
esac

