#!/usr/bin/make -f
# -*- Makefile -*-

PYTHON := $(shell pyversions -d)
export pythonpath=/usr/bin/$(PYTHON)

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

configure: configure-stamp
configure-stamp:
	[ -f config.sub.d-r-orig ] || cp -p config.sub config.sub.d-r-orig
	[ -f config.guess.d-r-orig ] || cp -p config.guess config.guess.d-r-orig
	cp -f /usr/share/misc/config.sub .
	cp -f /usr/share/misc/config.guess .
	[ -f xchat.xpm ] || convert -resize 32x32 xchat.png xchat.xpm
	CFLAGS="$(CFLAGS)" ./configure --prefix=/usr --datadir=/usr/share --sysconfdir=/etc --enable-python --enable-ipv6  \
	--disable-mmx --enable-openssl --enable-tcl=/usr/lib \
	--enable-dbus --disable-rpath
	touch configure-stamp

build: configure build-stamp
build-stamp: 
	dh_testdir
	$(MAKE)
	mv src/fe-gtk/xchat xchat
	touch build-stamp

clean: clean-stamp
clean-stamp:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	[ ! -f config.sub.d-r-orig ] || mv config.sub.d-r-orig config.sub
	[ ! -f config.guess.d-r-orig ] || mv config.guess.d-r-orig config.guess
	rm -f xchat.xpm
	rm -f config.log
	rm -f xchat xchat-gnome
	rm -f po/*.gmo
	rm -f build-stamp install-stamp configure-stamp po/stamp-po
	rm -rf debian/tmp debian/xchat debian/xchat-common
	rm -f src/common/dbus/org.xchat.service.service
	rm -f libtool
	dh_clean
	
install: install-stamp
install-stamp: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) install bindir=$(CURDIR)/debian/tmp/usr/bin libdir=$(CURDIR)/debian/tmp/usr/lib/xchat/plugins prefix=$(CURDIR)/debian/tmp/usr datadir=$(CURDIR)/debian/tmp/usr/share sysconfdir=$(CURDIR)/debian/tmp/etc servicedir=$(CURDIR)/debian/tmp/usr/share/dbus-1/services
	dh_install
	install -o root -g root -m 755 xchat $(CURDIR)/debian/xchat/usr/bin
	install -o root -g root -m 644 plugins/perl/*.pl $(CURDIR)/debian/xchat-common/usr/share/doc/xchat-common/scripts-perl/
	install -o root -g root -m 644 src/common/xchat-plugin.h $(CURDIR)/debian/xchat-common/usr/include/xchat/
	ln -s xchat-common $(CURDIR)/debian/xchat/usr/share/doc/xchat
	chrpath -d $(CURDIR)/debian/xchat/usr/bin/xchat
	chrpath -d $(CURDIR)/debian/xchat/usr/lib/xchat/plugins/perl.so
	chrpath -d $(CURDIR)/debian/xchat/usr/lib/xchat/plugins/python.so
	chrpath -d $(CURDIR)/debian/xchat/usr/lib/xchat/plugins/tcl.so

binary-indep: build install
	dh_testdir -pxchat-common
	dh_testroot -pxchat-common
	dh_installdocs -pxchat-common
	dh_installmenu -pxchat-common
	dh_installchangelogs ChangeLog -pxchat-common
	dh_installman -pxchat-common
	dh_compress -X.pl -pxchat-common
	dh_gconf -pxchat-common
	dh_fixperms -pxchat-common
	dh_installdeb -pxchat-common
	dh_gencontrol -pxchat-common
	dh_md5sums -pxchat-common
	dh_builddeb -pxchat-common

binary-arch: build install
	dh_testdir -pxchat
	dh_testroot -pxchat
	dh_installmenu -pxchat
	dh_installman -pxchat
	dh_link -pxchat
ifeq "$(findstring nostrip,$(DEB_BUILD_OPTIONS))" ""
	dh_strip -pxchat
endif	
	dh_compress -pxchat
	dh_fixperms -pxchat
	dh_installdeb -pxchat
	dh_shlibdeps -pxchat -Xpython.so -Xtcl.so -- \
		-dRecommends \
		debian/xchat/usr/lib/xchat/plugins/tcl.so \
		debian/xchat/usr/lib/xchat/plugins/python.so \
		-dDepends
	dh_gencontrol -pxchat
	dh_md5sums -pxchat
	dh_builddeb -pxchat
binary: binary-indep binary-arch
	
.PHONY: build clean clean-stamp binary-indep binary-arch binary install
