#!/bin/sh

gencommon () {
	dep="build/${s%.*}${1}.d"
	obj="build/${s%.*}${1}"
	
	mkdir -p $(dirname ${dep})

	echo "${dep}:"		>> deps.mk
	echo "include ${dep}"	>> deps.mk
	echo "${obj}: ${s}"		>> deps.mk

}
genlink () {
	gencommon ".ld"
	if [ "${init}" = "0" ] ; then
		echo "	\$(GENLINK) $< | \$(STRIPLINK) > \$@"\
		>> deps.mk;
	else
		echo "	\$(GENLINK) $< | \$(STRIPLINK) | \$(KERN_INFO) > \$@"\
		>> deps.mk;
	fi

}

genrule () {
	gencommon "${1}"
	echo "	\$(COMPILE) ${defs} -c \$< -o \$@"	>> deps.mk
}

case "${1}" in
	--kern)
		suffix=.k.o
		defs=-DKERNEL
		func=genrule
		;;
	--init)
		suffix=.i.o
		defs=-DINIT
		func=genrule
		;;
	--init-link)
		suffix=.ld
		init=1
		func=genlink
		;;

	--kern-link)
		suffix=.ld
		init=0
		func=genlink
esac

for s in ${2}
do
	${func} ${suffix}
	echo ${obj}
done
