#!/usr/bin/make -f
# debian/rules for the Debian tigervnc package.

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

# Where the tigervnc sources are
DEB_SRCDIR   = tigervnc
# Don't override DEB_BUILDDIR as /usr/share/cdbs/1/class/cmake.mk does not
# support this
## Where to built tigervnc
#DEB_BUILDDIR = $(CURDIR)/obj-$(DEB_BUILD_GNU_TYPE)

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/cmake.mk

# Do our complex patch dance first! After that quilt patch system can proceed!
clean:: unpatch clean-xorg
unpatch: reverse-patches-dance
apply-patches: apply-patches-dance
reverse-patches-dance: reverse-patches

QUILT_PATCH_DIR = $(CURDIR)/debian/patchdir
include /usr/share/cdbs/1/rules/patchsys-quilt.mk

DEB_DH_AUTORECONF_ARGS       = $(CURDIR)/debian/xorg-autoreconf.sh
DEB_DH_AUTORECONF_CLEAN_ARGS =

#include /usr/share/cdbs/1/rules/autoreconf.mk we need the deautoreconf in the unpatch sequence

# --disable-maintainer-mode does not seem to do its job! Why?!
DEB_CONFIGURE_EXTRA_FLAGS += --disable-maintainer-mode

DEB_CMAKE_INSTALL_PREFIX  = /usr
DEB_CMAKE_EXTRA_FLAGS     += -DUSE_INCLUDED_ZLIB:BOOL=OFF
# The FLTK in debian is either too old (squeeze) or is missing some extension patches (wheezy)
# required by TigerVNC, i.e., http://www.fltk.org/strfiles/2599/fltk-1.3.x-keyboard-x11.patch.
# Maybe some more patches are missing. I have not checked further.
DEB_CMAKE_EXTRA_FLAGS     += -DUSE_INCLUDED_FLTK:BOOL=ON
DEB_CMAKE_EXTRA_FLAGS     += -DENABLE_LIBWRAP:BOOL=ON
DEB_CMAKE_EXTRA_FLAGS     += -DENABLE_NLS:BOOL=ON
DEB_CMAKE_EXTRA_FLAGS     += -DENABLE_GNUTLS:BOOL=ON
DEB_CMAKE_EXTRA_FLAGS     += -DENABLE_PAM:BOOL=ON

# rawr paranoia
CFLAGS   += -Wall -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security
CXXFLAGS += -Wall -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security
CPPFLAGS += -D_FORTIFY_SOURCE=2
LDFLAGS  += -Wl,-z,relro
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS   += -O0
	CXXFLAGS += -O0
else
	CFLAGS   += -O3 -DNDEBUG
	CXXFLAGS += -O3 -DNDEBUG
endif
# We need that as /usr/share/cdbs/1/class/cmake.mk is buggy and does not use the :STRING qualifier
DEB_CMAKE_EXTRA_FLAGS += -DCMAKE_C_FLAGS:STRING="$(CPPFLAGS) $(CFLAGS)"
DEB_CMAKE_EXTRA_FLAGS += -DCMAKE_CXX_FLAGS:STRING="$(CPPFLAGS) $(CXXFLAGS)"
DEB_CMAKE_EXTRA_FLAGS += -DCMAKE_EXE_LINKER_FLAGS="$(LDFLAGS)"
DEB_CMAKE_EXTRA_FLAGS += -DCMAKE_SHARED_LINKER_FLAGS="$(LDFLAGS)"

# This is stolen from xorg-server/debian/xsfbs/xsfbs.mk
# Determine (source) package name from Debian changelog.
SOURCE_NAME:=$(shell dpkg-parsechangelog -ldebian/changelog \
                        | grep '^Source:' | awk '{print $$2}')

# Determine package version from Debian changelog.
SOURCE_VERSION:=$(shell dpkg-parsechangelog -ldebian/changelog \
                        | grep '^Version:' | awk '{print $$2}')

# Determine upstream version number.
UPSTREAM_VERSION:=$(shell echo $(SOURCE_VERSION) | sed 's/-.*//')

