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

# 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)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

include /usr/share/quilt/quilt.make

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS_RELEASE = -O0 -g -DNDEBUG $(CFLAGS)
	CFLAGS_DEBUG   = -O0 -g $(CFLAGS)
else
	CFLAGS_RELEASE = -O2 -g -DNDEBUG $(CFLAGS)
	CFLAGS_DEBUG   = -O2 -g $(CFLAGS)
endif

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 -DRESOURCEDIR=/usr/share/freemat -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) \
	      -DCMAKE_C_FLAGS_RELEASE="$(CFLAGS_RELEASE)" -DCMAKE_C_FLAGS_DEBUG="$(CFLAGS_DEBUG)" \
	      -DCMAKE_SKIP_RPATH=ON -DUSE_LLVM=OFF -DFORCE_BUNDLED_PCRE=OFF -DFORCE_BUNDLED_UMFPACK=OFF \
	      -DFORCE_BUNDLED_PORTAUDIO=OFF -DFORCE_BUNDLED_ZLIB=OFF -DFORCE_BUNDLED_AMD=OFF

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
endif


debian/build/CMakeCache.txt: $(QUILT_STAMPFN)
	mkdir -p debian/build
	cd $(CURDIR)/debian/build && \
		cmake $(BUILD_FLAGS) ../..
debian/build: debian/build/CMakeCache.txt


build: build-stamp

build-stamp: debian/build
	dh_testdir
	$(MAKE) -C debian/build $(MAKEFLAGS) FreeMat
	#cd help/latex && pdflatex FreeMat-4.0.tex && pdflatex FreeMat-4.0.tex
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf debian/build/
	find . -name '*.moc.cpp' -delete
	dh_clean CMakeCache.txt dependencies/volpack-1.0c7/config.log dependencies/volpack-1.0c7/config.status

install: build
	dh_testdir
	dh_testroot
	dh_prep 
	dh_installdirs
	$(MAKE) -C debian/build DESTDIR=$(CURDIR)/debian/tmp install
	#manually copy the xpm file
	cp -f images/freemat-2.xpm debian/freemat/usr/share/pixmaps/freemat/freemat.xpm


# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_installexamples -i
	dh_install -pfreemat-data debian/tmp/usr/share/FreeMat-4.0/toolbox usr/share/freemat
	dh_install -pfreemat-help debian/tmp/usr/share/FreeMat-4.0/help usr/share/freemat
	# Add the lintian override file
	cp debian/freemat-data.lintian-overrides debian/freemat-data/usr/share/lintian/overrides/freemat-data
	dh_install -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -s
	dh_testroot -s
	dh_installchangelogs -s ChangeLog
	dh_installdocs -s
	dh_installexamples -s
	dh_installmenu -s
	dh_install -s
	mv debian/freemat/usr/bin/FreeMat debian/freemat/usr/bin/freemat
	rm -f debian/freemat/usr/bin/blas.ini
	dh_installman -pfreemat debian/freemat.1
	dh_link -s
	dh_strip -s
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -s
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

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