#!/usr/bin/make -f
# debian/rules for the Debian xorg-server package.
# Copyright © 2004 Scott James Remnant <scott@netsplit.com>
# Copyright © 2005 Daniel Stone <daniel@fooishbar.org>
# Copyright © 2005 David Nusinow <dnusinow@debian.org>

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

include debian/xsfbs/xsfbs.mk

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

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	DEBUG_FLAGS="--enable-debug"
endif

# Use hardware floating point
ifneq (,$(findstring vfp,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -march=armv6 -mfpu=vfp -mfloat-abi=softfp
else
	# Use soft-float and thumb mode if it enabled.
	ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
		CFLAGS += -mthumb
	endif
endif

DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_BUILD_ARCH     ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build=$(DEB_BUILD_GNU_TYPE)
endif

INSTALL=/usr/bin/install

# Build Xomap on ARM, Xephyr on i386/amd64.  Xephyr doesn't work with
# Xomap.  Woo.  Also, ARM means a product target, i386/amd64 means an
# SDK target.  We really need a better way of making this distinction.
ifneq (,$(findstring arm,$(DEB_BUILD_ARCH)))
KDRIVE_PLATFORM_FLAGS=--enable-xomap --disable-xephyr --enable-xsp --enable-xkb --with-vendor-string=Nokia
else
KDRIVE_PLATFORM_FLAGS=--disable-xomap --enable-xephyr --disable-xsp --disable-xkb --with-vendor-string=Maemo
endif

KDRIVE_PLATFORM_FLAGS += --disable-xgl --disable-xsdl --disable-xfake --disable-xfbdev --disable-kdrive-vesa

export DH_OPTIONS

confflags += --disable-xorg --disable-dmx --disable-xvfb --disable-xprint \
             --disable-lbx --disable-xorgconfig --disable-xorgcfg \
             --disable-xtrap --enable-xsp --disable-xnest --disable-xwin \
             --disable-xgl --enable-kdrive $(KDRIVE_PLATFORM_FLAGS) \
             --disable-glx --disable-dri --enable-builtin-fonts \
	     --disable-xevie --disable-ipv6 --enable-dbus \
	     --enable-null-root-cursor --with-xkb-output=/tmp \
	     --disable-xcsecurity --enable-hal $(DEBUG_FLAGS)

build: build-stamp
build-stamp: $(STAMP_DIR)/patch
	dh_testdir

	mkdir -p obj-$(DEB_BUILD_GNU_TYPE)/GL
	cd obj-$(DEB_BUILD_GNU_TYPE) && \
	../configure --prefix=/usr --mandir=\$${prefix}/share/man \
	             --infodir=\$${prefix}/share/info --sysconfdir=/etc \
	             --localstatedir=/var $(confflags) CFLAGS="$(CFLAGS)" 
	cd obj-$(DEB_BUILD_GNU_TYPE) && $(MAKE)

	touch $@

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
ifneq (,$(findstring arm,$(DEB_BUILD_ARCH)))
	install -d $(CURDIR)/debian/xserver-xomap/etc
	install -m644 $(CURDIR)/debian/pointercal-* $(CURDIR)/debian/xserver-xomap/etc/
	install -d $(CURDIR)/debian/xserver-xomap/etc/osso-af-init
	install -m644 $(CURDIR)/debian/x-server.defs $(CURDIR)/debian/xserver-xomap/etc/osso-af-init/
endif

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

	dh_installdocs
	dh_installchangelogs
	dh_install --sourcedir=debian/tmp
ifneq (,$(findstring arm,$(DEB_BUILD_ARCH)))
	dh_installinit -pxserver-xomap --name=x-server -u"start 21 2 3 4 5 . stop 23 0 1 6 ." --no-start
endif
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_strip --dbg-package=xserver-xomap --dbg-package=xserver-xephyr
	dh_shlibdeps
	dh_gencontrol
	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
