#!/usr/bin/make -f
#export DH_VERBOSE=1

# bring in VERSION/ARCH/BUILD variables
include /usr/share/dpkg/default.mk
include debian/mono-archs.make

RUN_MONO = MONO_PATH=debian/tmp/usr/lib/mono/4.5 runtime/mono-wrapper
DH_INTERNAL_MONO_PARAM = --internal-mono

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	CONF_FLAGS += --build $(DEB_HOST_GNU_TYPE)
else
	CONF_FLAGS += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
	CONF_FLAGS += --enable-minimal=aot
endif

ifeq ($(DEB_HOST_ARCH), armel)
	CONF_FLAGS += --with-fpu=NONE --with-tls=pthread
	BTLS_ARCH = true
else ifeq ($(DEB_HOST_ARCH), armhf)
	CONF_FLAGS += --with-tls=pthread
	BTLS_ARCH = true
else ifeq ($(DEB_HOST_ARCH), arm64)
	SKIP_LIBGC = true
	BTLS_ARCH = true
else ifeq ($(DEB_HOST_ARCH), i386)
	BTLS_ARCH = true
else ifeq ($(DEB_HOST_ARCH), amd64)
	BTLS_ARCH = true
else ifeq ($(DEB_HOST_ARCH), mipsel)
	CONF_FLAGS += --with-tls=pthread
else ifeq ($(DEB_HOST_ARCH), powerpc)
	CONF_FLAGS += --with-tls=pthread
else ifeq ($(DEB_HOST_ARCH), ppc64)
	CONF_FLAGS += --with-tls=pthread
else ifeq ($(DEB_HOST_ARCH), ppc64el)
	CONF_FLAGS += --with-tls=pthread
	BTLS_ARCH = true
else ifeq ($(DEB_HOST_ARCH), s390x)
	BTLS_ARCH = true
endif

ifneq (,$(findstring $(DEB_HOST_ARCH),$(DEB_MONO_SGEN_ARCHS)))
	CONF_FLAGS += --with-sgen=yes
else
	CONF_FLAGS += --with-sgen=no
endif

# Skip building Boehm's gargabe collector on unsupported architectures.
ifeq ($(DEB_HOST_ARCH), arm64)
	DISABLE_BOEHM = true
endif
ifdef DISABLE_BOEHM
	CONF_FLAGS += --disable-boehm
endif

%:
	dh $@

CONFIGURE = --libdir=/usr/lib/ --libexecdir=/usr/lib/ \
	--with-ikvm-native=no --disable-quiet-build --with-csc=mcs \
	--disable-system-aot --enable-llvm=no --enable-loadedllvm=no \
	$(CONF_FLAGS)


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

override_dh_auto_configure:
	dh_auto_configure -- $(CONFIGURE)

override_dh_auto_build:
	cd mono/eglib && $(MAKE) $(MONO_MAKE_FLAGS)
ifndef SKIP_LIBGC
	cd external/bdwgc && $(MAKE) $(MONO_MAKE_FLAGS)
endif
	cd mono && $(MAKE) $(MONO_MAKE_FLAGS)
	LC_ALL=C $(MAKE) $(MAKE_FLAGS) EXTERNAL_MCS=false EXTERNAL_MONO=false

	cd debian/detector && $(MAKE)
	chmod +x debian/dh_makeclilibs \
	         debian/dh_clistrip
	cp -a ./external/binary-reference-assemblies/v4.7.1 ./external/binary-reference-assemblies/v4.7.1.debbak
	find ./external/binary-reference-assemblies/v4.7.1/ -name \*.dll -print -delete
	MONO_PATH=$(CURDIR)/mcs/class/lib/net_4_x-linux/ $(MAKE) $(MONO_MAKE_FLAGS) -C external/binary-reference-assemblies/ \
		V=$(DH_VERBOSE) CSC="$(CURDIR)/runtime/mono-wrapper $(CURDIR)/mcs/class/lib/net_4_x-linux/mcs.exe"
	# Build the late-GAC tool, using the built Mono runtime
	MONO_PATH=$(CURDIR)/mcs/class/lib/net_4_x-linux/ runtime/mono-wrapper \
		mcs/class/lib/net_4_x-linux/mcs.exe -target:exe \
		-out:debian/MonoGetAssemblyName.exe debian/MonoGetAssemblyName.cs

override_dh_auto_test:
	-cd mono/mini && timeout -k 15m 1h make check
	-cd mono/tests && MONO_ENV_OPTIONS="$(MONO_ENV_OPTIONS) --gc=boehm" timeout -k 15m 1h make check
	-cd mono/tests && MONO_ENV_OPTIONS="$(MONO_ENV_OPTIONS) --gc=sgen" timeout -k 15m 1h make check
	# funny bug-459094.cs creates too long directory names which
	# pbuilder is unable to remove, eh
	rm -rf mono/tests/abcde12345abcde12345abcde12345abcde12345/

override_dh_auto_clean:
ifneq ("$(wildcard external/binary-reference-assemblies/v4.7.1.debbak)","")
		rm -rf external/binary-reference-assemblies/v4.7.1
		mv external/binary-reference-assemblies/v4.7.1.debbak external/binary-reference-assemblies/v4.7.1