# Determine the source version without the epoch for make-orig-tar-gz
NO_EPOCH_VER:=$(shell echo $(UPSTREAM_VERSION) | sed 's/^.://')

# Highly modified and adapted from http://wiki.debian.org/SandroTosi/Svn_get-orig-source
TIGERVNC_PACKAGE = tigervnc
TIGERVNC_VERSION = $(shell echo ${NO_EPOCH_VER} | cut -d"+" -f1)
TIGERVNC_TARBALL = $(TIGERVNC_PACKAGE)_$(TIGERVNC_VERSION).orig.tar.gz

X11_PACKAGE = xorg-server
X11_VERSION = $(shell echo ${NO_EPOCH_VER} | cut -d"X" -f2 | cut -d"-" -f1)
X11_TARBALL = $(X11_PACKAGE)_$(X11_VERSION).orig.tar.gz

debug-debian-rules:
	@echo '$$(DEB_CONFIGURE_EXTRA_FLAGS) $(DEB_CONFIGURE_EXTRA_FLAGS)'
	@echo '$$(DEB_CMAKE_EXTRA_FLAGS) $(DEB_CMAKE_EXTRA_FLAGS)'
	@echo '$$(SOURCE_NAME) $(SOURCE_NAME)'
	@echo '$$(SOURCE_VERSION) $(SOURCE_VERSION)'
	@echo '$$(UPSTREAM_VERSION) $(UPSTREAM_VERSION)'
	@echo '$$(NO_EPOCH_VER) $(NO_EPOCH_VER)'
	@echo '$$(TIGERVNC_PACKAGE) $(TIGERVNC_PACKAGE)'
	@echo '$$(TIGERVNC_VERSION) $(TIGERVNC_VERSION)'
	@echo '$$(TIGERVNC_TARBALL) $(TIGERVNC_TARBALL)'
	@echo '$$(X11_PACKAGE) $(X11_PACKAGE)'
	@echo '$$(X11_VERSION) $(X11_VERSION)'
	@echo '$$(X11_TARBALL) $(X11_TARBALL)'

.PHONY: apply-patches-dance reverse-patches-dance

apply-patches-dance: apply-patches-vnc-patch-xorg
apply-patches-vnc-patch-xorg: apply-patches-unify-xorg-and-vnc-tree
apply-patches-unify-xorg-and-vnc-tree: apply-patches-xorg

reverse-patches-dance: reverse-patches-xorg
reverse-patches-xorg: reverse-patches-remove-xorg-from-vnc-tree
reverse-patches-remove-xorg-from-vnc-tree: reverse-patches-vnc-unpatch-xorg
reverse-patches-vnc-unpatch-xorg: reverse-patches-deautoreconf

.PHONY: reverse-patches-deautoreconf

post-patches:: $(DEB_SRCDIR)/unix/xserver/.autoreconf-xorg.stamp

$(DEB_SRCDIR)/unix/xserver/.autoreconf-xorg.stamp:
	dh_autoreconf $(DEB_DH_AUTORECONF_ARGS) && touch $@

reverse-patches-deautoreconf:
	if test -f $(DEB_SRCDIR)/unix/xserver/.autoreconf-xorg.stamp; then \
	  dh_autoreconf_clean $(DEB_DH_AUTORECONF_CLEAN_ARGS) && \
	  rm -f $(DEB_SRCDIR)/unix/xserver/.autoreconf-xorg.stamp \
	  	$(CURDIR)/debian/autoreconf.before \
	  	$(CURDIR)/debian/autoreconf.after; \
	else \
	  rm -f $(CURDIR)/debian/autoreconf.before \
	  	$(CURDIR)/debian/autoreconf.after; \
	fi

.PHONY: apply-patches-vnc-patch-xorg reverse-patches-vnc-unpatch-xorg

apply-patches-vnc-patch-xorg: $(DEB_SRCDIR)/unix/xserver/.apply-patches-vnc-patch-xorg.stamp

$(DEB_SRCDIR)/unix/xserver/.apply-patches-vnc-patch-xorg.stamp: debian/xserver.patch
	cd $(DEB_SRCDIR)/unix/xserver && { \
		patch --no-backup-if-mismatch -p1 < ../../../debian/xserver.patch && \
		touch .apply-patches-vnc-patch-xorg.stamp; \
	}

