#!/usr/bin/make -f

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

include debian/xsfbs/xsfbs.mk

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# FOR AUTOCONF 2.52 AND NEWER ONLY
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

xkb_base := /usr/share/X11/xkb

configure: $(STAMP_DIR)/patch
	intltoolize -c -f
	autoreconf -v -f --install

config.status: configure
	dh_testdir
	./configure $(confflags) \
	  --prefix=/usr \
	  --disable-xkbcomp-symlink --with-xkb-rules-symlink=evdev,xorg \
	  --disable-nls \
	  --with-xkb-base=$(xkb_base)
	>$@

build: $(STAMP_DIR)/build
$(STAMP_DIR)/build: config.status
	dh_testdir
	$(MAKE)
	>$@

clean: xsfclean
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf $$(find -name Makefile.in)
	rm -f aclocal.m4 configure install-sh intltool-*.in
	rm -f missing po/Makefile.in.in
	rm -f semantics/semantics.dir
	rm -f rules/evdev rules/evdev.xml.in

	# Files not cleaned up
	-rm -f intltool-extract intltool-merge intltool-update
	-rm -f rules/base.xml
	-rm -f po/.intltool-merge-cache config.status.lineno
	dh_clean
	rm -f compat/compat.dir geometry/geometry.dir keycodes/keycodes.dir
	rm -f keymap/keymap.dir symbols/symbols.dir types/types.dir rules/base
	rm -f rules/compat/base.l[2-4]*.part rules/compat/base.m*.part

deb:
	dpkg-buildpackage -rfakeroot -i'/\.git|/\.svn'

dir:
	set -e; for f in compat geometry keycodes keymap semantics symbols types; do \
	  rm -f $$f/$$f.dir; \
	  cd $$f; \
	  make $$f.dir XKBCOMP=xkbcomp; \
	  LC_ALL=C sort -k3 $$f.dir > $$f.new && mv $$f.new $$f.dir; \
	  cd ..; \
	  cp $$f/$$f.dir debian/; \
	done

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/xkb-data-extkbd
	#  Remove this symlink, it is no more needed
	rm -f $(CURDIR)/debian/xkb-data-extkbd/$(xkb_base)/compiled

# Build architecture-dependent files here.
binary-arch: build install
# We have nothing to do by default.

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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