#!/usr/bin/make -f

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

# Dpatch targets
include /usr/share/dpatch/dpatch.make

# Get supported python version and destdir
PYVERS  = $(shell pyversions -rv)
DESTDIR = $(CURDIR)/debian/exaile
PREFIX  = /usr

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

build: patch-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-install-lib-stamp*
	$(MAKE) clean
	# This should be in upstream Makefile
	rm -f exaile.glade.h messages.pot plugins/plugins.glade.h
	dh_clean 

install: install-prereqs $(PYVERS:%=build-install-lib-stamp%)
	$(MAKE) translations
	$(MAKE) install DESTDIR=$(DESTDIR) PREFIX=$(PREFIX)
	# Move png to /usr/share/icons
	mv $(DESTDIR)/usr/share/pixmaps $(DESTDIR)/usr/share/icons
	# XPM icon
	install -D -m 644 images/icon.xpm \
		$(DESTDIR)/usr/share/pixmaps/exaile.xpm
	chmod +x $(DESTDIR)/usr/share/exaile/xl/burn.py
	chmod +x $(DESTDIR)/usr/share/exaile/xl/cd_import.py

install-prereqs:
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

build-install-lib-stamp%: build
	$(MAKE) clean
	$(MAKE) make-install-mmkeys.so DESTDIR=$(DESTDIR) PREFIX=$(PREFIX) PYTHON_INC=/usr/include/python$*
	touch $@

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs changelog
	dh_installdocs
	dh_installmenu
	dh_installman
	# Add mmkeys private python module path
	sed -i "s!\(^[^#]*import mmkey.*\)!    sys.path.append(\"/usr/lib/exaile/\"+\"python\"+sys.version[:3])\n\1!" \
	$(DESTDIR)/usr/share/exaile/exaile.py
	# Strip useless shebang lines
	perl debian/strip_shebang $(DESTDIR)/usr/share/exaile/xl/plugins/*.py $(DESTDIR)/usr/share/exaile/exaile.py
	dh_link $(PREFIX)/share/exaile/exaile.py $(PREFIX)/lib/exaile/exaile.py
	dh_strip
	dh_compress
	dh_fixperms
	dh_pysupport
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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