#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)
LDFLAGS2:=-Wl,-z,defs  -Wl,-as-needed -Wl,--no-undefined

# This has to be exported to make some magic below work.
export DH_OPTIONS

BUILD_TREE=$(CURDIR)/build-tree

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Perform an out-of-tree build to keep a clean source tree
	rm -rf $(BUILD_TREE)
	mkdir $(BUILD_TREE)
	cd $(BUILD_TREE); cmake .. \
		-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
		-DCMAKE_INSTALL_PREFIX:STRING="/usr" \
		-DLibDA_AUTODETECT_PLUGINS:BOOL=OFF \
		-DLibDA_PLUGIN_ALSA:BOOL=ON \
		-DLibDA_PLUGIN_GSTREAMER:BOOL=ON \
		-DLibDA_PLUGIN_JACK:BOOL=ON \
		-DLibDA_PLUGIN_PORTAUDIO:BOOL=ON \
		-DLibDA_PLUGIN_PULSE:BOOL=ON \
		-DLibDA_PLUGIN_TESTING:BOOL=OFF \
		-DCMAKE_C_FLAGS:STRING="$(CFLAGS)" \
		-DCMAKE_CXX_FLAGS:STRING="$(CXXFLAGS) $(CPPFLAGS)" \
		-DCMAKE_SKIP_INSTALL_RPATH:BOOL=ON \
		-DCMAKE_EXE_LINKER_FLAGS:STRING="$(LDFLAGS) $(LDFLAGS2)" \
		-DCMAKE_MODULE_LINKER_FLAGS:STRING="$(LDFLAGS) $(LDFLAGS2)" \
		-DCMAKE_SHARED_LINKER_FLAGS:STRING="$(LDFLAGS) $(LDFLAGS2)"
	touch $@

build-stamp: configure-stamp 
	dh_testdir
	cd $(BUILD_TREE); $(MAKE)
	touch $@

build-indep: build-stamp

build-arch: build-stamp

build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	if [ -d $(BUILD_TREE) ]; then cd $(BUILD_TREE); $(MAKE) clean; fi
	rm -fr $(BUILD_TREE)
	$(MAKE) -C debian/h2m clean
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	cd $(BUILD_TREE); $(MAKE) install DESTDIR=$(CURDIR)/debian/performous
	$(MAKE) -C debian/h2m
	cp themes/default/icon.png $(CURDIR)/debian/performous/usr/share/pixmaps/performous.png
	mkdir -p  $(CURDIR)/debian/performous/usr/share/games/ultrastar/songs
	mv $(CURDIR)/debian/performous/usr/bin $(CURDIR)/debian/performous/usr/games

	# Remove development stuff
	rm -rf $(CURDIR)/debian/performous/usr/include
	rm -rf $(CURDIR)/debian/performous/usr/lib/*.a

	# Move tools to their own package
	mkdir -p $(CURDIR)/debian/performous-tools/usr/games
	cd $(CURDIR)/debian/performous/usr/games/ && \
		for i in ss_adpcm_decode ss_cover_conv ss_ipu_conv ss_ipu_decode \
			ss_archive_extract ss_chc_decode ss_pak_extract ss_extract \
			gh_fsb_decrypt gh_xen_decrypt itg_pck; do \
		mv $(CURDIR)/debian/performous/usr/games/"$$i" $(CURDIR)/debian/performous-tools/usr/games/ ; \
		done

# Build architecture independant packages using the common target.
binary-indep:
	dh_testdir
	dh_testroot
	dh_install -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_makeshlibs -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i -- -Zxz

# Build architecture dependant packages using the common target.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installmenu -a
	dh_installman -a
	dh_install -a
	dh_icons -a
	dh_link -a
	dh_strip --dbg-package=performous-dbg
	dh_compress -a 
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a \
		-Xusr/lib/performous/libda-1/
	dh_shlibdeps -a \
		-Xusr/lib/performous/libda-1/libda.a \
		-Xusr/lib/performous/libda-1/libda_audio_dev_gst.so \
		-Xusr/lib/performous/libda-1/libda_audio_dev_jack.so \
		-Xusr/lib/performous/libda-1/libda_audio_dev_pa19.so \
		-Xusr/lib/performous/libda-1/libda_audio_dev_pulse.so \
		-- -dRecommends -palsalibs
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a -- -Zxz

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