#!/usr/bin/make -f

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=4

# 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)
CONFIGURE_OPTIONS = --disable-dependency-tracking

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O4
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
ifneq (,$(findstring nossl,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_OPTIONS += --disable-ssl
else
        CONFIGURE_OPTIONS += --enable-ssl
endif
ifneq (,$(findstring notcl,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_OPTIONS += --disable-tcl
else
        CONFIGURE_OPTIONS += --enable-tcl
endif
ifneq (,$(findstring noxmpp,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_OPTIONS += --disable-xmpp
else
        CONFIGURE_OPTIONS += --enable-xmpp
endif
ifneq (,$(findstring autopackage,$(DEB_BUILD_OPTIONS)))
	CONFIGURE_OPTIONS += --enable-autopackage
endif

ifeq (,$(CLIMM_EXTRAVERSION))
	CLIMM_EXTRAVERSION = Debian hand compiled
endif
export CLIMM_EXTRAVERSION

configure: configure-stamp
configure-stamp:
	dh_testdir
	./configure --host=$(DEB_HOST_GNU_TYPE) \
	  --build=$(DEB_BUILD_GNU_TYPE) \
	  --prefix=/usr \
	  --mandir=\$${prefix}/share/man \
	  $(CONFIGURE_OPTIONS)
	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	$(MAKE)
	touch build-stamp

clean:
	-$(MAKE) distclean
	dh_clean
	rm -rf configure-stamp build-stamp src/.cvsupdate

install:
	dh_clean -k
	dh_testroot
	$(MAKE) install DESTDIR=`pwd`/debian/climm
	mkdir -p debian/climm/etc/apt/sources.list.d
	-. /etc/lsb-release; echo "deb http://www.climm.org/deb/ $${CLIMM_DISTRI:-$$DISTRIB_CODENAME} main" \
	  > debian/climm/etc/apt/sources.list.d/climm

binary-indep: build install

binary-arch: build install
	dh_testroot
	dh_installdocs
	
	dh_installchangelogs ChangeLog
	install -D -m 644 doc/climm.xpm debian/climm/usr/share/pixmaps/climm.xpm
	install -D -m 644 doc/climm@48x48.png debian/climm/usr/share/icons/hicolor/48x48/apps/climm.png
	install -D -m 644 doc/climm@32x32.png debian/climm/usr/share/icons/hicolor/32x32/apps/climm.png
	install -D -m 644 doc/climm@16x16.png debian/climm/usr/share/icons/hicolor/16x16/apps/climm.png
	dh_installmenu
	
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip
endif
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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