#!/usr/bin/make -f
%:
ifneq ($(shell dh -l | grep -xF translations),)
	dh $@ --with python2,translations
else
	dh $@ --with python2
endif

override_dh_auto_install:
	dh_auto_install -- --install-scripts=/opt/extras.ubuntu.com/journey                 --install-data=/opt/extras.ubuntu.com/journey                 --install-lib=/opt/extras.ubuntu.com/journey

override_dh_python2:
	dh_python2 /opt/extras.ubuntu.com/journey


override_dh_install:
	dh_install
	mkdir -p debian/journey/opt/extras.ubuntu.com/journey/bin
	if [ -x debian/journey/opt/extras.ubuntu.com/journey/journey/journey ]; then mv debian/journey/opt/extras.ubuntu.com/journey/journey/journey debian/journey/opt/extras.ubuntu.com/journey/bin; fi
	if [ -f debian/journey/opt/extras.ubuntu.com/journey/share/applications/journey.desktop ]; then \
		mkdir -p debian/journey/usr/share/applications; \
		mv debian/journey/opt/extras.ubuntu.com/journey/share/applications/journey.desktop debian/journey/usr/share/applications/extras-journey.desktop; \
		rmdir --ignore-fail-on-non-empty debian/journey/opt/extras.ubuntu.com/journey/share/applications; \
		sed -i 's|Exec=.*|Exec=/opt/extras.ubuntu.com/journey/bin/journey|' debian/journey/usr/share/applications/extras-journey.desktop; \
		sed -i 's|Icon=/usr/|Icon=/opt/extras.ubuntu.com/journey/|' debian/journey/usr/share/applications/extras-journey.desktop; \
	fi
	grep -RlZ 'import gettext' debian/journey/* | xargs -0 -r sed -i 's|\(import\) gettext$$|\1 locale|'
	grep -RlZ 'from gettext import gettext as _' debian/journey/* | xargs -0 -r sed -i 's|from gettext \(import gettext as _\)|from locale \1|'
	grep -RlZ "gettext.textdomain('journey')" debian/journey/* | xargs -0 -r sed -i "s|gettext\(\.textdomain('journey')\)|locale\.bindtextdomain('journey', '/opt/extras.ubuntu.com/journey/share/locale')\nlocale\1|" 
	sed -i "s|__journey_data_directory__ =.*|__journey_data_directory__ = '/opt/extras.ubuntu.com/journey/share/journey/'|" debian/journey/opt/extras.ubuntu.com/journey/journey*/journeyconfig.py
	sed -i 's|        sys.path.insert(0, opt_path)|\0\n    os.putenv("XDG_DATA_DIRS", "%s:%s" % ("/opt/extras.ubuntu.com/journey/share/", os.getenv("XDG_DATA_DIRS", "")))|' debian/journey/opt/extras.ubuntu.com/journey/bin/journey
	if [ -d debian/journey/opt/extras.ubuntu.com/journey/share/glib-2.0/schemas ]; then glib-compile-schemas debian/journey/opt/extras.ubuntu.com/journey/share/glib-2.0/schemas; fi