#!/usr/bin/make -f
# Based on the multi2 sample debian/rules file:
# ---
# Sample debian/rules that uses debhelper.
# This file is public domain software, originally written by Joey Hess.
#
# This version is for a multibinary package. It also allows you to build any
# of the binary packages independantly, via binary-<package> targets.
# ---

include /usr/share/quilt/quilt.make

VER_MAJOR = 2
VER_MINOR = 0
VER_PATCH = 16

VER_FULL = $(VER_MAJOR).$(VER_MINOR).$(VER_PATCH)
SOVERSION = $(VER_MAJOR).$(VER_MINOR)

UPSTREAM_SRC = gdcm-$(VER_FULL)
DEBIAN_SRC_DIR = gdcm-$(VER_FULL)
DEBIAN_SRC_TAR = gdcm_$(VER_FULL).orig.tar.gz

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

# Debian CLI Policy 4.3 [0]
# -include /usr/share/cli-common/cli.make
export MONO_DISABLE_SHM=1

# 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)
DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

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

# Mono architectures:
# Mono is not available for all Debian architectures. It supports all except:
# m68k, hppa and alpha.
DEB_WRAP_CSHARP = 1
ifneq (,$(filter m68k alpha hppa mips mipsel ,$(DEB_BUILD_ARCH)))
  DEB_WRAP_CSHARP = 0
  SKIP_ME = -Xsharp
endif

# set the number of build jobs
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
        JOBS := -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# export JAVA_HOME because upstream CMake uses it
export JAVA_HOME=/usr/lib/jvm/default-java

SRC	:= $(CURDIR)
BUILD	:= $(CURDIR)/debian/build
TARGET	:= $(CURDIR)/debian/tmp

PYVERS  := $(shell pyversions -vr)
PYMODDIR := /usr/lib/$(shell pyversions -r)

debian/configure-stamp: $(PYVERS:%=debian/configure-python%-stamp)
	touch $@

debian/configure-python%-stamp: $(QUILT_STAMPFN)
	dh_testdir
	[ -d $(BUILD)-python$* ] || mkdir $(BUILD)-python$*
	cd $(BUILD)-python$* && cmake $(CURDIR) -DCMAKE_INSTALL_PREFIX="/usr" \
		-DCMAKE_SKIP_RPATH=ON \
		-DGDCM_NO_EXECUTABLE_PROPERTIES=ON \
		-DCMAKE_VERBOSE_MAKEFILE=$(DH_VERBOSE) \
		-DGDCM_BUILD_APPLICATIONS=ON \
		-DGDCM_DOCUMENTATION:BOOL=ON \
		-DGDCM_PDF_DOCUMENTATION:BOOL=OFF \
		-DGDCM_BUILD_SHARED_LIBS=ON \
		-DGDCM_WRAP_PYTHON=ON \
		-DGDCM_WRAP_CSHARP=$(DEB_WRAP_CSHARP) \
		-DGDCM_WRAP_JAVA=ON \
		-DGMCS_EXECUTABLE:FILEPATH=/usr/bin/mono-csc \
		-DDESIRED_CSHARP_COMPILER_VERSION=2 \
		-DGDCM_BUILD_TESTING:BOOL=OFF \
		-DCMAKE_BUILD_TYPE:STRING=Release \
		-DGDCM_USE_SYSTEM_EXPAT:BOOL=ON \
		-DGDCM_USE_SYSTEM_UUID:BOOL=ON \
		-DGDCM_USE_SYSTEM_ZLIB:BOOL=ON \
		-DGDCM_USE_SYSTEM_OPENJPEG:BOOL=ON \
		-DGDCM_USE_SYSTEM_OPENSSL:BOOL=ON \
		-DGDCM_USE_SYSTEM_POPPLER:BOOL=ON \
		-DGDCM_VTK_JAVA_JAR:FILEPATH=/usr/share/java/vtk.jar \
		-DGDCM_USE_VTK:BOOL=ON
	touch $@

build: debian/build-stamp

debian/build-stamp: $(PYVERS:%=debian/build-python%-stamp)
	touch $@

debian/build-python%-stamp: debian/configure-stamp
	dh_testdir
	$(MAKE) $(JOBS) -C $(BUILD)-python$*
	-(cd $(BUILD)-python$* && LD_LIBRARY_PATH=$(BUILD)-python$*/gdcm ctest --force-new-ctest-process -V)
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f debian/configure*stamp debian/build*stamp
	rm -rf debian/build*
	dh_clean

install: DH_OPTIONS=
install: debian/build-stamp install-common $(PYVERS:%=install-python%)
install-common:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

install-python%:
	$(MAKE) $(JOBS) -C $(BUILD)-python$* install DESTDIR=$(TARGET)

	# correct location of python-gdcm files
	mv $(TARGET)/usr/lib/python $(TARGET)/$(PYMODDIR)

# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything you want to only affect one package
# should be put in another target, such as the install target.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_lintian
	dh_install --sourcedir=$(TARGET) --fail-missing
	dh_strip --dbg-package=libgdcm2.0-dbg
	dh_link
	dh_compress
	dh_fixperms
	dh_pysupport
#	dh_makeclilibs
	dh_makeshlibs -Xformats -V
	dh_installdeb
	LD_LIBRARY_PATH="/usr/lib/jni$${LD_LIBRARY_PATH:+:}$$LD_LIBRARY_PATH" dh_shlibdeps -v -a -l /usr/lib/jvm/default-java/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: install
# (Uncomment this next line if you have such packages.)
#	 $(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: install
	$(MAKE) -f $(SRC)/debian/rules DH_OPTIONS=-s binary-common

# Any other binary targets build just one binary package at a time.
binary-%: install
	make -f $(SRC)/debian/rules binary-common DH_OPTIONS=-p$*

$(UPSTREAM_SRC).tar.gz:
	wget http://voxel.dl.sourceforge.net/sourceforge/gdcm/$@

get-orig-source: $(UPSTREAM_SRC).tar.gz
	tar xzf $(UPSTREAM_SRC).tar.gz
	GZIP="--best --no-name" tar czf $(DEBIAN_SRC_TAR) $(DEBIAN_SRC_DIR)
	rm -rf $(DEBIAN_SRC_DIR)

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