#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto

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), s390x)
	_RUNTIME_ARCH = s390x
else ifeq ($(DEB_HOST_ARCH), arm64)
# 	-fstack-clash-protection is not implemented in arm64 clang,
# 	therefore stripping this flag away to avoid the error:
# 	clang: error: argument unused during compilation.
# 	See https://github.com/llvm/llvm-project/issues/40148
	export DEB_CFLAGS_MAINT_STRIP = -fstack-clash-protection
	export DEB_CXXFLAGS_MAINT_STRIP = -fstack-clash-protection

	_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)
_RUNTIME_VERSION = $(shell echo $(DEB_VERSION_UPSTREAM) | cut -d'-' -f2 | cut -d'~' -f1)
_VERSION = $(shell echo $(_SDK_VERSION) | cut -d'.' -f1)
_VERSION_SUFFIX = $(shell echo $(DEB_VERSION_UPSTREAM) | cut -d'-' -f2 | grep -Eo '~(.*)')
_DEB_REV_SUFFIX = $(shell echo $(DEB_VERSION) | cut -d'-' -f3)

_FULL_RUNTIME_VERSION = $(_RUNTIME_VERSION)$(_VERSION_SUFFIX)-$(_DEB_REV_SUFFIX)
_FULL_SDK_VERSION = $(_SDK_VERSION)$(_VERSION_SUFFIX)-$(_DEB_REV_SUFFIX)

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

RM_GIT_REPO = $(shell cat release.info | grep RM_GIT_REPO | cut -d= -f2)
RM_GIT_COMMIT = $(shell cat release.info | grep RM_GIT_COMMIT | cut -d= -f2)

INITIAL_BOOTSTRAPPING = no

%:
	dh $@ --with bash-completion

override_dh_auto_build:

ifeq ($(RM_GIT_REPO),)
	echo "RM_GIT_REPO is empty, please make sure there is a valid release.info file in the source tarball."
	exit 1
endif

ifeq ($(RM_GIT_COMMIT),)
	echo "RM_GIT_COMMIT is empty, please make sure there is a valid release.info file in the source tarball."
	exit 1
endif

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

	VERBOSE=1 ./build.sh \
	--with-sdk $(CURDIR)/previously-built-dotnet \
	--source-repository $(RM_GIT_REPO) \
	--source-version $(RM_GIT_COMMIT) \
	-- \
	/v:n \
	/p:SkipPortableRuntimeBuild=true \
	/p:LogVerbosity=n \
	/p:MinimalConsoleLogOutput=false \
	/p:ContinueOnPrebuiltBaselineError=true
else
	VERBOSE=1 ./build.sh --clean-while-building \
	--source-repository $(RM_GIT_REPO) \
	--source-version $(RM_GIT_COMMIT) \
	-- \
	/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_gencontrol:
	dh_gencontrol
	dh_gencontrol -paspnetcore-runtime-8.0 -- -v$(_FULL_RUNTIME_VERSION)
	dh_gencontrol -paspnetcore-targeting-pack-8.0 -- -v$(_FULL_RUNTIME_VERSION)
	dh_gencontrol -pdotnet-apphost-pack-8.0 -- -v$(_FULL_RUNTIME_VERSION)
	dh_gencontrol -pdotnet-host-8.0 -- -v$(_FULL_RUNTIME_VERSION)
	dh_gencontrol -pdotnet-hostfxr-8.0 -- -v$(_FULL_RUNTIME_VERSION)
	dh_gencontrol -pdotnet-runtime-8.0 -- -v$(_FULL_RUNTIME_VERSION)
	dh_gencontrol -pdotnet-sdk-8.0 -- -v$(_FULL_SDK_VERSION)
	dh_gencontrol -pdotnet-sdk-8.0-source-built-artifacts -- -v$(_FULL_SDK_VERSION)
	dh_gencontrol -pdotnet-targeting-pack-8.0 -- -v$(_FULL_RUNTIME_VERSION)
	dh_gencontrol -pdotnet-templates-8.0 -- -v$(_FULL_SDK_VERSION)
	dh_gencontrol -pnetstandard-targeting-pack-2.1-8.0 -- -v$(_FULL_SDK_VERSION)
	dh_gencontrol -paspnetcore-runtime-dbg-8.0 -- -v$(_FULL_RUNTIME_VERSION)
	dh_gencontrol -pdotnet-runtime-dbg-8.0 -- -v$(_FULL_RUNTIME_VERSION)
	dh_gencontrol -pdotnet-sdk-dbg-8.0 -- -v$(_FULL_SDK_VERSION)

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)

	# Clean up unwanted PDB files in sdk tar.gz
	find $(DESTDIR)/$(DOTNETLIBDIR) -type f -name '*.pdb' -exec rm {} \;

	# Extract debug symbols to their own directory for easy handling
	mkdir -p artifacts/debug-symbols
	tar xf artifacts/$(_RUNTIME_ARCH)/Release/dotnet-symbols-sdk-*-$(_RUNTIME_ID)-$(_RUNTIME_ARCH).tar.gz -C artifacts/debug-symbols

	# 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"
