#!/usr/bin/make -f
# -*- makefile -*-
#
# Makefile to build the mCRL2 package using debhelper.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=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)

CFLAGS = $(shell dpkg-buildflags --get CFLAGS)
CFLAGS += $(shell dpkg-buildflags --get CPPFLAGS)
CXXFLAGS = $(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)

BUILD_TYPE = Release
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	BUILD_TYPE = Debug
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))  
    MAKEFLAGS += -j$(NUMJOBS)
endif

BUILD_FLAGS = -DCMAKE_INSTALL_PREFIX="/usr" \
              -DCMAKE_INSTALL_RPATH="/usr/lib/mcrl2" \
	      -DCMAKE_BUILD_TYPE="$(BUILD_TYPE)" \
	      -DCMAKE_VERBOSE_MAKEFILE="ON" \
	      -DMCRL2_ENABLE_EXPERIMENTAL="OFF" \
	      -DMCRL2_ENABLE_DEPRECATED="OFF"	 \
	      -DMCRL2_ENABLE_TEST_TARGETS="OFF" \
	      -DMCRL2_MAN_PAGES="ON" \
	      -DMCRL2_PACKAGE_RELEASE="ON"

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
    BUILD_FLAGS += -DCMAKE_SYSTEM_NAME=$(shell dpkg-architecture -qDEB_BUILD_ARCH_OS) \
		   -DCMAKE_SYSTEM_PROCESSOR=$(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU) \
		   -DCMAKE_C_COMPILER=$(DEB_BUILD_GNU_TYPE)-gcc \
		   -DCMAKE_CXX_COMPILER=$(DEB_BUILD_GNU_TYPE)-g++
endif

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

configure: configure-stamp

configure-stamp:
	dh_testdir
	[ -d $(BUILD) ] || mkdir $(BUILD)
	cd $(BUILD) && \
	  cmake $(BUILD_FLAGS) $(CURDIR) 
	touch $@

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp: configure
	dh_testdir
	$(MAKE) -C $(BUILD) $(MAKEFLAGS)
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -rf $(BUILD)
	rm -f configure-stamp build-stamp 
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	$(MAKE) -C $(BUILD) install DESTDIR=$(TARGET)
	# Move the examples and man-pages to the correct location
	mkdir -p $(TARGET)/usr/share/doc/mcrl2 && \
	  mv $(TARGET)/usr/share/mcrl2/doc/* $(TARGET)/usr/share/doc/mcrl2 && \
	  rmdir $(TARGET)/usr/share/mcrl2/doc
	mv $(TARGET)/usr/share/mcrl2/man $(TARGET)/usr/share

# 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
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -V --exclude=/usr/lib/mcrl2
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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