reverse-patches-vnc-unpatch-xorg: debian/xserver.patch
	cd $(DEB_SRCDIR)/unix/xserver && if test -f .apply-patches-vnc-patch-xorg.stamp; then \
		patch --no-backup-if-mismatch -R -p1 < ../../../debian/xserver.patch; \
		rm -f .apply-patches-vnc-patch-xorg.stamp; \
	fi

.PHONY: apply-patches-unify-xorg-and-vnc-tree reverse-patches-remove-xorg-from-vnc-tree

apply-patches-unify-xorg-and-vnc-tree: $(DEB_SRCDIR)/unix/xserver/.apply-patches-unify-xorg-and-vnc-tree.stamp

$(DEB_SRCDIR)/unix/xserver/.apply-patches-unify-xorg-and-vnc-tree.stamp:
	cd $(DEB_SRCDIR)/unix/xserver && { \
		lndir ../../../xorg-server . && \
		find -type l > .apply-patches-unify-xorg-and-vnc-tree.stamp; \
	}

reverse-patches-remove-xorg-from-vnc-tree:
	cd $(DEB_SRCDIR)/unix/xserver && if test -f .apply-patches-unify-xorg-and-vnc-tree.stamp; then \
		xargs rm -f < .apply-patches-unify-xorg-and-vnc-tree.stamp; \
		find -name ".svn" -prune -o -type d -print | sort -r | xargs rmdir; \
		rm -f .apply-patches-unify-xorg-and-vnc-tree.stamp; \
	fi

.PHONY: apply-patches-xorg reverse-patches-xorg clean-xorg

apply-patches-xorg:
	cd $(CURDIR)/xorg-server && chmod a+x debian/rules && \
	  debian/rules stampdir/stampdir && debian/rules patch

reverse-patches-xorg:
	cd $(CURDIR)/xorg-server && chmod a+x debian/rules && debian/rules unpatch

clean-xorg:
	cd $(CURDIR)/xorg-server && chmod a+x debian/rules && debian/rules clean

$(DEB_BUILDDIR)/unix/xserver/debian/confflags:
	mkdir -p $(dir $@)
	( cd $(DEB_SRCDIR)/unix/xserver && chmod a+x debian/rules && debian/rules echo_confflags ) | \
	  sed -ne '/^CONFFLAGS:/{s/^[^:]*:\s*//;p}' > $@

configure/tigervnc-standalone-server configure/tigervnc-xorg-extension:: $(DEB_BUILDDIR)/unix/xserver/configure.stamp

XORG_DEBIAN_CONFIGURE_FLAGS = $(shell cat $(DEB_BUILDDIR)/unix/xserver/debian/confflags)

# Next step is run configure script. It is very difficult use correct parameters.
# You should use same parameters as used in your distribution X server and add
# --disable-xvfb --disable-xnest --disable-xorg
$(DEB_BUILDDIR)/unix/xserver/configure.stamp: $(DEB_BUILDDIR)/unix/xserver/debian/confflags
	mkdir -p $(DEB_BUILDDIR)/unix/xserver
	cd $(DEB_BUILDDIR)/unix/xserver && \
	  $(DEB_CONFIGURE_SCRIPT_ENV) ../../../tigervnc/unix/xserver/configure \
		CPPFLAGS="$(CPPFLAGS)" \
		CFLAGS="$(CFLAGS)" \
		CXXFLAGS="$(CXXFLAGS)" \
		LDFLAGS="$(LDFLAGS) -lpam" \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--sysconfdir=/etc \
		--localstatedir=/var \
		$(XORG_DEBIAN_CONFIGURE_FLAGS) \
		$(DEB_CONFIGURE_EXTRA_FLAGS) \
		--with-builderstring="tigervnc-xorg-server $(NO_EPOCH_VER) (Joachim Falk <joachim.falk@gmx.de>)" \
		--disable-xfbdev \
		--disable-dmx \
		--disable-xvfb \
		--disable-xnest \
		--disable-xephyr \
		--disable-xorg \
		--disable-dri \
		--disable-dri2
	touch $@

