#!/usr/bin/make -f

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

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/simple-patchsys.mk
include /usr/share/cdbs/1/rules/utils.mk

# Version inforamtion
VA_MAJOR_VERSION := $(shell echo $(DEB_UPSTREAM_VERSION) | cut -d'.' -f1)
VA_MINOR_VERSION := $(shell echo $(DEB_UPSTREAM_VERSION) | cut -d'.' -f2)

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

# Allow SMP build
ifeq ($(DEBIAN_BUILD_NCPUS),)
	DEBIAN_BUILD_NCPUS = $(shell /usr/bin/getconf _NPROCESSORS_ONLN)
endif
ifneq ($(DEBIAN_BUILD_NCPUS),)
	EXTRA_MAKE_FLAGS += -j$(DEBIAN_BUILD_NCPUS)
endif
MAKE += $(EXTRA_MAKE_FLAGS)

# Detect libdrm2 version
LIBDRM_VERSION := $(shell dpkg-query -W -f='$${Version}\n' libdrm2 2>/dev/null)
has-libdrm-2.4 := $(shell dpkg --compare-versions $(LIBDRM_VERSION) ge 2.4 && echo yes || echo no)

# Extra configure flags
DEB_CONFIGURE_SCRIPT_ENV += LDFLAGS="-Wl,-z,defs"

ifeq ($(has-libdrm-2.4),yes)
DEB_CONFIGURE_USER_FLAGS += --enable-i965-driver
else
DEB_CONFIGURE_USER_FLAGS += --disable-i965-driver
endif

update-config:: run-autoreconf
common-binary-predeb-arch:: list-missing

run-autoreconf: stamp-autoreconf
stamp-autoreconf:
	autoreconf -i
	touch $@

# Compatibility with older packaging (and SONAME)
install/libva1::
	echo ".text"|gcc -xassembler - -o debian/tmp/usr/lib/libva.so.0.29 -shared -Wl,-soname,libva.so.0 -Ldebian/tmp/usr/lib -lva-compat
	echo ".text"|gcc -xassembler - -o debian/tmp/usr/lib/libva.so.1.0.0 -shared -Wl,-soname,libva.so.1 -Ldebian/tmp/usr/lib -lva
	echo ".text"|gcc -xassembler - -o debian/tmp/usr/lib/libva-x11.so.1.0.0 -shared -Wl,-soname,libva-x11.so.1 -Ldebian/tmp/usr/lib -lva-x11
	echo ".text"|gcc -xassembler - -o debian/tmp/usr/lib/libva-glx.so.1.0.0 -shared -Wl,-soname,libva-glx.so.1 -Ldebian/tmp/usr/lib -lva-glx
	/sbin/ldconfig -n debian/tmp/usr/lib

# Regenerate debian/control
regen-control: debian/control.in debian/control.driver.i965
	sed -e 's/@LIBDRM_VERSION@/$(LIBDRM_VERSION)/'		  \
	  < debian/control.in					  \
	  > debian/control
	if test "$(has-libdrm-2.4)" = "yes"; then		  \
	  echo >> debian/control				; \
	  cat debian/control.driver.i965 >> debian/control	; \
	fi
