#!/usr/bin/make -f


ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

DEB_HOST_GNU_TYPE = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

ifeq (x86_64-linux-gnu,$(DEB_HOST_GNU_TYPE))
BUILD_TARGET=multilib
INSTALL_TARGET=install-multilib
else
BUILD_TARGET=all
INSTALL_TARGET=install
endif

build: build-stamp
build-stamp:
	dh_testdir
	./autogen.sh
	./configure --prefix=/usr
	make $(BUILD_TARGET)

	touch build-stamp

# this is really not part of debian package build, but just for convinience

clean: checkroot
	$(checkdir)
	rm -f build-stamp
	rm -rf .deps
	rm -f debian/substvars debian/files
	rm -rf debian/tmp obj obj-build obj64 objfake
	rm -f config.log config.status
	find -name '*.o'  | xargs rm -f
	find -name '*.a'  | xargs rm -f
	find -name '*.so' | xargs rm -f
	dh_clean

# Build architecture-independent files here.
binary-indep: build
# We have nothing to do by default.

# Build architecture-dependent files here.
install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	$(MAKE) prefix="$(CURDIR)/debian/scratchbox2/usr" $(INSTALL_TARGET)
	# make lintian happy
	rm -f debian/scratchbox2/usr/share/scratchbox2/host_usr
	ln -sf ../.. debian/scratchbox2/usr/share/scratchbox2/host_usr

binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a README
	dh_installchangelogs -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	# FIXME, do in postinst
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch

.PHONY: build clean binary-indep binary-arch binary checkroot prebuild

define checkdir
	dh_testdir
endef

checkroot:
	$(checkdir)
	dh_testroot