build/tigervnc-standalone-server build/tigervnc-xorg-extension::
	$(MAKE) -C $(DEB_BUILDDIR)/unix/xserver

install/tigervnc-common::
	mkdir -p $(DEB_DESTDIR)/usr/share/doc
	mv $(DEB_DESTDIR)/usr/man \
	  $(DEB_DESTDIR)/usr/share
	mv $(DEB_DESTDIR)/usr/doc \
	  $(DEB_DESTDIR)/usr/share/doc/tigervnc-common

install/tigervnc-viewer::
	mv $(DEB_DESTDIR)/usr/bin/vncviewer \
	  $(DEB_DESTDIR)/usr/bin/xvncviewer
	mv $(DEB_DESTDIR)/usr/share/man/man1/vncviewer.1 \
	  $(DEB_DESTDIR)/usr/share/man/man1/xvncviewer.1

install/tigervnc-standalone-server install/tigervnc-xorg-extension::
	$(MAKE) -C $(DEB_BUILDDIR)/unix/xserver/hw/vnc DESTDIR=$(DEB_DESTDIR) install

common-binary-arch common-binary-indep:: debian/tmp/common-binary-impl.stamp

debian/tmp/common-binary-impl.stamp:
	cd $(DEB_DESTDIR) && find usr/bin usr/share/man -name "*vnc*" | while read line; do \
	  mv "$$line" `echo "$$line" | sed -e 's/vnc/tigervnc/g'`; \
	done
	cp -pr debian/helpers/* \
	  debian/tmp
	find debian/tmp/usr/share/man -type f | while read line; do \
	  sed -e 's/\<vncviewer\>/xtigervncviewer/g' \
	      -e 's/\<vncpasswd\>/tigervncpasswd/g' \
	      -e 's/\<vncconfig\>/tigervncconfig/g' \
	      -e 's/\<vncserver\>/tigervncserver/g' \
	      -e 's/\<Xvnc\>/Xtigervnc/g' $$line | \
	    gzip -9 > $$line.gz; \
	  rm -f $$line; \
	done
	touch $@

get-orig-source:
	git submodule update --init || { echo "Ups... Can't fetch subprojects!"; exit 1; }
	( cd $(DEB_SRCDIR) && pristine-tar checkout ../$(TIGERVNC_TARBALL) ) && \
	  mv -f $(DEB_SRCDIR)/../$(TIGERVNC_TARBALL) $(CURDIR)/../tigervnc_$(NO_EPOCH_VER).orig-$(TIGERVNC_PACKAGE).tar.gz
	( cd $(CURDIR) && { git fetch origin pristine-tar:pristine-tar || true; } ) && { \
	  cp -pr $(CURDIR)/xorg-server $(CURDIR)/xorg-server-help && \
	  ( cd $(CURDIR)/xorg-server-help && \
	    if test -f .git; then \
	      gitdir=`sed -ne '/^gitdir:/{s/^[^:]*:\s*//;p}' .git`; \
	      test x"$$gitdir" != x"" -a -d "$$gitdir" || exit 1; \
	      rm -f .git && cp -pr "$$gitdir" .git || exit 1; \
	      sed -e '/worktree/d' $$gitdir/config > .git/config || exit 1; \
	    fi && \
            { git checkout -b pristine-tar remotes/origin/pristine-tar || true; } && \
	    { git remote add pkg-tigervnc .. || true; } && \
	    git fetch pkg-tigervnc && \
	    git merge remotes/pkg-tigervnc/pristine-tar -m "Merge in pristine-tar delta+id files for currently used xorg-server version." && \
	    pristine-tar checkout ../$(X11_TARBALL) ) && \
	  mv -f $(CURDIR)/$(X11_TARBALL) $(CURDIR)/../tigervnc_$(NO_EPOCH_VER).orig-$(X11_PACKAGE).tar.gz && \
	  rm -rf $(CURDIR)/xorg-server-help; } || { rm -rf $(CURDIR)/xorg-server-help; exit 1; }

.PHONY: get-orig-source

clean::
	rm -rf $(DEB_BUILDDIR)
