# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.

#
## Default target
#

default::

# Get the variables definition
include $(ZK_ROOT)/freedom/client/makefiles/Makefile.vars
ZK_INCS :=
ZK_DEFS :=

# Override and define new variables
file_list = $(wildcard $(dir)/*)
distdirs := $(wildcard ./*-*)
archs := $(foreach dir, $(distdirs), $(file_list))
modname := clientshim
outobjs := $(patsubst %, $(modname)_%.o, $(subst ._,, $(subst /,_,$(archs))))
tarballs := $(wildcard $(ZK_ROOT)/kernel/linux/*.tgz)
distros := $(patsubst $(ZK_ROOT)/kernel/linux/%.tgz, %, $(tarballs))

# Get the delegation rules
include $(ZK_ROOT)/freedom/makefiles/Makefile.targs

$(outobjs):
	@(\
		DISTVER=`echo $(patsubst clientshim_%.o, %, $@) | cut -f 1 -d '_' -`; \
		KERNVER=`echo $(patsubst clientshim_%.o, %, $@) | cut -f 2 -d '_' -`; \
		KERNBASEVER=`echo $$KERNVER | cut -f 1 -d '-' -`; \
		echo -e "\033[33mBuilding $@ $$DISTVER $$KERNBASEVER $$KERNVER\033[m" ;\
		ln -sf ../../../../src ./$$DISTVER/$$KERNVER/clientshim ;\
                ln -sf ../../../generic/Makefile.arch ./$$DISTVER/$$KERNVER/Makefile.arch; \
                ln -sf ../../../generic/$$KERNBASEVER/Rules.make ./$$DISTVER/$$KERNVER/Rules.make; \
                ln -sf ../../../generic/$$KERNBASEVER/arch ./$$DISTVER/$$KERNVER/arch; \
                ln -sf ../../../generic/$$KERNBASEVER/scripts ./$$DISTVER/$$KERNVER/scripts; \
		if [ -f ./$$DISTVER/$$KERNVER/Rules.extra ]; then \
			RULES_EXTRA=`cat ./$$DISTVER/$$KERNVER/Rules.extra`; \
		fi ; \
		make -C ./$$DISTVER/$$KERNVER/ -f Makefile.arch DISTVER=$$DISTVER KERNVER=$$KERNVER OUTOBJ=$@ $$RULES_EXTRA SUBDIRS=clientshim modules ; \
                rm -f ./$$DISTVER/$$KERNVER/Makefile.arch; \
		rm -f ./$$DISTVER/$$KERNVER/clientshim/ ; \
                rm -f ./$$DISTVER/$$KERNVER/Rules.make; \
                rm -f ./$$DISTVER/$$KERNVER/arch; \
                rm -f ./$$DISTVER/$$KERNVER/scripts; \
	)

$(wildcard */*): $(patsubst %, $(modname)_%.o, $(subst ._,, $(subst /,_,$@)))
	make $(patsubst %, $(modname)_%.o, $(subst ._,, $(subst /,_,$@)))


package-recurse: $(outobjs)

$(tarballs):
	tar xzf $@ 
	make package-recurse
	@rm -rf $(patsubst $(ZK_ROOT)/kernel/linux/%.tgz, %, $@)

$(distros): 
	make $(ZK_ROOT)/kernel/linux/$@.tgz

package:: $(tarballs)

clean::
	-rm -f $(ZK_BUILDDIR_PUBBIN)/clientshim_*.o
	-rm -f $(ZK_BUILDDIR_PROJ)/drv/*

.PHONY: $(wildcard */*)
.PHONY: $(outobjs)
.PHONY: package package-recurse $(tarballs) $(distros)

