#!/usr/bin/make -f
# debian/rules for the Debian libx11 package.
# Copyright © 2004 Scott James Remnant <scott@netsplit.com>
# Copyright © 2005 Daniel Stone <daniel@fooishbar.org>
# Copyright © 2005 David Nusinow <dnusinow@debian.org>
# Copyright © 2006 Josh Triplett <josh@freedesktop.org>
# Copyright © 2006 Jamey Sharp <sharpone@debian.org>

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

include debian/xsfbs/xsfbs.mk

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# This is for maintainer's convenience. It really should be always on option
# but dh_install doesn't support --exclude-from <file>, and we leave out too
# much crap to bother specifying it through --exclude arguments.
# It alters dh_install call in binary_indep, as it is the first one called
# from binary target.
ifneq (,$(findstring checkinstall,$(DEB_BUILD_OPTIONS)))
	dhinstallindepflags := --fail-missing
else
	dhinstallindepflags := -i
endif

DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build=$(DEB_HOST_GNU_TYPE)
else
	confflags += --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

confflags += --enable-man-pages=3 --without-xcb

build: genscripts patch build-stamp
build-stamp:
	dh_testdir

	test -d obj-$(DEB_BUILD_GNU_TYPE) || mkdir obj-$(DEB_BUILD_GNU_TYPE)
	cd obj-$(DEB_BUILD_GNU_TYPE) && \
	../autogen.sh --prefix=/usr --mandir=\$${prefix}/share/man \
	             --infodir=\$${prefix}/share/info $(confflags) \
	             CFLAGS="$(CFLAGS)" 
	cd obj-$(DEB_BUILD_GNU_TYPE) && $(MAKE)

	touch build-stamp

clean: xsfclean
	dh_testdir
	dh_testroot
	rm -f build-stamp

	rm -f config.cache config.log config.status
	rm -f */config.cache */config.log */config.status
	rm -f conftest* */conftest*
	rm -rf autom4te.cache */autom4te.cache
	rm -rf obj-*

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd obj-$(DEB_BUILD_GNU_TYPE) && $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot

	dh_installdocs -s NEWS
	# Using exclusions here allows libx11-6.install to use wildcards for
	# headers and manpages, rather than listing every header and manpage
	# other than these.
	dh_install --sourcedir=debian/tmp -plibx11-dev \
	           --exclude=usr/include/X11/Xlib-xcb.h \
	           --exclude=usr/share/man/man3/XGetXCBConnection.3 \
	           --exclude=usr/share/man/man3/XSetEventQueueOwner.3
	dh_install --sourcedir=debian/tmp -Nlibx11-dev -s
#	dh_installchangelogs -s ChangeLog
	dh_installman -s
	dh_link -s
	dh_strip -plibx11-6 --dbg-package=libx11-6-dbg
	dh_strip -plibx11-xcb1 --dbg-package=libx11-xcb1-dbg
	dh_strip -Nlibx11-6 -Nlibx11-xcb1
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -s
	dh_shlibdeps -s
	dh_installdeb -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot

	dh_installdocs -i
	dh_installchangelogs -i NEWS
	dh_install --sourcedir=debian/tmp -i
	dh_link -i
	dh_installman -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

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