#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

PREFIX=/usr

# 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)

version=$(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //' | sed -e 's/-.*//')
major=$(shell echo ${version} | cut -d. -f1-2)
micro=$(shell echo ${version} | cut -d. -f3 | cut -d+ -f1)
nextmicro=$(shell expr ${micro} + 1)
launcher_check=$(shell dpkg --compare-versions "$$(apt-cache policy maemo-version | grep Installed | awk '{print $$2;}')" "lt" "5.0" && echo "maemo-launcher" || echo "nolauncher")


CFLAGS = -Wall -g

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

# Use soft-float and thumb mode if enabled. 
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# Find out how many parallel threads to run 
NUMJOBS=$(shell echo "$(DEB_BUILD_OPTIONS)" | awk -F ',' '{ for (Nix=1 ; Nix <= NF ; Nix++) { n_fields = split ($$Nix, fields, "="); if (fields[1] == "parallel" && n_fields == 2) print(fields[2]);}}')
ifneq (,$(NUMJOBS))
	PARALLEL_MAKEFLAGS += -j$(NUMJOBS)
endif

CONFIGURE_SWITCHES = \
		--disable-missing-dependencies \
		--prefix=$(PREFIX) \
		--with-python=/usr/bin/python2.5 \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--sysconfdir=/etc \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--disable-consoleui \
		--disable-doxygen \
		--disable-perl \
		$(NULL)

ifeq ($(launcher_check),maemo-launcher)
	ifneq (,$(findstring maemo-launcher,$(DEB_BUILD_OPTIONS)))
		CONFIGURE_SWITCHES += --enable-maemo-launcher
		USE_MAEMO_LAUNCHER = true
	endif
endif
ifeq ($(launcher_check),nolauncher)
	ifeq (,$(findstring nolauncher,$(DEB_BUILD_OPTIONS)))
		CONFIGURE_SWITCHES += --enable-maemo-launcher
		USE_MAEMO_LAUNCHER = true
	endif
endif

configure:
	echo -e '\033[7mtarget: configure\033[0m'

	dh_testdir

	CFLAGS="$(CFLAGS)" SBOX_DEFAULT_AUTOMAKE="1.9" ./autogen.sh $(CONFIGURE_SWITCHES)
#	autoreconf -s -i && glib-gettextize && aclocal

build: build-stamp
	echo -e '\033[7mtarget: build\033[0m'

build-stamp: configure
	echo -e '\033[7mtarget: build-stamp\033[0m'
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) $(PARALLEL_MAKEFLAGS)

	pod2man $(CURDIR)/debian/dh_pidgin > $(CURDIR)/debian/dh_pidgin.1 || \
		rm -f $(CURDIR)/debian/dh_pidgin.1

	touch build-stamp

clean:
	echo -e '\033[7mtarget: clean\033[0m'
	dh_testdir
	dh_testroot
	rm -f *-stamp 

	# Add here commands to clean up after the build process.
	-$(MAKE) $(PARALLEL_MAKEFLAGS) clean
	dh_clean
	rm -f $(CURDIR)/debian/dh_pidgin.1
	-$(MAKE) package_revision_raw.txt
	if ! test -f $(CURDIR)/package_revision_raw.txt; then \
		if test -d $(CURDIR)/_MTN; then \
			cat $(CURDIR)/_MTN/revision | grep old_revision | sed -r 's/^.*[[]([0-9a-fA-F]*)[]].*$$/\1/' > $(CURDIR)/package_revision_raw.txt; \
		fi; \
	fi
	if test -f $(CURDIR)/package_revision_raw.txt; then \
		mv $(CURDIR)/package_revision_raw.txt $(CURDIR)/package_revision_raw.txt.tmp; \
	fi

install: build install-stamp

install-stamp:
	echo -e '\033[7mtarget: install\033[0m'
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	echo -e '\033[7mMaking install\033[0m'
	# Add here commands to install the package into debian/tmp
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	# 26x26 icon - the "scalable" icons is installed via make install
	mkdir -p $(CURDIR)/debian/tmp$(PREFIX)/share/icons/hicolor/26x26/hildon
	cp $(CURDIR)/debian/pidgin.png $(CURDIR)/debian/tmp$(PREFIX)/share/icons/hicolor/26x26/hildon

	# mandatory D-Bus service file
	mkdir -p $(CURDIR)/debian/tmp$(PREFIX)/share/dbus-1/services
ifneq (,$(findstring maemo-launcher,$(DEB_BUILD_OPTIONS)))
	cp $(CURDIR)/debian/override-files/pidgin.service $(CURDIR)/debian/tmp$(PREFIX)/share/dbus-1/services
endif

	mkdir -p $(CURDIR)/debian/tmp$(PREFIX)/share/applications/hildon
ifneq (,$(findstring maemo-launcher,$(DEB_BUILD_OPTIONS)))
	cp $(CURDIR)/debian/override-files/pidgin.desktop $(CURDIR)/debian/tmp$(PREFIX)/share/applications/hildon
else
	cat $(CURDIR)/debian/override-files/pidgin.desktop | grep -v 'X-Osso-Service' > $(CURDIR)/debian/tmp$(PREFIX)/share/applications/hildon/pidgin.desktop
