#!/usr/bin/make -f

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

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

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


VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -d' ' -f2 | cut -d'-' -f1 | cut -d':' -f2 )

configure: configure-stamp
configure-stamp:
	dh_testdir
	#automake
	autoconf
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--disable-kernel-module \
		--enable-dynamic \
		--enable-xosd \
		--enable-wacom \
		CFLAGS="$(CFLAGS)" \
		LDFLAGS="-L$(CURDIR)/debian/wacomcfg -Wl,-z,defs" \
		CPPFLAGS="$(CPPFLAGS) -I$(CURDIR)/debian/wacomcfg"
	sed -i 's/-lXinerama//' config.status Makefile src/Makefile
	touch configure-stamp

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

clean:
	dh_testdir
	dh_testroot
	make -C src clean || true
	-$(MAKE) distclean || true
	rm -f build-stamp configure-stamp
	dh_clean 

install: d=$(CURDIR)/debian/tmp
install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	$(MAKE) DESTDIR=$(d) install

install-kernel-source: d=$(CURDIR)/debian/tmp/usr/src/fujitsu-tablet-$(VERSION)
install-kernel-source: configure-stamp
	install --mode=755 -d $(d)
	install --mode=644 src/linux/fujitsu-tablet.c $(d)
	install --mode=644 src/linux/Makefile $(d)
	$(MAKE) -C contrib dkms.conf
	install --mode=644 contrib/dkms.conf $(d)

binary-arch: build install install-kernel-source
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
#	dh_installexamples -a
	dh_install -a --sourcedir=$(CURDIR)/debian/tmp --fail-missing
#	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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