#!/usr/bin/make -f

# Set temporary HOME for lazarus primary config directory
export HOME=$(CURDIR)/tmphome

%:
	dh $@

override_dh_install:
	cd language ;\
		if [ -f doublecmd.po ] ; then mv doublecmd.po doublecmd.en.po; fi ;

	# Remove convenience copy of Free Pascal Qt4 binding, use libqt4pas-dev instead
	rm -f plugins/wlx/WlxMplayer/src/qt4.pas

	# Build GTK2 version
	./build.sh beta gtk2
	./install/linux/install.sh --install-prefix=$(CURDIR)/debian/doublecmd-common
	./clean.sh

	# Build Qt4 version
	./build.sh beta qt
	./install/linux/install.sh --install-prefix=$(CURDIR)/debian/doublecmd-qt-temp
	./clean.sh

	# Separate GTK2-specific files
	mkdir -p $(CURDIR)/debian/doublecmd-gtk/usr/lib/doublecmd/plugins
	mv $(CURDIR)/debian/doublecmd-common/usr/lib/doublecmd/doublecmd $(CURDIR)/debian/doublecmd-gtk/usr/lib/doublecmd/
	mv $(CURDIR)/debian/doublecmd-common/usr/lib/doublecmd/plugins/wlx $(CURDIR)/debian/doublecmd-gtk/usr/lib/doublecmd/plugins/
	mkdir -p $(CURDIR)/debian/doublecmd-gtk-dbg/usr/lib/doublecmd
	mv $(CURDIR)/debian/doublecmd-common/usr/lib/doublecmd/doublecmd.zdli $(CURDIR)/debian/doublecmd-gtk-dbg/usr/lib/doublecmd/

	# Separate Qt4-specific files
	mkdir -p $(CURDIR)/debian/doublecmd-qt/usr/lib/doublecmd/plugins
	mv $(CURDIR)/debian/doublecmd-qt-temp/usr/lib/doublecmd/doublecmd $(CURDIR)/debian/doublecmd-qt/usr/lib/doublecmd/
	mv $(CURDIR)/debian/doublecmd-qt-temp/usr/lib/doublecmd/plugins/wlx $(CURDIR)/debian/doublecmd-qt/usr/lib/doublecmd/plugins/
	mkdir -p $(CURDIR)/debian/doublecmd-qt-dbg/usr/lib/doublecmd
	mv $(CURDIR)/debian/doublecmd-qt-temp/usr/lib/doublecmd/doublecmd.zdli $(CURDIR)/debian/doublecmd-qt-dbg/usr/lib/doublecmd/
	rm -rf $(CURDIR)/debian/doublecmd-qt-temp/

	# Separate plugins
	mkdir -p $(CURDIR)/debian/doublecmd-plugins/usr/lib/doublecmd
	mv $(CURDIR)/debian/doublecmd-common/usr/lib/doublecmd/plugins $(CURDIR)/debian/doublecmd-plugins/usr/lib/doublecmd

	# Clean up common files
	rm -rf $(CURDIR)/debian/doublecmd-common/usr/share/doublecmd/doc
	find $(CURDIR)/debian/doublecmd-common/usr/share/ -type f | xargs chmod a-x ;

	dh_install

override_dh_strip:
	# Strip plugins because dh_strip cannot handle non-standard extensions (bug #35733)
	find $(CURDIR)/debian/doublecmd-*/usr/lib/doublecmd/plugins/ -name '*.w?x' -o -name '*.dsx' | \
		xargs strip --remove-section=.comment --strip-unneeded ;
	dh_strip

override_dh_clean:
	./clean.sh

	cd language ;\
		if [ -f doublecmd.en.po ] ; then mv doublecmd.en.po doublecmd.po; fi ;
	# Clean up temporary HOME
	rm -rf $(CURDIR)/tmphome
	dh_clean
