#!/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

# Set build flags.
DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_BUILD_MAINT_OPTIONS

DPKG_EXPORT_BUILDFLAGS=1
include /usr/share/dpkg/buildflags.mk
CFLAGS += $(CPPFLAGS)
CXXFLAGS += $(CPPFLAGS)

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# Run the test suite unless explicitly requested not to.
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
TEST_TARGET = test
endif


builddir:
	mkdir builddir

config.h: CMakeLists.txt builddir
	dh_testdir
	(cd builddir && cmake ..)

src/gui/Makefile:
	(cd src/gui && qmake gui.pro "CONFIG+=release" "QMAKE_CFLAGS_RELEASE+=$(CFLAGS)" "QMAKE_CXXFLAGS_RELEASE+=$(CXXFLAGS)" "QMAKE_LFLAGS_RELEASE+=$(LDFLAGS)" "QMAKE_VERSION_STAGE=stable" "QMAKE_VERSION_REVISION=debian" -r)

# Man page builders.  These are better than the upstream man pages
# in doc.

%.1: debian/%.sgml
	mkdir man-$*-tmp
	cd man-$*-tmp && docbook2man ../$<
	ls man-$*-tmp/*.1 | head -n 1 | (while read f; do mv $$f $@; done)
	rm -rf man-$*-tmp

# Main targets.

bin/unittests: builddir config.h
	dh_testdir
	(cd builddir && $(MAKE) VERBOSE=1)

bin/synergy: builddir src/gui/Makefile
	dh_testdir
	(cd src/gui && $(MAKE))

src/test/guitests/Makefile:
	(cd src/test/guitests && qmake guitests.pro "CONFIG+=release" "QMAKE_CFLAGS_RELEASE+=$(CFLAGS)" "QMAKE_CXXFLAGS_RELEASE+=$(CXXFLAGS)" "QMAKE_LFLAGS_RELEASE+=$(LDFLAGS)" -r)

bin/guitests: bin/synergy builddir src/test/guitests/Makefile
	dh_testdir
	(cd src/test/guitests && $(MAKE))

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: bin/unittests bin/synergy synergy.1 synergys.1 synergyc.1 synergyd.1 syntool.1
	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	rm -f build-stamp 
	rm -f debian/substvars
	rm -rf build builddir bin lib
	rm -f synergy.1 synergys.1 synergyc.1 synergyd.1 syntool.1 config.h

	find . -name \*.pyc -delete
	find . -iwholename '*cmake*' -not -name CMakeLists.txt -delete
	$(MAKE) -C src/gui distclean || true
	$(MAKE) -C src/test/guitests distclean || true
	find . -type f -name Makefile -delete

	rm -rf src/gui/tmp
	rm -f NetworkTests.mock
	dh_clean 

test: build-stamp bin/guitests
	bin/unittests --gtest_color=no
	bin/integtests --gtest_color=no --gtest_filter=-XWindowsKeyStateTests.*:CXWindowsScreenTests.*:ArchInternetTests.get:NetworkTests.*:IpcTests.*
	bin/guitests

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install $(TEST_TARGET)
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_lintian
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install 

# Repack upstream tarball, excluded files are in the Files-Excluded paragraph of debian/copyright
get-orig-source: 
	uscan --force-download --rename --repack --download-current-version --destdir=.
