#!/bin/sh

# Make sure what mode we're in. The basic cc wrapper does a lot.
ARGS=$*
USEMODE=0
for i in $ARGS; do
	case "${i}" in
		-o)
			if [ $USEMODE != 2 ]; then
				# Link
				USEMODE=1
			fi
		;;
		-c | -S | -E)
			# Partial compile
			USEMODE=2
		;;
		conftest.c)
			# Used in autoconf... we really probably need
			# a better way to do this. (scan for any .c?)
			USEMODE=1
		;;
		*)
		;;
	esac
done

case $USEMODE in
	0)
		# Unknown mode ... use default args and cross fingers.
		if [ x${KOS_WRAPPERS_VERBOSE} = x"1" ]; then
			echo ${KOS_CC} "$@"
		fi
		exec ${KOS_CC} "$@"
	;;

	1)
		# In link mode, add all the extra processing.
		if [ x${KOS_WRAPPERS_VERBOSE} = x"1" ]; then
			echo ${KOS_CC} ${KOS_CFLAGS} ${KOS_LDFLAGS} ${KOS_START} "$@" ${KOS_LIBS}
		fi
		exec ${KOS_CC} ${KOS_CFLAGS} ${KOS_LDFLAGS} ${KOS_START} "$@" ${KOS_LIBS}
	;;

	2)
		# In non-link mode, just go ahead and run CC.
		if [ x${KOS_WRAPPERS_VERBOSE} = x"1" ]; then
			echo ${KOS_CC} ${KOS_CFLAGS} "$@"
		fi
		exec ${KOS_CC} ${KOS_CFLAGS} "$@"
	;;
esac
