#!/usr/bin/make -f

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

pixmaps = $(CURDIR)/debian/kid3/usr/share/pixmaps
bindir = $(CURDIR)/debian/kid3/usr/bin
kdedocs = $(CURDIR)/debian/kid3/usr/share/doc/kde/HTML

INSTALL_DIR = install -p -d -o root -g root -m 755
INSTALL_FILE = install -p -o root -g root -m 644

DEBVERSION:=$(shell head -n 1 debian/changelog \
                    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
UPVERSION:=$(shell echo $(DEBVERSION) | sed -e 's/^.*://' -e 's/-[0-9.]*$$//' -e 's/.dfsg$$//')

FILENAME := kid3_$(UPVERSION).orig.tar.gz
UPFILENAME := kid3-$(UPVERSION).tar.gz
URL = http://heanet.dl.sourceforge.net/kid3/$(UPFILENAME)

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

build: build-stamp

-include debian/debiandirs

DEB_CMAKE_PREFIX = /usr
DEB_CONFIG_INSTALL_DIR = $(DEB_CMAKE_PREFIX)/share/kde4/config
DEB_HTML_INSTALL_DIR = $(DEB_CMAKE_PREFIX)/share/doc/kde/HTML

CMAKE_KDE4_FLAGS += \
	-DCMAKE_SKIP_RPATH=true \
	-DKDE4_USE_ALWAYS_FULL_RPATH=false \
	-DWITH_MP4V2=true \
	-DSYSCONF_INSTALL_DIR=/etc \
	-DCMAKE_INSTALL_PREFIX=$(DEB_CMAKE_PREFIX) \
	-DCONFIG_INSTALL_DIR=$(DEB_CONFIG_INSTALL_DIR) \
	-DDATA_INSTALL_DIR=$(DEB_CMAKE_PREFIX)/share/kde4/apps \
	-DHTML_INSTALL_DIR=$(DEB_HTML_INSTALL_DIR) \
	-DKCFG_INSTALL_DIR=$(DEB_CMAKE_PREFIX)/share/kde4/config.kcfg \
	-DLIB_INSTALL_DIR=$(DEB_CMAKE_PREFIX)/lib

debian/debiandirs: admin/debianrules
	perl -w admin/debianrules echodirs > debian/debiandirs

build-stamp:
	dh_testdir
	#Kubuntu addition for KDE 4.5
	patch -p1 < debian/patches/kubuntu_01_fix_docs.diff
	# KDE4 build.
	mkdir kid3-kde4
	cd kid3-kde4 && cmake .. $(CMAKE_KDE4_FLAGS)
	$(MAKE) -C kid3-kde4
	# Qt4 build.
	cd kid3-qt && \
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr --with-qmake=qmake-qt4 && \
	cd ..
	$(MAKE) -C kid3-qt
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) -C kid3-qt distclean
	[ ! -f Makefile ] || $(MAKE) -C kid3-kde4 distclean
	rm -rf kid3-kde4
	rm -f *-stamp po/*.gmo kid3-qt/kid3/*.png kid3-qt/kid3/*.svgz
	#Kubuntu addition for KDE 4.5
	-patch -p1 -R < debian/patches/kubuntu_01_fix_docs.diff
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	# Main install.
	$(MAKE) -C kid3-kde4 install DESTDIR=$(CURDIR)/debian/kid3
	$(MAKE) -C kid3-qt install INSTALL_ROOT=$(CURDIR)/debian/kid3-qt
	# Make common links for docs.
	for p in `dh_listpackages`; do \
	  for i in `find $(kdedocs) -type d -name $$p -not -regex .*/en/$$p`; do \
	    ln -f -n -s ../../en/common $$i/common; done; done
	perl -w admin/debianrules cleanup

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -A AUTHORS NEWS README
	dh_installmenu
	dh_installman
	dh_installchangelogs ChangeLog
	dh_icons
	dh_link
	dh_strip
	dh_compress --exclude=.docbook
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

get-orig-source:
	@@dh_testdir
	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs
	@@echo Downloading $(UPFILENAME) from $(URL) ...
	@@wget -N -nv -T10 -t3 -O ../tarballs/$(FILENAME) $(URL)
