#!/usr/bin/make -f

DEB_PYTHON_SYSTEM = $(if $(wildcard /usr/bin/dh_python2),,pysupport)
DEB_PYTHON2_MODULE_PACKAGES=tde-guidance-trinity guidance-backends-trinity
PYSUPPORT_INST_PATH = /opt/trinity/share/python-support
GUIDANCE_INST_PATH = $(PYSUPPORT_INST_PATH)/tde-guidance-trinity
export EXTRA_MODULE_DIR=$(GUIDANCE_INST_PATH)

include /usr/share/cdbs/1/rules/debhelper.mk
ifeq (pysupport, $(DEB_PYTHON_SYSTEM))
  include debian/python-distutils-jaunty.mk
else
  include /usr/share/cdbs/1/class/python-distutils.mk
endif
#include /usr/share/cdbs/1/rules/patchsys-quilt.mk
include /usr/share/cdbs/1/rules/utils.mk

DEB_DH_INSTALL_ARGS = --sourcedir=debian/tmp
DEB_UPDATE_RCD_PARAMS := multiuser
DEB_INSTALL_MANPAGES_tde-guidance-trinity := debian/mountconfig-trinity.1  debian/serviceconfig-trinity.1  debian/userconfig-trinity.1
PYSUPPORT_PATH = $(DEB_DESTDIR)$(PYSUPPORT_INST_PATH)
DEB_CONFIGURE_EXTRA_FLAGS := --prefix=/opt/trinity --with-extra-libs=/opt/trinity/lib --with-extra-includes=/opt/trinity/include/tde
LDFLAGS_APPEND := -L/opt/trinity/lib

DEB_DH_BUILDDEB_ARGS += -- -Z$(shell dpkg-deb --help | grep -q ":.* xz[,.]" \
                               && echo xz || echo bzip2)

post-patches::
	# make sure that python binary exists
	# since the existing code is still dependent on python2
	# and cdbs uses python as the default binary name
	test -x /usr/bin/python || ln -s python2 /usr/bin/python

install/tde-guidance-trinity::
	# install icons to right place
	install -D -p -m0644 $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/pics/hi32-app-daemons.png \
		$(DEB_DESTDIR)/opt/trinity/share/icons/crystalsvg/32x32/apps/daemons.png
	install -D -p -m0644 $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/pics/kcmpartitions.png \
		$(DEB_DESTDIR)/opt/trinity/share/icons/crystalsvg/32x32/apps/disksfilesystems.png
	install -D -p -m0644 $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/pics/hi32-user.png \
		$(DEB_DESTDIR)/opt/trinity/share/icons/crystalsvg/32x32/apps/userconfig.png
	install -D -p -m0644 $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/pics/32-wine.png \
		$(DEB_DESTDIR)/opt/trinity/share/icons/crystalsvg/32x32/apps/wineconfig.png
	install -D -p -m0644 kde/wineconfig/pics/16x16/wineconfig.png \
		$(DEB_DESTDIR)/opt/trinity/share/icons/crystalsvg/16x16/apps/wineconfig.png

	# install lintian overrides
	install -D -p -m0644 debian/tde-guidance-trinity.lintian-overrides \
		$(DEB_DESTDIR)/usr/share/lintian/overrides/tde-guidance-trinity

	# fix binary-or-shlib-defines-rpath
	chrpath -d $(DEB_DESTDIR)/opt/trinity/lib/trinity/kcm_*.so*

	# fix executable-not-elf-or-script
	chmod 0644 $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/pics/kdewinewizard.png

	# fix the link properly
	mkdir -p debian/tde-guidance-trinity/opt/trinity/bin
	(cd debian/tde-guidance-trinity/opt/trinity/bin; ln -sf $(GUIDANCE_INST_PATH)/mountconfig.py mountconfig)
	(cd debian/tde-guidance-trinity/opt/trinity/bin; ln -sf $(GUIDANCE_INST_PATH)/serviceconfig.py serviceconfig)
	(cd debian/tde-guidance-trinity/opt/trinity/bin; ln -sf $(GUIDANCE_INST_PATH)/userconfig.py userconfig)
	(cd debian/tde-guidance-trinity/opt/trinity/bin; ln -sf $(GUIDANCE_INST_PATH)/wineconfig.py wineconfig)
#	(cd debian/tde-guidance-trinity/opt/trinity/bin; ln -sf $(GUIDANCE_INST_PATH)/grubconfig.py grubconfig)

	# move python modules in PYSUPPORT_PATH
	mkdir -p $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/SMBShareSelectDialog.py $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/SimpleCommandRunner.py $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/fuser.py $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/fuser_ui.py $(PYSUPPORT_PATH)/tde-guidance-trinity
