#!/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.  For wheezy+1, we will switch to using debhelper 9.
# For now, preserve compatibility with squeeze.  Includes CMake workaround
# (which, again, should not be necessary after wheezy).
ifeq (mips,$(shell dpkg --print-architecture))
DEB_BUILD_MAINT_OPTIONS = hardening=+all,-relro
else ifeq (mipsel,$(shell dpkg --print-architecture))
DEB_BUILD_MAINT_OPTIONS = hardening=+all,-relro
else
DEB_BUILD_MAINT_OPTIONS = hardening=+all
endif
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)

# Synergy needs libcrypto++.  We can attempt to use packaged libcrypto++,
# or the embedded zip file of the libcrypto++ source.  To use the zip,
# remove the Build-Depends on libcrypto++-dev, add one for unzip, and
# uncomment the following line.
#USE_CRYPTOPP_DIR = tools/cryptopp

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

CFLAGS += -Wall -g
CXXFLAGS += -Wall -g
LDFLAGS += -Wl,-z,defs

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

CXXFLAGS = $(CFLAGS)

builddir:
	mkdir builddir

config.h: CMakeLists.txt builddir $(USE_CRYPTOPP_DIR)
	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)" -r)

tools/cryptopp: tools/cryptopp562.zip
	mkdir -p tools/cryptopp
	cd tools/cryptopp && unzip -a ../cryptopp562.zip

# 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))

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
	touch build-stamp

clean:
	dh_testdir
	dh_testroot

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

	find . -name \*.pyc -delete

	$(MAKE) -C src/gui distclean || true
	rm -rf src/gui/tmp

	rm -rf tools/cryptopp

	dh_clean 

test: build-stamp
	bin/unittests --gtest_color=no
	# Seems to segfault often, so don't fail
	bin/integtests --gtest_color=no || true

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 synergys.1 synergyc.1 synergyd.1 synergy.1
	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 
