#!/usr/bin/make -f

CFLAGS = -g -Wall
PURPLE_LIBDIR=$(shell pkg-config --variable=libdir purple)
PIDGIN_DATADIR=$(shell pkg-config --variable=datadir pidgin)
INSTALL_DIR=$(CURDIR)/debian/tmp

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

clean:
	dh_clean
	rm -f *so
	rm -f *stamp

build: build-stamp

build-stamp:
	cc -shared $(CFLAGS) -DPURPLE_PLUGINS `pkg-config --cflags --libs purple` libfacebook.c -o libfacebook.so
	touch build-stamp

install: build
	mkdir -p $(INSTALL_DIR)/$(PURPLE_LIBDIR)/purple-2
	cp -a libfacebook.so $(INSTALL_DIR)/$(PURPLE_LIBDIR)/purple-2

	mkdir -p $(INSTALL_DIR)/$(PIDGIN_DATADIR)/pixmaps/pidgin/protocols/16/
	cp facebook16.png $(INSTALL_DIR)/$(PIDGIN_DATADIR)/pixmaps/pidgin/protocols/16/facebook.png

	mkdir -p $(INSTALL_DIR)/$(PIDGIN_DATADIR)/pixmaps/pidgin/protocols/22/
	cp facebook22.png $(INSTALL_DIR)/$(PIDGIN_DATADIR)/pixmaps/pidgin/protocols/22/facebook.png

	mkdir -p $(INSTALL_DIR)/$(PIDGIN_DATADIR)/pixmaps/pidgin/protocols/48/
	cp facebook48.png $(INSTALL_DIR)/$(PIDGIN_DATADIR)/pixmaps/pidgin/protocols/48/facebook.png

binary-arch: binary

binary: install
	if test -x $(CURDIR)/debian/scripts/add-icons-to-control; then \
	  $(CURDIR)/debian/scripts/add-icons-to-control; \
	fi

	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
	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
	dh_md5sums

	dh_builddeb

	# remove icons added earlier
	if test -x $(CURDIR)/debian/scripts/add-icons-to-control; then \
	  $(CURDIR)/debian/scripts/add-icons-to-control --remove; \
	fi
	
squeaky: clean
	rm -rf \
		debian/tmp \
		$(shell cat debian/control | grep 'Package:' | awk '{print "../" $$2 "_*{deb,dsc,gz,changes}";}') \
		$(shell find | grep 'Makefile\.in$$') \
		aclocal.m4 compile config.guess config.h.in config.sub configure depcomp \
		install-sh intltool-extract.in intltool-merge.in intltool-update.in ltmain.sh Makefile.in missing mkinstalldirs

.PHONY: clean build binary install squeaky binary-arch
