#!/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
endif
_RUNTIME_ID = $(shell . /etc/os-release; [ ! -z $${VERSION_ID} ] && echo $${ID}.$${VERSION_ID} || echo $${ID})
_SDK_VERSION = $(DEB_VERSION_UPSTREAM)
_minor_sdk_version=$(shell echo $(_SDK_VERSION) | sed 's/.*\(..\)/\1/')
_RUNTIME_VERSION = 6.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)/$(dotnet_dirname)
DOTNETETCDIR=$(SYSCONFDIR)/dotnet


%:
	dh $@

override_dh_auto_build:

	mkdir -p $(CURDIR)/packages/archive
	ln -s $(DOTNET_TOP)/$(dotnet_version)/source-built-artifacts/Private.SourceBuilt.Artifacts.*.tar.gz $(CURDIR)/packages/archive/
	cp -RLp $(DOTNET_TOP)/$(dotnet_version) $(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 \

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-$(_SDK_VERSION)-$(_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 {} \;


	install -dm 0755 $(DESTDIR)/$(DOTNETLIBDIR)/man/man1
	find -iname 'dotnet*.1' -type f -exec cp {} $(DESTDIR)/$(DOTNETLIBDIR)/man/man1/ \;
	gzip -9v -n $(DESTDIR)/$(DOTNETLIBDIR)/man/man1/*

	for f in $(DESTDIR)/$(DOTNETLIBDIR)/man/man1/*.1.gz; do \
       	f=$$(basename $$f); \
		echo " /usr/share/man/man1/$$f $$f $(DOTNETLIBDIR)/man/man1/$$f" >> debian/tmp/dependants;  \
	done

	echo "$(DOTNETLIBDIR)" > install_location_$(_RUNTIME_ARCH)
	echo " /etc/dotnet/install_location install_location $(DOTNETLIBDIR)/install_location" >> debian/tmp/dependants
	echo " /etc/dotnet/install_location_$(_RUNTIME_ARCH) install_location_$(_RUNTIME_ARCH) $(DOTNETLIBDIR)/install_location_$(_RUNTIME_ARCH)" >> debian/tmp/dependants


	cat debian/tmp/dependants >> debian/dotnet-host.alternatives.in
	echo "Priority: 6010$(_minor_sdk_version)" >> debian/dotnet-host.alternatives.in


	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)
	rm debian/tmp/dependants
	dh_install

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