#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
gles2_architectures := armel armhf

CORE_ABIVERSION := $(shell sed -rn 's/^\#define[[:space:]]+CORE_ABIVERSION[[:space:]]+//p' include/core/abiversion.h )

DEFAULT_PLUGINS = "ccp"

override_dh_quilt_patch:
	dh_quilt_patch
	if test -r debian/patches/series.$(DEB_HOST_ARCH); then \
	  pc=".pc.$(DEB_HOST_ARCH)"; \
	  test -d "$(CURDIR)/$$pc" || mkdir "$(CURDIR)/$$pc"; \
	  cp debian/patches/series.$(DEB_HOST_ARCH) $(CURDIR)/$$pc/series; \
	  cd $(CURDIR); \
	  QUILT_PC="$$pc" quilt upgrade || true; \
	  QUILT_PC="$$pc" QUILT_PATCHES="debian/patches/" quilt push -a || true; \
	fi; \

override_dh_quilt_unpatch:
	if test -r debian/patches/series.$(DEB_HOST_ARCH); then \
	  pc=".pc.$(DEB_HOST_ARCH)"; \
	  cd $(CURDIR); \
	  QUILT_PC="$$pc" QUILT_PATCHES="debian/patches/" quilt pop -a || true; \
	fi; \
	dh_quilt_unpatch

override_dh_auto_configure:
	# currently, segfault if CMAKE_BUILD_TYPE=Release
	LDFLAGS=$(shell echo $$LDFLAGS | sed -e 's/\-Wl,-Bsymbolic-functions //')
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(gles2_architectures)))
	dh_auto_configure -- -DCOMPIZ_BUILD_WITH_RPATH=FALSE -DCOMPIZ_DEFAULT_PLUGINS=\"$(DEFAULT_PLUGINS)\" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCOMPIZ_PACKAGING_ENABLED=TRUE -DUSE_GSETTINGS=OFF -DCOMPIZ_DISABLE_GS_SCHEMAS_INSTALL=ON -DCOMPIZ_DISABLE_PLUGIN_KDE=ON -DCOMPIZ_BUILD_TESTING=OFF -DUSE_KDE4=OFF -DBUILD_GLES=ON
else
	dh_auto_configure -- -DCOMPIZ_BUILD_WITH_RPATH=FALSE -DCOMPIZ_DEFAULT_PLUGINS=\"$(DEFAULT_PLUGINS)\" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCOMPIZ_PACKAGING_ENABLED=TRUE -DUSE_GSETTINGS=OFF -DCOMPIZ_DISABLE_GS_SCHEMAS_INSTALL=ON -DCOMPIZ_DISABLE_PLUGIN_KDE=ON -DCOMPIZ_BUILD_TESTING=OFF -DUSE_KDE4=OFF
endif

override_dh_install:
	# Language packs
	for d in $$(find debian/tmp -type f \( -name "*.desktop" -o -name "*.directory" \) ); do \
		sed -ri '/^(Name|GenericName|Comment|X-GNOME-FullName)\[/d' $$d; \
		echo "X-Ubuntu-Gettext-Domain=compiz" >> $$d; \
	done;
	#for d in $$(find debian/tmp -type f -name "*.schemas" ); do \
	#		GETTEXT_DOMAIN="compiz" perl /usr/lib/cdbs/strip-schema.pl $$d > $$d.new; mv $$d.new $$d; \
	#done;

	# create compiz keybindings file based on the metacity ones.
	mkdir -p $(CURDIR)/debian/tmp/usr/share/gnome-control-center/keybindings/
	# keep package=metacity to get the right translation :)
	for i in launchers navigation screenshot system windows ; do \
	    sed 's/wm_name=\"Metacity\"/wm_name=\"Compiz\"/' /usr/share/gnome-control-center/keybindings/50-metacity-$${i}.xml > $(CURDIR)/debian/tmp/usr/share/gnome-control-center/keybindings/50-compiz-$${i}.xml ; \
    done
	# add some selected keys
	sed -i 's#key=\"/apps/metacity/general/num_workspaces\" comparison=\"gt\"##g' $(CURDIR)/debian/tmp/usr/share/gnome-control-center/keybindings/50-compiz-navigation.xml
    # TODO: checking and bending those key to a compiz equivalent (or move the blessed option to g-c-c)
	#sed -i 's#description="Toggle magnifier">#description="Toggle magnifier">/apps/compiz-1/plugins/scale/screen0/options/initiate_key#g' $(CURDIR)/debian/tmp/usr/share/gnome-control-center/keybindings/50-compiz-accessibility.xml
	#sed -i 's#description="Magnifier zoom in">#description="Magnifier zoom in">/apps/compiz-1/plugins/ezoom/screen0/options/zoom_in_key#g' $(CURDIR)/debian/tmp/usr/share/gnome-control-center/keybindings/50-compiz-accessibility.xml
	#sed -i 's#description="Magnifier zoom out">#description="Magnifier zoom out">/apps/compiz-1/plugins/ezoom/screen0/options/zoom_out_key#g' $(CURDIR)/debian/tmp/usr/share/gnome-control-center/keybindings/50-compiz-accessibility.xml

	# cmake findcompiz_install use COMPIZ_DESTDIR and using COMPIZ_DESTDIR and
	# DESTDIR together is completely broken upstream
	# So, doing the same in debian/rules to find a way to install things in the
	# right place, sorry for the kittens
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), $(gles2_architectures)))
	cmake_dir=`cmake --system-information 2> /dev/null | grep "^CMAKE_ROOT " | sed -e 's/.*"\(.*\)"/\1/'` && \
	mkdir -p debian/tmp$${cmake_dir}/Modules && \
	cp cmake/FindCompiz.cmake debian/tmp$${cmake_dir}/Modules && cp cmake/FindOpenGLES2.cmake debian/tmp$${cmake_dir}/Modules
else
	cmake_dir=`cmake --system-information 2> /dev/null | grep "^CMAKE_ROOT " | sed -e 's/.*"\(.*\)"/\1/'` && \
	mkdir -p debian/tmp$${cmake_dir}/Modules && \
	cp cmake/FindCompiz.cmake debian/tmp$${cmake_dir}/Modules
endif

	# remove .a files
	rm -f debian/tmp/usr/lib/*.a

	dh_install --fail-missing

override_dh_gencontrol:
	dh_gencontrol -- -Vcoreabiversion=$(CORE_ABIVERSION)

override_dh_auto_test:
	# test broken right now :(

%:
	dh --with quilt $@

