#!/usr/bin/make -f

-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

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

get-orig-source:
	git clone git://anongit.freedesktop.org/git/nouveau/xf86-video-nouveau && \
	cd xf86-video-nouveau && autoreconf -v --install && \
	git log |head -3|tail -1|tr -d '\n' > snapshot-date && \
	REVISION=$$(git show --pretty=format:"%h" HEAD | head -n1) && cd .. && \
	PREFIX=xserver-xorg-video-nouveau_0.0.15+git$$(date +%Y%m%d)+$$REVISION && \
	rm -rf xf86-video-nouveau/.git && \
	tar czf $$PREFIX.orig.tar.gz --exclude=autom4te.cache xf86-video-nouveau && \
	rm -rf xf86-video-nouveau

build: 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) && \
	../configure --prefix=/usr --mandir=\$${prefix}/share/man \
	             --infodir=\$${prefix}/share/info $(confflags) \
	             CFLAGS="$(CFLAGS)"
	cd obj-$(DEB_BUILD_GNU_TYPE) && $(MAKE)
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp

	[ ! -f Makefile ] || $(MAKE) distclean

	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-*
	rm -rf stampdir

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

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

binary-arch: build install serverabi
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installchangelogs
	dh_install --sourcedir=debian/tmp --list-missing
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep: build install

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install get-orig-source