endif

	# Ship default values for some settings
	mkdir -p $(CURDIR)/debian/tmp/etc/purple
	cp $(CURDIR)/debian/override-files/prefs.xml $(CURDIR)/debian/tmp/etc/purple

	# dh_pidgin
	mkdir -p $(CURDIR)/debian/tmp$(PREFIX)/bin
	cp $(CURDIR)/debian/dh_pidgin $(CURDIR)/debian/tmp$(PREFIX)/bin
	if test -f $(CURDIR)/debian/dh_pidgin.1; then \
		mkdir -p $(CURDIR)/debian/tmp$(PREFIX)/share/man/man1; \
		cp $(CURDIR)/debian/dh_pidgin.1 $(CURDIR)/debian/tmp$(PREFIX)/share/man/man1; \
	fi

	# Move icons to a Hildon-friendly place
	for icon_dir in $(CURDIR)/debian/tmp$(PREFIX)/share/icons/hicolor/* ; do \
		if test -d $$icon_dir/apps; then \
			echo mv $$icon_dir/apps $$icon_dir/hildon; \
			mv $$icon_dir/apps $$icon_dir/hildon; \
		else \
			echo $$icon_dir/apps DNE; \
		fi ; \
	done
	if test -f $(CURDIR)/debian/tmp$(PREFIX)/share/icons/hicolor/48x48/hildon/pidgin.png; then \
		mkdir -p $(CURDIR)/debian/tmp$(PREFIX)/share/icons/hicolor/scalable/hildon; \
		cp $(CURDIR)/debian/tmp$(PREFIX)/share/icons/hicolor/48x48/hildon/pidgin.png \
			$(CURDIR)/debian/tmp$(PREFIX)/share/icons/hicolor/scalable/hildon; \
	fi

	# Remove the dreaded .la files
	find $(CURDIR)/debian/tmp | grep '\.la$$' | xargs rm -f

	# Add osso-backup script
	mkdir -p $(CURDIR)/debian/tmp/etc/osso-backup/applications
	cp $(CURDIR)/debian/pidgin.conf $(CURDIR)/debian/tmp/etc/osso-backup/applications

	# Include author lists in copyright file
	sed -e '/@PIDGIN_COPYRIGHT@/r COPYRIGHT' \
		-e '/@PIDGIN_COPYRIGHT@/d' \
		-e '/@OSCAR_AUTHORS@/r libpurple/protocols/oscar/AUTHORS' \
		-e '/@OSCAR_AUTHORS@/d' \
	debian/copyright.in > debian/copyright

	touch install-stamp

# Build architecture-independent files here.
binary-indep: build install
	echo -e '\033[7mtarget: binary-indep\033[0m'
# We have nothing to do by default.

clean-debian:
	echo -e '\033[7mtarget: clean-debian\033[0m'
	rm -rf \
		`ls -ld debian/* | grep '^d' | grep -vE 'override-files|scripts|tmp' | awk '{print $$$$NF;}'` \
		debian/{*.{debhelper,substvars},files,pidgin.install,copyright}

$(CURDIR)/debian/pidgin.install: $(CURDIR)/debian/pidgin.install.in
	# Grab only those .install.in globs that actually match
	echo -n '' > $(CURDIR)/debian/pidgin.install
	cat $(CURDIR)/debian/pidgin.install.in | while read; do \
		if eval "ls $(CURDIR)/debian/tmp/$$REPLY" > /dev/null; then \
			echo $$REPLY >> $(CURDIR)/debian/pidgin.install; \
		fi; \
	done

# Build architecture-dependent files here.
binary-arch: build install clean-debian $(CURDIR)/debian/pidgin.install
	echo -e '\033[7mtarget: binary-arch\033[0m'

	dh_maemo_package_icons
	dh_testdir
	dh_testroot
#	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_install --sourcedir=debian/tmp --fail-missing -v
	dh_gconf
#	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	if test "x$(USE_MAEMO_LAUNCHER)" = "xtrue"; then \
	  dh_maemolauncher -v; \
	fi
	for dbg in `cat $(CURDIR)/debian/control | grep '^Package:' | grep --regex='-dbg$$' | awk '{print $$2;}' | sed 's/-dbg$$//'`; do \
		echo dh_strip --dbg-package=$$dbg; \
		dh_strip --dbg-package=$$dbg; \
	done
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
	dh_makeshlibs -V "libpurple0 (>= $(major).0)"

	# some dh_*-generated pre- and post- -inst and -rm segments may pass args unknown to some busybox coreutils
	for script in `ls $(CURDIR)/debian/*.p{re,ost}{inst,rm}.* 2> /dev/null`; do \
		cat $$script | sed -r -e 's/-p --ignore-fail-on-non-empty (.*$$)/\1 || true/' > $$script.new && \
			mv $$script.new $$script; \
	done

	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- $(LAUNCHER_DEPENDS)
	dh_md5sums

	# maemo-optify all packages the standard way, except pidgin-emotes-default
	if test -x /usr/bin/maemo-optify; then \
		for pkg in `cat $(CURDIR)/debian/control | grep '^Package:' | awk '{print $$2;}' | grep -vE 'pidgin-emotes-default'`; do \
			maemo-optify $$pkg; \
		done; \
	fi

	dh_builddeb
	dh_maemo_package_icons -r

binary: binary-indep binary-arch
	echo -e '\033[7mtarget: binary\033[0m'

squeaky: clean clean-debian
	-$(MAKE) distclean
	rm -rf \
		debian/tmp \
		`cat debian/control | grep 'Package:' | awk '{print "../" $$2 "_*deb";}'` \
		../pidgin_*.* \
		`find | grep 'Makefile\.in$$'` \
		aclocal.m4 compile config.guess config.h.in config.sub configure depcomp \
		package_revision.h package_revision_raw.txt.tmp package_revision_raw.txt \
		install-sh intltool-extract.in intltool-merge.in intltool-update.in ltmain.sh Makefile.in missing mkinstalldirs

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