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

UPVERSION = $(subst +ds,,$(DEB_VERSION_UPSTREAM))
NEXT_UPVERSION = $(shell perl -e '$$_=pop; s/(\d+)$$/$$1+1/e; print' $(UPVERSION))

RUN_MONO = MONO_PATH=debian/tmp/usr/lib/mono/4.5 runtime/mono-wrapper
KILL_MONO = pgrep mono | xargs kill -9
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)
	MONO_BOEHM_TEST = true
	CONF_FLAGS += --disable-boehm
	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


%:
	dh $@ --with=python3


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_clideps \
	         debian/dh_clistrip \
	         debian/dh_clifixperms \
	         debian/dh_monoaot
	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; $(KILL_MONO)
	-cd mono/tests && MONO_ENV_OPTIONS="$(MONO_ENV_OPTIONS) --gc=boehm" timeout -k 15m 1h make check; $(KILL_MONO)
	-cd mono/tests && MONO_ENV_OPTIONS="$(MONO_ENV_OPTIONS) --gc=sgen" timeout -k 15m 1h make check; $(KILL_MONO)
	-$(KILL_MONO)
	# 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

override_dh_auto_install:
	dh_auto_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-common/usr/lib/libmono-btls-shared.so
endif

execute_after_dh_install:
	# Late GAC
	install -D --mode=755 debian/mono.runtime-script debian/mono-gac/usr/share/cli-common/runtimes.d/mono
	install -D --mode=644 \
		--target-directory=debian/libmono-corlib4.5-cil/usr/lib/mono/gac/System.Data/4.0.0.0__b77a5c561934e089/ \
		debian/System.Data.dll.config
	install -D --mode=644 \
		--target-directory=debian/libmono-corlib4.5-cil/usr/lib/mono/gac/System.Drawing/4.0.0.0__b03f5f7f11d50a3a/ \
		debian/System.Drawing.dll.config
	install -D --mode=644 \
		--target-directory=debian/libmono-corlib4.5-cil/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/libmono-corlib4.5-cil/usr/lib/mono/gac/Mono.Cairo/4.0.0.0__0738eb9f132ed756/ \
		debian/Mono.Cairo.dll.config
	install -D --mode=644 \
		--target-directory=debian/libmono-corlib4.5-cil/usr/lib/mono/gac/Mono.WebBrowser/4.0.0.0__0738eb9f132ed756/ \
		debian/Mono.WebBrowser.dll.config

	debian/dh_clistrip -i --dbg-package=mono-dbg
	debian/dh_clifixperms -i
	DH_AUTOSCRIPTDIR=$(CURDIR)/debian debian/dh_monoaot -i
	debian/dh_makeclilibs -i -m 1.0 $(DH_INTERNAL_MONO_PARAM)
	debian/dh_makeclilibs -p libmono-corlib4.5-cil $(DH_INTERNAL_MONO_PARAM)
	debian/dh_clideps -i -X/usr/lib/mono/2.0-api -X/usr/lib/mono/3.5-api -X/usr/lib/mono/4.0-api -X/usr/lib/mono/4.5.1-api \
		-X/usr/lib/mono/4.5.2-api -X/usr/lib/mono/4.5-api -X/usr/lib/mono/4.6.1-api -X/usr/lib/mono/4.6.2-api \
		-X/usr/lib/mono/4.6-api -X/usr/lib/mono/4.7.1-api -X/usr/lib/mono/4.7.2-api -X/usr/lib/mono/4.7-api \
		-X/usr/lib/mono/4.8-api -l debian/tmp $(DH_INTERNAL_MONO_PARAM)
	#DH_VERBOSE=1 debian/dh_clideps -i -l debian/tmp $(DH_INTERNAL_MONO_PARAM)
	# mono-1.0/2.0-gac needs special runtime dep, to prevent circular dep (mono-runtime <-> mono-1.0/2.0-gac)
	debian/dh_clideps -p mono-4.0-gac -r $(DH_INTERNAL_MONO_PARAM)

override_dh_strip:
	dh_strip -pmono-runtime-common -pmono-runtime-sgen -pmono-runtime-boehm --dbg-package=mono-runtime-dbg
	dh_strip -plibmonoboehm-2.0-1 --dbg-package=libmonoboehm-2.0-1-dbg
	dh_strip -plibmonosgen-2.0-1 --dbg-package=libmonosgen-2.0-1-dbg
	dh_strip -a -Xbin/mono-sgen -Xbin/mono-boehm

override_dh_makeshlibs:
	dh_makeshlibs -a -V
	dh_makeshlibs -plibmonosgen-2.0-1 -V 'libmonosgen-2.0-1 (>= $(UPVERSION))' -- -c4
ifndef SKIP_LIBGC
	dh_makeshlibs -plibmonoboehm-2.0-1 -V 'libmonoboehm-2.0-1 (>= $(UPVERSION))'
endif

# no elves here, skip zipping debug info
override_dh_dwz:

override_dh_gencontrol:
	dh_gencontrol -- -Vmono:upversion=$(UPVERSION) -Vmono:next-upversion=$(NEXT_UPVERSION)
