#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk

SHELL = /bin/bash
export HOME=$(shell mktemp -d)

ifeq ($(DEB_HOST_ARCH), amd64)
        _RUNTIME_ARCH = x64
else ifeq ($(DEB_HOST_ARCH), arm64)
        _RUNTIME_ARCH = arm64
endif

_RUNTIME_ID = $(shell . /etc/os-release; [ ! -z $${VERSION_ID} ] && echo $${ID}.$${VERSION_ID} || echo $${ID})
_SDK_VERSION = $(shell echo $(DEB_VERSION_UPSTREAM) | cut -d'-' -f1)
_minor_sdk_version=$(shell echo $(_SDK_VERSION) | sed 's/.*\(.\)/\1/')
_RUNTIME_VERSION = 7.0.$(_minor_sdk_version)
_VERSION = $(shell echo $(_SDK_VERSION) | cut -d'.' -f1)

PREFIX = /usr
SYSCONFDIR = /etc
BINDIR = $(PREFIX)/bin
LIBDIR = $(PREFIX)/lib
DATADIR = $(PREFIX)/share
MANDIR = $(DATADIR)/man
DESTDIR = $(CURDIR)/debian/tmp
DOTNET_TOP = $(LIBDIR)/dotnet
dotnet_version = dotnet$(_VERSION)
dotnet_dirname = $(dotnet_version)-$(_SDK_VERSION)
DOTNETLIBDIR=$(DOTNET_TOP)
DOTNETETCDIR=$(SYSCONFDIR)/dotnet

INITIAL_BOOTSTRAPPING=1

%:
	dh $@ --with bash-completion

override_dh_auto_build:

ifeq ($(INITIAL_BOOTSTRAPPING),1)
	mkdir -p $(CURDIR)/packages/archive
	ln -s $(DOTNET_TOP)/source-built-artifacts/Private.SourceBuilt.Artifacts.*.tar.gz $(CURDIR)/packages/archive/
	cp -RLp $(DOTNET_TOP) $(CURDIR)/previously-built-dotnet
	find previously-built-dotnet

	VERBOSE=1 ./build.sh \
	--with-sdk $(CURDIR)/previously-built-dotnet \
	-- \
	/v:n \
	/p:SkipPortableRuntimeBuild=true \
	/p:LogVerbosity=n \
	/p:MinimalConsoleLogOutput=false \
   	/p:ContinueOnPrebuiltBaselineError=true
else
	VERBOSE=1 ./build.sh --clean-while-building \
	-- \
	/v:n \
	/p:SkipPortableRuntimeBuild=true \
	/p:LogVerbosity=n \
	/p:MinimalConsoleLogOutput=false \
	/p:ContinueOnPrebuiltBaselineError=true
endif

override_dh_auto_test:
	python3 ./debian/tests/build-time-tests/tests.py --verbose --purge-after

override_dh_install:

	install -dm 0755 $(DESTDIR)/$(DOTNETLIBDIR)
	install -dm 0755 $(DESTDIR)/$(DOTNETETCDIR)
	ls artifacts/$(_RUNTIME_ARCH)/Release
	tar xf artifacts/$(_RUNTIME_ARCH)/Release/dotnet-sdk-*-$(_RUNTIME_ID)-$(_RUNTIME_ARCH).tar.gz -C $(DESTDIR)/$(DOTNETLIBDIR)

	# See https://github.com/dotnet/source-build/issues/2579
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name 'testhost.x86' -delete
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name 'vstest.console' -delete


	# Fix executable permissions on files
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name 'apphost' -exec chmod +x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name 'lib*so' -exec chmod +x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name 'singlefilehost' -exec chmod +x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.a' -exec chmod -x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.dll' -exec chmod -x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.h' -exec chmod 0644 {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.json' -exec chmod -x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.pdb' -exec chmod -x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.props' -exec chmod -x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.pubxml' -exec chmod -x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.targets' -exec chmod -x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.txt' -exec chmod -x {} \;
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.xml' -exec chmod -x {} \;


	echo "$(DOTNETLIBDIR)" > install_location_$(_RUNTIME_ARCH)

	for f in debian/*.in; do \
	  f2=$$(echo $$f | sed 's/\.in$$//'); \
	  sed -e 's,@DOTNET_TOP@,$(DOTNET_TOP),g' \
	      -e 's,@basedir@,$(DOTNETLIBDIR),g' \
	      -e 's,@dotnet_dirname@,$(dotnet_dirname),g' \
	      -e 's,@major@,$(_VERSION),g' \
    	  -e 's,@dotnet_version@,$(dotnet_version),g' \
	      -e 's,@rid@,$(_RUNTIME_ID),g' \
	      -e 's,@arch@,$(_RUNTIME_ARCH),g' \
	      -e 's,@etcdir@,$(DOTNETETCDIR),g' \
	      -e 's,@multiarch@,$(DEB_HOST_MULTIARCH),g' \
	      -e 's,@hostarch@,$(DEB_HOST_ARCH),g' \
	      -e 's,@runtimearch@,$(_RUNTIME_ARCH),g' \
	    $$f > $$f2; \
	done

	cp $(CURDIR)/debian/install_location $(CURDIR)/debian/install_location_$(_RUNTIME_ARCH)
	dh_install

override_dh_dwz:
#	Leaving it empty due to "dwz: Unknown debugging section .debug_addr"
