#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

LLVM_VER=3.5

COMMON_FLAGS = USE_SYSTEM_LIBUNWIND=1 USE_SYSTEM_PCRE=1 USE_SYSTEM_BLAS=1	\
	USE_SYSTEM_LAPACK=1 USE_BLAS64=0 USE_SYSTEM_FFTW=1 USE_SYSTEM_GMP=1	\
	USE_SYSTEM_ARPACK=1 USE_SYSTEM_MPFR=1 USE_SYSTEM_SUITESPARSE=1		\
	USE_SYSTEM_GRISU=1 USE_SYSTEM_OPENSPECFUN=1 USE_SYSTEM_PATCHELF=1	\
	USE_LLVM_SHLIB=1 USE_SYSTEM_LLVM=1 LLVM_VER=$(LLVM_VER)			\
	LLVM_CONFIG=/usr/bin/llvm-config-$(LLVM_VER) VERBOSE=1			\
	MULTIARCH_INSTALL=1 sysconfdir=/etc prefix=/usr DESTDIR=debian/tmp/

ifeq ($(DEB_HOST_ARCH),amd64)
COMMON_FLAGS += MARCH=x86-64 USE_SYSTEM_OPENLIBM=1
else ifeq ($(DEB_HOST_ARCH),i386)
COMMON_FLAGS += MARCH=pentium4 USE_SYSTEM_OPENLIBM=1
else
COMMON_FLAGS += USE_SYSTEM_LIBM=1
export DEB_BUILD_OPTIONS += nocheck
endif

ifeq ($(DEB_HOST_ARCH),ppc64el)
COMMON_FLAGS += LIBBLAS=-lblas LIBBLASNAME=libblas \
                LIBLAPACK=-llapack LIBLAPACKNAME=liblapack
else 
COMMON_FLAGS += LIBBLAS=-lopenblas LIBBLASNAME=libopenblas \
                LIBLAPACK=-lopenblas LIBLAPACKNAME=libopenblas
endif

%:
	dh $@ --parallel --with=sphinxdoc

override_dh_auto_build-arch:
	make $(COMMON_FLAGS) -C base version_git.jl.phony
	make $(COMMON_FLAGS) all

override_dh_auto_build-indep:
	# Link mathjax in _static
	mkdir -p doc/_static/
	ln -s /usr/share/javascript/mathjax doc/_static/
	make -C doc html

override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	make -C test $(COMMON_FLAGS)
endif

override_dh_auto_test-indep:

override_dh_auto_clean:
	make $(COMMON_FLAGS) distcleanall
	rm -rf doc/_static/
	find doc/juliadoc -name '*.pyc' -delete # Special rule for embedded juliadoc

override_dh_auto_install-arch:
	make $(COMMON_FLAGS) install
	rm -rf usr # Otherwise dh_install does not see debian/tmp/usr

override_dh_auto_install-indep:

override_dh_strip-arch:
	dh_strip --dbg-package=julia-dbg

override_dh_installdocs:
	# Remove awesome font (dh_link will create symlinks)
	rm -f doc/_build/html/_static/fonts/fontawesome-webfont.*
	# Do not copy mathjax to the binary package
	dh_installdocs -X_static/mathjax

override_dh_sphinxdoc:
	# dh_link creates the link for mathjax after dh_sphinxdoc, so ignore
	# MathJax.js
	dh_sphinxdoc -XMathJax.js

get-orig-source:
	git clone https://github.com/JuliaLang/julia.git julia-$(DEB_VERSION_UPSTREAM)
	# Checkout right commit and submodules, download and patch dSFMT and utf8proc,
	# remove unneeded embedded copies.
	# Also manually add an embedded copy of juliadoc python package (see also dh_auto_clean rule).
	cd julia-$(DEB_VERSION_UPSTREAM) && \
		git checkout v`echo $(DEB_VERSION_UPSTREAM) | sed 's/\+dfsg\d*//' | sed 's/~/-/'` && \
		git submodule update --init && \
		make -C	deps configure-dsfmt utf8proc-v$(shell grep '^UTF8PROC_VER ' deps/Versions.make | sed 's/^UTF8PROC_VER = //')/Makefile && \
		git clone https://github.com/JuliaLang/JuliaDoc doc/juliadoc && \
		rm -rf deps/dsfmt-*.tar.gz \
		       deps/utf8proc-v*.tar.gz \
		       deps/openspecfun \
		       deps/openlibm \
		       deps/dsfmt-*/html/jquery.js \
		       .gitmodules
	tar --exclude-vcs -caf julia_$(DEB_VERSION_UPSTREAM).orig.tar.xz julia-$(DEB_VERSION_UPSTREAM)
	rm -rf julia-$(DEB_VERSION_UPSTREAM)
