#!/bin/sh -e
redo-ifchange config/cxx config/cxxflags config/cppflags
read cxx < config/cxx
read cxxflags < config/cxxflags
read cppflags < config/cppflags
cxxbase=$(basename "${cxx}")
if [ "${cxx}" = "owcc.exe" ]
then
	#echo ${cxx} ${cppflags} ${cxxflags} -o "$1" -c "$2" -MMD 1>&2
	${cxx} ${cxxflags} ${cppflags} -o "$1" -c "$2" -MMD
	obj="`echo $3|sed -e 's/\\\\/\//g'`"
	obj="`basename ${obj}`"
	sed -e "s/^.* *: *//" -e 's/\\$//g' -e 's/\\/\\\\/g' "${obj}" | xargs -r redo-ifchange
elif test -x ../command/${cxxbase} && test -d ../command/../include
then
	#echo ${cxx} ${cppflags} ${cxxflags} -o "$1" -c "$2" 1>&2
	${cxx} ${cppflags} ${cxxflags} -o "$1" -c "$2"
	extra="-ffull-errors -fonly-dump -fallow-macro-redefinitions -fdisable-directives -Wno-function-macro-wrong-context -Wnon-standard-feature-tests -fno-intrinsics"
	case "`uname`" in
	(Linux)	;;
	(NetBSD)	extra="${extra} -fno-builtins -nonativeinc" ;;
	(FreeBSD)	extra="${extra} -fno-builtins -nonativeinc" ;;
	(*BSD)	;;
	(*)	;;
	esac
	echo ../command/${cxxbase} -E ${cppflags} "$2" -MD -MF "$3" ${extra} 1>&2
	../command/${cxxbase} -E ${cppflags} "$2" -MD -MF "$3" ${extra}
	. ./"$3" || true
	redo-ifdelete -- ../command/${cxxbase} ../command/../include
else
	test -x ../command/${cxxbase} || redo-ifcreate ../command/${cxxbase}
	test -d ../command/../include || redo-ifcreate ../command/../include
	#echo ${cxx} ${cppflags} ${cxxflags} -o "$1" -c "$2" -MMD -MF "$3" 1>&2
	${cxx} ${cppflags} ${cxxflags} -o "$1" -c "$2" -MMD -MF "$3"
	sed -e "s!^$1: *!!" -e 's!\\$!!g' "$3" | xargs -r redo-ifchange
fi