#	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/grubconfig.py $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/mountconfig.py $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/serviceconfig.py $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/sizeview.py $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/unixauthdb.py $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/userconfig.py $(PYSUPPORT_PATH)/tde-guidance-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/wineconfig.py $(PYSUPPORT_PATH)/tde-guidance-trinity

        # fix script-not-executable
	chmod 0755 $(PYSUPPORT_PATH)/tde-guidance-trinity/fuser.py
#	chmod 0755 $(PYSUPPORT_PATH)/tde-guidance-trinity/grubconfig.py

	: # Replace all '#!' calls to python with /usr/bin/python
	: # and make them executable
	for i in `find debian/tde-guidance-trinity -type f`; do \
		sed '1s,#!.*python[^ ]*\(.*\),#! /usr/bin/python\1,' \
			$$i > $$i.temp; \
		if cmp --quiet $$i $$i.temp; then \
			rm -f $$i.temp; \
		else \
			mv -f $$i.temp $$i; \
			chmod 755 $$i; \
			echo "fixed interpreter: $$i"; \
		fi; \
	done

install/guidance-backends-trinity::
	mkdir -p $(PYSUPPORT_PATH)/guidance-backends-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/MicroHAL.py $(PYSUPPORT_PATH)/guidance-backends-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/wineread.py $(PYSUPPORT_PATH)/guidance-backends-trinity
	cp $(DEB_DESTDIR)/opt/trinity/share/apps/guidance/winewrite.py $(PYSUPPORT_PATH)/guidance-backends-trinity

clean::
	rm -rf build

	rm -rf doc/en/*html doc/en/*bz2
	rm -f serviceconfig/kcm_serviceconfig.cpp
	rm -f mountconfig/kcm_mountconfig.cpp
	rm -f displayconfig/kcm_displayconfig.cpp
	rm -f userconfig/kcm_userconfig.cpp
	rm -f install_log.txt
	rm -f po/*.gmo
	rm -f po/*.pot

	# clean up edited .desktop etc files
	if [ -f /usr/lib/kubuntu-desktop-i18n/createdesktop.pl ]; then \
		sh /usr/lib/kubuntu-desktop-i18n/findfiles LIST; \
		for file in `cat LIST`; do \
			sed -i '/X-Ubuntu-Gettext-Domain=/d' $${file}; \
		done; \
		rm -f LIST; \
	fi

common-install-prehook-impl::
	# generate form implementation using pytde user interface compiler

	mkdir -p po
	touch list
	find -name "*.py" >> list
	xgettext -ki18n -LPython -flist -o po/guidance.pot
	rm list

	for file in po/*pot; do \
		sed "s/charset=CHARSET/charset=UTF-8/" -i $$file; \
	done

	# generate .po files for .desktop etc files
	if [ -z $${KUBUNTU_DESKTOP_POT} ]; then \
		KUBUNTU_DESKTOP_POT=$(DEB_SOURCE_PACKAGE); \
	fi; \
	echo KUBUNTU_DESKTOP_POT: $${KUBUNTU_DESKTOP_POT}; \
	if [ -f /usr/lib/kubuntu-desktop-i18n/createdesktop.pl ]; then \
		sh /usr/lib/kubuntu-desktop-i18n/findfiles LIST; \
		cat LIST; \
		perl /usr/lib/kubuntu-desktop-i18n/createdesktop.pl --file-list=LIST --base-dir=. > desktop.$${KUBUNTU_DESKTOP_POT}.tmp; \
		msguniq --to-code=UTF-8 --no-wrap -o desktop.$${KUBUNTU_DESKTOP_POT} desktop.$${KUBUNTU_DESKTOP_POT}.tmp 2>/dev/null; \
		python /usr/lib/kubuntu-desktop-i18n/msgsplit desktop.$${KUBUNTU_DESKTOP_POT}; \
		mv desktop.$${KUBUNTU_DESKTOP_POT} po/desktop_$${KUBUNTU_DESKTOP_POT}.pot; \
		rm -f desktop.$${KUBUNTU_DESKTOP_POT} desktop.$${KUBUNTU_DESKTOP_POT}.tmp; \
		# add translation domain to files \
		for file in `cat LIST`; do \
			echo X-Ubuntu-Gettext-Domain=desktop_$${KUBUNTU_DESKTOP_POT} >> $${file}; \
		done; \
		rm -f LIST; \
	fi

	rm -f tde-guidance-trinity/usr/share/python-support/tde-guidance-trinity/.version