#!/bin/sh

gencommon () {
	lint="build/${s%.*}${1}.l"
	dep="build/${s%.*}${1}.d"
	obj="build/${s%.*}${1}"

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

genlink () {
	gencommon ".ld"
	echo "	\$(GENLINK) $< | \$(STRIPLINK) | \$(KERN_INFO) > \$@"\
		>> deps.mk;
}

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

	echo "${lint}: ${s}"			>> deps.mk
	echo "	\$(LINT) -c \$< -o /dev/null"	>> deps.mk
}

case "${1}" in
	--compile)
		suffix=.o
		func=genrule
		;;
	--link)
		suffix=.ld
		func=genlink
esac

# create all subdirectories
mkdir -p $(echo "${2}" | xargs dirname | uniq | sed 's|^|build/|g')

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