include ./$(CONFIG)

build:
	$(MAKE) -C meta/toolchain    CONFIG=$(CONFIG) cache
	$(MAKE) -C meta/toolchain    CONFIG=$(CONFIG) build
	$(MAKE) -C meta/device_tools CONFIG=$(CONFIG) arch-tools device-tools

clean-toolchain:
	$(MAKE) -C meta/toolchain    CONFIG=$(CONFIG) clean
	

all-sums:
	$(MAKE) -C meta/toolchain	CONFIG=$(CONFIG) all-sums


build-static:
	$(MAKE) -C meta/device_tools CONFIG=$(CONFIG) build-sbrsh-static

ifdef COMPILER_PACKAGE

CONFLICTS = "scratchbox-devicetools"

ifdef COMPILER_PACKAGE_REPLACES
REPLACES  = $(CONFLICTS)
else
REPLACES  = $(CONFLICTS), $(COMPILER_PACKAGE_REPLACES)
endif

VERSION   ?= $(shell head -n1 /scratchbox/etc/scratchbox-version)
DATE      = $(shell head -n2 /scratchbox/etc/scratchbox-version | tail -n1)
ARCH      = $(shell dpkg-architecture -qDEB_BUILD_ARCH || uname -i)
CONTENTS  = /scratchbox/compilers/$(COMPILER_NAME)/ \
            /scratchbox/device_tools/*/$(COMPILER_NAME)/

all-packages: tarball deb

tarball:
	rm -f ../$(COMPILER_PACKAGE)-$(VERSION)-$(ARCH).tar.gz
	tar cfz ../$(COMPILER_PACKAGE)-$(VERSION)-$(ARCH).tar.gz --owner=root --group=root $(CONTENTS)

deb:
	sed -e "s/<NAME>/$(COMPILER_NAME)/" \
	    -e "s/<PACKAGE>/$(COMPILER_PACKAGE)/" \
	    -e "s/<REPLACES>/$(REPLACES)/" \
	    -e "s/<CONFLICTS>/$(CONFLICTS)/" \
	    debian/control.in > debian/control

	sed -e "s/<VERSION>/$(VERSION)/" \
	    -e "s/<DATE>/$(DATE)/" \
	    debian/changelog.in > debian/changelog

	cp COPYRIGHT debian/copyright

	CONFIG=$(CONFIG) dpkg-buildpackage -rfakeroot -b -uc

clean:
	[ ! -e debian/control ] || fakeroot debian/rules clean
	rm -f debian/control
	rm -f debian/copyright
	rm -f debian/changelog

endif

.PHONY: build build-static packages tarball deb clean