endif
	[ ! -f Makefile ] || $(MAKE) distclean
	# distclean misses stuff
	find -name "*.mdb" -delete
	find ./external/binary-reference-assemblies/ -name "*.dll" -not -path "./external/binary-reference-assemblies/v4.7.1/*" -print -delete
	rm -rf mcs/class/lib/basic/ \
	       mcs/class/lib/net_4_5/
	-cd debian/detector && $(MAKE) clean
	rm -f debian/MonoGetAssemblyName.exe
	rm -f mcs/packages/*.dll mcs/packages/*.exe* mcs/packages/*.rsp
	dh_auto_clean

execute_before_dh_install:
	# Boehm's garbage collector is not built on all architectures,
	# so can not be handled by debian/${pkg}.install files.
ifndef DISABLE_BOEHM
	install -D --mode=755 \
		--target-directory=debian/mono-runtime/usr/bin/ \
		debian/tmp/usr/bin/mono-boehm
	install -D --mode=644 \
		--target-directory=debian/mono-devel/usr/lib/ \
		debian/tmp/usr/lib/libmonoboehm-2.0.a \
		debian/tmp/usr/lib/libmonoboehm-2.0.so.1.0.0
	## install always dereference symbolic links,
	## so we need to create them explicitly.
	ln --symbolic libmonoboehm-2.0.so.1.0.0 \
		debian/mono-devel/usr/lib/libmonoboehm-2.0.so.1
	ln --symbolic libmonoboehm-2.0.so.1 \
		debian/mono-devel/usr/lib/libmonoboehm-2.0.so
	rm \
		debian/tmp/usr/bin/mono-boehm \
		debian/tmp/usr/lib/libmonoboehm-2.0.a \
		debian/tmp/usr/lib/libmonoboehm-2.0.so \
		debian/tmp/usr/lib/libmonoboehm-2.0.so.1 \
		debian/tmp/usr/lib/libmonoboehm-2.0.so.1.0.0
endif

execute_after_dh_install:
	cd mcs/jay && \
		$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	# don't want docs of bundled libgc
	rm -rf debian/tmp/usr/share/libgc-mono
	# Mono.Security.Win32.dll is only useful on windows, as it wrap the win api
	rm -rf $(CURDIR)/debian/tmp/usr/lib/mono/gac/Mono.Security.Win32/
	rm -f  $(CURDIR)/debian/tmp/usr/lib/mono/4.5/Mono.Security.Win32.dll*
	# we don't ship bundled nunit of mono
	rm -f  $(CURDIR)/debian/tmp/usr/lib/mono/4.5/nunit*
	rm -f  $(CURDIR)/debian/tmp/usr/lib/pkgconfig/mono-nunit.pc
	rm -rf $(CURDIR)/debian/tmp/usr/lib/mono/gac/nunit*/
	rm -f  $(CURDIR)/debian/tmp/usr/bin/nunit*
	# mono 2.10.1 bug, prj2make was removed but the start script not
	rm -f $(CURDIR)/debian/tmp/usr/bin/prj2make
	install -D -m 644 debian/cli.binfmt \
		debian/mono-runtime/usr/share/binfmts/cli
ifndef SKIP_LIBGC
	strip --strip-unneeded --remove-section=.comment --remove-section=.note \
		debian/tmp/usr/bin/pedump
endif
ifdef BTLS_ARCH
	install -D -m 644 debian/tmp/usr/lib/libmono-btls-shared.so \
		debian/mono-runtime/usr/lib/libmono-btls-shared.so
endif
	# Late GAC
	install -D --mode=644 \
		--target-directory=debian/mono-libraries/usr/lib/mono/gac/System.Data/4.0.0.0__b77a5c561934e089/ \
		debian/System.Data.dll.config
	install -D --mode=644 \
		--target-directory=debian/mono-libraries/usr/lib/mono/gac/System.Drawing/4.0.0.0__b03f5f7f11d50a3a/ \
		debian/System.Drawing.dll.config
	install -D --mode=644 \
		--target-directory=debian/mono-libraries/usr/lib/mono/gac/System.Windows.Forms/4.0.0.0__b77a5c561934e089/ \
		debian/System.Windows.Forms.dll.config
	install -D --mode=644 \
		--target-directory=debian/mono-libraries/usr/lib/mono/gac/Mono.Cairo/4.0.0.0__0738eb9f132ed756/ \
		debian/Mono.Cairo.dll.config
	install -D --mode=644 \
		--target-directory=debian/mono-libraries/usr/lib/mono/gac/Mono.WebBrowser/4.0.0.0__0738eb9f132ed756/ \
		debian/Mono.WebBrowser.dll.config

	debian/dh_clistrip -i --dbg-package=mono-libraries-debug
	debian/dh_makeclilibs -i -m 1.0 $(DH_INTERNAL_MONO_PARAM)
	debian/dh_makeclilibs -p mono-libraries $(DH_INTERNAL_MONO_PARAM)

	# debian/package.install can not be used for -dbgsym packages.
	install -D --mode=755 \
		--target-directory=debian/mono-runtime-dbgsym/usr/lib/debug/usr/bin/ \
		debian/tmp/usr/bin/mono-gdb.py
	rm debian/tmp/usr/bin/mono-gdb.py

execute_after_dh_fixperms:
	find debian/mono-*/usr/lib/mono \
		\( -name "*.dll" -o -name "*.mdb" \) \
		-exec chmod 644 {} +

override_dh_strip:
	dh_strip -a --dbgsym-migration='libmonoboehm-2.0-1-dbg (<< 6.14.1+ds-3), libmonosgen-2.0-1-dbg (<< 6.14.1+ds-3)'

override_dh_makeshlibs:
	dh_makeshlibs -a -V

# no elves here, skip zipping debug info
override_dh_dwz:

