#!/usr/bin/make -f
export DH_OPTIONS

PKG_DIR=$(shell pwd)
BUILD_DIR=$(CURDIR)/build-dir
INSTALL_DIR=$(PKG_DIR)/debian/tmp
DEB_BUILD_ARCH_OS := $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)

configure: configure-stamp
configure-stamp: debian/alsa-stamp
	dh_testdir
	mkdir $(BUILD_DIR)
	find . -name '*.o' -delete
	rm -f debian/kadu.install
	cp debian/kadu.install.noalsa debian/kadu.install
ifeq ($(DEB_BUILD_ARCH_OS),linux)
	cat debian/kadu.install.alsa >> debian/kadu.install
endif
	bash debian/external.sh add
	cd $(BUILD_DIR) && cmake -DLIB_SUFFIX_64=0 -DCMAKE_INSTALL_PREFIX=/usr $(PKG_DIR)
	touch configure-stamp

#Architecture
build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp
	cd $(BUILD_DIR) && $(MAKE)
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp
	touch $@

clean: 
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp debian/alsa-stamp
	rm -rf $(BUILD_DIR)
	cp -f debian/kadu.install.noalsa debian/kadu.install
	bash debian/external.sh remove
	dh_clean 

install: install-indep install-arch
install-indep:
	dh_testdir
	dh_testroot
	dh_prep -i 
	dh_installdirs -i
	cd $(BUILD_DIR) && $(MAKE) DESTDIR=$(INSTALL_DIR) install
	rm -f $(INSTALL_DIR)/usr/share/kadu/themes/sounds/ultr/license.txt
	rm -f $(INSTALL_DIR)/usr/share/kadu/themes/sounds/bns/sound.conf~
	rm -f $(INSTALL_DIR)/usr/share/kadu/themes/emoticons/tango/license.txt
	dh_install -i

install-arch:
	dh_testdir
	dh_testroot
	dh_prep -s 
	dh_installdirs -s
	cd $(BUILD_DIR) && $(MAKE) DESTDIR=$(INSTALL_DIR) install
	cp $(INSTALL_DIR)/usr/share/kadu/ChangeLog debian/kadu/usr/share/doc/kadu/about-changes-tab.txt
	rm -f $(INSTALL_DIR)/usr/share/kadu/modules/data/osd_hints/License
	dh_install -s

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_link
	dh_strip
	dh_compress -XAUTHORS -XTHANKS -XLICENSE -Xabout-changes-tab.txt
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb
# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

binary: binary-arch binary-indep

debian/alsa-stamp:
ifeq ($(DEB_BUILD_ARCH_OS),linux)
	sed -i s/module_alsa_sound=n/module_alsa_sound=m/ $(PKG_DIR)/.config
else
	sed -i s/module_alsa_sound=m/module_alsa_sound=n/ $(PKG_DIR)/.config
endif
	touch $@

.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch configure
