#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

include debian/rules.flags

%:
	dh $@

override_dh_auto_configure:
	# work-around for missing symlink patches.xorg/latest-available/
	ln -s 21.1.8 patches.xorg/latest-available

	# prepare xorg-server build tree
	mkdir -p '$(CURDIR)/BUILD'
	cd '$(CURDIR)/BUILD' && tar -xvf '/usr/src/xorg-server.tar.xz'
	mkdir -p '$(CURDIR)/BUILD/xorg-server/hw/kdrive/x2gokdrive/'

	# inject x2gokdrive into xorg-server build tree
	cp 'Makefile.am' *'.c' *'.h' '$(CURDIR)/BUILD/xorg-server/hw/kdrive/x2gokdrive/'
	cp -r '$(CURDIR)/man/' 'BUILD/xorg-server/hw/kdrive/x2gokdrive/'

	# patch xorg-server build tree, so that it will build x2gokdrive
	set -x; \
		export XORG_UPSTREAM_VERSION="$$(grep AC_INIT '$(CURDIR)/BUILD/xorg-server/configure.ac' | sed -r 's/^AC_INIT[^,]*, ([^,]+),.*/\1/')" && \
		cd '$(CURDIR)/BUILD/xorg-server' && \
		if [ -d  "../../patches.xorg/$${XORG_UPSTREAM_VERSION}" ]; then \
			QUILT_PATCHES="../../patches.xorg/$${XORG_UPSTREAM_VERSION}/" quilt pop -a; \
			rm .pc/ -Rfv; \
			QUILT_PATCHES="../../patches.xorg/$${XORG_UPSTREAM_VERSION}/" quilt push -a; \
		else \
			QUILT_PATCHES='../../patches.xorg/latest-available/' quilt pop -a; \
			rm -Rfv '.pc'; \
			QUILT_PATCHES='../../patches.xorg/latest-available/' quilt push -a; \
			QUILT_EXITCODE="$${?}"; \
			if [ '0' != "$${QUILT_EXITCODE}" ]; then \
				( \
					set +x; \
					printf '\n##################################################\nERROR: This X2Go KDrive version does not support\nbuilding against X.Org version %s.\n##################################################\n' "$${XORG_UPSTREAM_VERSION}"; \
					exit '1'; \
				); \
			fi; \
		fi

	# run autoreconf
	cd '$(CURDIR)/BUILD/xorg-server' && autoreconf -fvi

	# run configure
	dh_auto_configure --builddirectory='BUILD/xorg-server/' --sourcedirectory='BUILD/xorg-server/' -- \
	    $(confflags) \
	    $(confflags_main) \
	    $(vars)

override_dh_auto_build:
	dh_auto_build --builddirectory='BUILD/xorg-server/' --sourcedirectory='BUILD/xorg-server/'

override_dh_auto_test:
	# disabled, nothing to test for x2gokdrive...

override_dh_auto_install:
	dh_auto_install --builddirectory='BUILD/xorg-server/' --destdir='debian/tmp/'

	# drop unwanted files
	rm       debian/tmp/usr/lib/xorg/protocol.txt
	rmdir -p --ignore-fail-on-non-empty \
                 debian/tmp/usr/lib/xorg/
	rm       debian/tmp/usr/share/man/man1/Xserver.1
	rmdir -p --ignore-fail-on-non-empty \
                 debian/tmp/usr/share/man/man1/
	rm       debian/tmp/var/lib/xkb/README.compiled
	rmdir -p --ignore-fail-on-non-empty \
                 debian/tmp/var/lib/xkb/

override_dh_auto_clean:
	-dh_auto_clean

override_dh_clean:
	-dh_clean
	rm -Rf '$(CURDIR)/BUILD/'

	# work-around for missing symlink patches.xorg/latest-available/
	rm -f patches.xorg/latest-available

get-orig-source:
	uscan --noconf --force-download --rename --download-current-version --destdir=..
