#!/usr/bin/make -f
#
# debian/rules for the xf86-video-unichrome package.
#
# Copyright © 2004 Scott James Remnant <scott@netsplit.com>
# Copyright © 2005 Daniel Stone <daniel@fooishbar.org>
# Copyright © 2005 David Nusinow <dnusinow@debian.org>
# Copyright © 2007 Luc Verhaegen <libv@skynet.be>

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

# get a git SHAID for package versioning.
# this needs to be dynamic, as i don't want to touch this file every 5 minutes.
UNICHROMEVERSION = 0.2.0
git_tools := $(shell which git)
ifdef git_tools
	not_git_dir := $(shell test -e ../.git/index)
	ifndef not_git_dir
		SHAID = $(shell git whatchanged | head -n1 | sed s/^commit\ // | sed s/[a-f0-9]/\ /9 | awk '{print $$1}')
		UNICHROMEVERSION = 0.2.0-git-${SHAID}
	endif
endif

# We want this to build against many xserver-xorg-core packages,
# but we do need decent binary dependency tracking.
XORGVERSION ?= $(shell dpkg-query -l xserver-xorg-dev | awk '/xserver-xorg-dev/ {print $$3}' | sed s/-.*//)

# Ubuntu packagers: get a clue... which they got some time between may 2006 and may 2007.
ISDEBIAN ?= $(shell dpkg-query -l xutils-dev | awk '/ii/ {print $$1}')
ifeq ($(ISDEBIAN),ii)
	# Now work in the ABI version.

	VIDEO_ABI = $(shell cat /usr/share/xserver-xorg/videoabiver 2>/dev/null)

	ifndef VIDEO_ABI
		VIDEO_VERSION = xserver-xorg-video
	else
		VIDEO_VERSION = xserver-xorg-video-$(VIDEO_ABI)
	endif

	PROVIDES := "$(VIDEO_VERSION), xserver-xorg-video-via, xserver-xorg-video-openchrome"
	CONFLICTS := "xserver-xorg-video-via, xserver-xorg-video-openchrome"
	REPLACES := "xserver-xorg-video-via, xserver-xorg-video-openchrome"
else
	PROVIDES := "xserver-xorg-driver, xserver-xorg-driver-via, xserver-xorg-driver-openchrome"
	CONFLICTS := "xserver-xorg-driver-via, xserver-xorg-driver-openchrome"
	REPLACES := "xserver-xorg-driver-via, xserver-xorg-driver-openchrome"
endif

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

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

# kbd_drv.a isn't phenomenally useful; kbd_drv.so more so
confflags += --disable-static

build: build-stamp
build-stamp:
	dh_testdir

	./autogen.sh --prefix=/usr --mandir=\$${prefix}/share/man \
	             --infodir=\$${prefix}/share/info $(confflags) \
	             CFLAGS="$(CFLAGS)"
	./git_version.sh > git_version.h
	$(MAKE)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	rm -f git_version.h
	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

	$(MAKE) DESTDIR=$(CURDIR)/debian/xf86-video-unichrome install

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

	dh_installdocs
	dh_installchangelogs
	dh_install
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_shlibdeps
	dh_installdeb
	dh_gencontrol -- -v$(UNICHROMEVERSION) -VXorg:Version=$(XORGVERSION) -VReplaces=$(REPLACES) -VConflicts=$(CONFLICTS) -VProvides=$(PROVIDES)
	dh_md5sums
	dh_builddeb

# Build architecture-independent files here.
binary-indep: build install
# Nothing to do

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