#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

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

VENDOR			?= $(call dpkg_late_eval,VENDOR,dpkg-vendor --derives-from Ubuntu && echo Ubuntu || echo Debian)


include debian/rules.defs
CUDA_ARCH_LIST			?= amd64
CUDA_ARCH_LIST_NO_DRIVER	?=
CUDA_ARCH_LIST_NO_TESLA_DRIVER	?=
CUDA_VERSION_TOOLKIT	?= $(DEB_VERSION_UPSTREAM)
CUDA_VERSION_MAJOR	?= $(word 1,$(subst ., ,$(CUDA_VERSION_TOOLKIT)))
CUDA_VERSION_MINOR	?= $(word 2,$(subst ., ,$(CUDA_VERSION_TOOLKIT)))
version_driver		 = $(CUDA_VERSION_DRIVER_MINIMAL.$(DEB_HOST_ARCH))
watch_url		 = $(CUDA_WATCH_URL)
PACKAGE_LIST		:= $(shell dh_listpackages)
THRUST_VERSION		?= $(or $(strip $(shell perl -ne 'if (/define\s+THRUST_VERSION\s+(\d*)/) { printf "%d.%d.%d\n", $$1 / 100000, $$1 / 100 % 1000, $$1 % 100; }' nvidia-cuda/include/thrust/version.h)),$(error empty THRUST_VERSION))
thrust_pkg		?= $(filter libthrust-dev,$(PACKAGE_LIST))
nsight_compute_pkg	?= $(filter nsight-compute,$(PACKAGE_LIST))
nsight_systems_pkg	?= $(filter nsight-systems,$(PACKAGE_LIST))
CUDA_JAVA_HOME          ?= /usr/lib/jvm/java-8-openjdk-$(DEB_HOST_ARCH)

package_libcuda1 \
	= libcuda1 (>= $${nvidia:MinVersion}) [$${cuda:arch:has-driver}] \
	| libnvidia-tesla-cuda1 (>= $${nvidia:MinVersion}) [$${cuda:arch:has-tesla-driver}] \
	| libcuda.so.1 (>= $${nvidia:MinVersion}) \
	| libcuda-$${cudart:SoVersion}-1
#ifeq ($(VENDOR),Ubuntu)
package_libcuda1	+= | no-libcuda1 [$${cuda:arch:has-no-driver}]
#endif

# system libdir
libdir			 = usr/lib/$(DEB_HOST_MULTIARCH)

TEMPLATES	:= $(wildcard debian/*.in debian/patches/*.in)
SOVERTEMPLATES	:= $(wildcard debian/*SOVER*)
RTSOVERTEMPLATES:= $(wildcard debian/*SORTVER*)
AUTOGEN		+= $(patsubst %.in,%,$(TEMPLATES))
AUTOGEN		+= $(subst SOVER,$(CUDA_SOVERSION),$(sort $(patsubst %.in,%,$(SOVERTEMPLATES))))
AUTOGEN		+= $(subst SORTVER,$(CUDART_SOVERSION),$(sort $(patsubst %.in,%,$(RTSOVERTEMPLATES))))
AUTOGEN		+= debian/g++
AUTOKEEP	 = debian/watch
AUTOCLEAN	 = $(filter-out $(AUTOKEEP),$(AUTOGEN))
AUTOCLEAN	+= debian/shlibs.local


.PHONY: autogen prepare
autogen: $(AUTOGEN)
prepare: autogen unpack-stamp compare-copyright-license


# deep copy, files will get irreversibly modified via hardlinks in the build target
define nvidia-cuda-unpack-repack
	dh_testdir
	$(RM) -r $@ $@.tmp
	cp -a $1 $@
endef

define nvidia-cuda-unpack-run
	dh_testdir
	$(RM) -r $@ $@.tmp
	sh $(CUDA_FILENAME.$1) --noexec --keep --target $@.tmp
	mv $@.tmp $@
endef

define nvidia-cuda-unpack-deb
	dh_testdir
	$(RM) -r $@ $@.tmp
	mkdir $@.tmp
	@set -e -x ; for deb in $1/cuda*.deb ; do dpkg -x $$deb $@.tmp/ubuntutree ; done
	mv $@.tmp/ubuntutree/usr/local/cuda*/* $@.tmp/
	$(RM) -r $@.tmp/ubuntutree/usr/lib/pkgconfig
	$(RM) -r $@.tmp/ubuntutree/usr/share/doc
	$(RM) -r $@.tmp/ubuntutree/usr/share/lintian
	$(RM) -v $@.tmp/ubuntutree/etc/ld.so.conf.d/cuda-$(subst .,-,$(CUDART_SOVERSION)).conf
	$(RM) -v $@.tmp/bin/computeprof
	$(RM) -v $@.tmp/LICENSE
	$(RM) -v $@.tmp/README
	mv $@.tmp/doc/EULA.txt $@.tmp/
	$(RM) $@.tmp/include $@.tmp/lib64
	mv $@.tmp/targets/ppc64le-linux/include $@.tmp/include
	mv $@.tmp/targets/ppc64le-linux/lib $@.tmp/lib64
	mv $@.tmp $@
endef

nvidia-cuda-tree-%:
	$(call nvidia-cuda-unpack-$(CUDA_TAR_TYPE.$*),$*)

unpack-stamp: $(filter debian/patches%,$(AUTOGEN))
unpack-stamp: nvidia-cuda-tree-$(DEB_HOST_ARCH)
	$(RM) nvidia-cuda
	ln -sf nvidia-cuda-tree-$(DEB_HOST_ARCH) nvidia-cuda
	QUILT_PATCHES=debian/patches QUILT_SERIES=series-postunpack quilt --quiltrc /dev/null push -a || test $$? = 2
	touch $@

# Reformat the EULA to the format needed for debian/copyright.
nvidia-cuda/EULA.txt: unpack-stamp
EULA.fmt: nvidia-cuda/EULA.txt
	cat $< \
		| fromdos | fromdos | expand \
		| iconv -f UTF-8 -t latin1//TRANSLIT \
		| fold -s -w 80 \
		| sed -e 's/ *$$//;s/^$$/./;s/^/ /;' \
		> $@

# Compare the license in debian/copyright with the EULA shipped in the archive.
compare-copyright-license: EULA.fmt
	sed -e '1,/^License: other-NVIDIA-CUDA-TOOLKIT/d; /^$$/,$$d; /^ .$$/d' debian/copyright > copyright.tmp
	sed -e '/^ .$$/d' EULA.fmt > EULA.tmp
	diff -w copyright.tmp EULA.tmp
	rm -f copyright.tmp EULA.tmp


.PHONY: binary binary-arch binary-indep build build-arch build-indep clean install
binary-arch build build-arch build-indep clean install:
	dh $@

binary binary-indep:
	# the documentation packages must be built on amd64 (otherwise some parts are missing)
	test "$(DEB_HOST_ARCH)" = "amd64"
	dh $@

build-stamp: unpack-stamp
	$(RM) -r build
	cp -al nvidia-cuda-tree-$(DEB_HOST_ARCH)/ build
	test ! -d build/nsight-compute-$(NSIGHT_COMPUTE_VERSION) || mv build/nsight-compute-$(NSIGHT_COMPUTE_VERSION) build/nsight-compute
	test ! -d build/nsight-systems-$(NSIGHT_SYSTEMS_VERSION) || mv build/nsight-systems-$(NSIGHT_SYSTEMS_VERSION) build/nsight-systems
	# fix permissions
	chmod -x build/libnvvp/*.xpm
	chmod -x build/libnsight/*.xpm
	chmod -x build/nvvm/libnvvm-samples/build.bat
	# uuid(3) is already provided by uuid-dev
	mv -v build/doc/man/man3/uuid.3 build/doc/man/man3/CUDA_uuid.3
	# remove tracking scripts
	find build \( -name google-analytics-tracker.js -o -name google-analytics-write.js -o -name tynt.js \) -exec rm -v {} +
	# remove tracking images
	@set -e -x ; for dir in \
		build/doc/html \
		build/nsight-compute/docs \
		build/extras/CUPTI/doc \
		build/extras/Sanitizer/docs \
	; do test ! -d $$dir || find $$dir \
	    -name '*.html' -exec sed -r -i \
		-e '\,http://omniture.nvidia.com/b/ss/nvidiacudadocs/1/H.17--NS/0, { s,(<noscript>),<!-- \1,; s,img src,img DISABLED,; s,(</noscript>),\1 -->, }' \
		-e 's,(<script type="text/javascript" )src(="http://w.sharethis.com/button/buttons.js"[^>]*></script>),<!-- \1DISABLED\2 -->,' \
		-e 's,(<script type="text/javascript" charset="utf-8" )src(="//assets.adobedtm.com/b92787824f2e0e9b68dc2e993f9bd995339fe417/satelliteLib-7ba51e58dc61bcb0e9311aadd02a0108ab24cc6c.js"[^>]*></script>),<!-- \1DISABLED\2 -->,' \
		{} + ; \
	done
	find \
		build/libnsight \
		build/libnvvp \
	    -name 'license.html' -exec sed -r -i \
		-e 's,(<script type="text/javascript" )src(="http://w.sharethis.com/button/buttons.js"[^>]*></script>),<!-- \1DISABLED\2 -->,' \
		{} +
	# replace embedded javascript libraries
	find build -name html5shiv-printshiv.min.js -exec ln -sfv /usr/share/javascript/html5shiv/html5shiv-printshiv.min.js {} \;
	find build -name jquery.js -exec ln -sfv /usr/share/javascript/jquery/jquery.js {} \;
	find build -name jquery-3.1.0.js -exec ln -sfv /usr/share/javascript/jquery/jquery.js {} \;
	find build -name jquery.min.js -exec ln -sfv /usr/share/javascript/jquery/jquery.min.js {} \;
	find build -name underscore.js -exec ln -sfv /usr/share/javascript/underscore/underscore.js {} \;
	# reduce 'dh_missing --list-missing' noise
	@set -e -x ; for dir in build/include build/lib64 ; do if [ -h "$$dir" ] ; then target=$$(readlink -f $$dir) ; $(RM) $$dir ; mv -v $$target $$dir ; fi ; done
ifeq (amd64,$(DEB_HOST_ARCH))
	$(RM) -rv build/nsight-compute/target/$(NSIGHT_COMPUTE_DESKTOP_DIR.i386)/
	$(RM) -rv build/nsight-compute/target/$(NSIGHT_COMPUTE_DESKTOP_DIR.ppc64el)/
endif
ifeq (,$(thrust_pkg))
	# that's packaged separately
	$(RM) -r build/include/thrust
endif
	# remove empty directories
	find build -depth -type d -exec sh -c 'd="{}"; rmdir --ignore-fail-on-non-empty "$$d" ; test -d "$$d" || echo "removed $$d"' \;
	# split arch and indep eclipse plugins
	mkdir build/libnsight/plugins-arch
	mv build/libnsight/plugins/org.eclipse.equinox.launcher.gtk.linux.* build/libnsight/plugins-arch/
	mkdir build/libnvvp/plugins-arch
	mv build/libnvvp/plugins/org.eclipse.equinox.launcher.gtk.linux.* build/libnvvp/plugins-arch/
ifneq (,$(nsight_compute_pkg))
	# clean up nsight-compute
ifeq (amd64,$(DEB_HOST_ARCH))
	chrpath -d build/nsight-compute/host/$(NSIGHT_COMPUTE_DESKTOP_DIR.$(DEB_HOST_ARCH))/libicu*.so.56
endif
ifeq (ppc64el,$(DEB_HOST_ARCH))
	chrpath -d build/nsight-compute/target/*/libTreeLauncherTargetInjection.so
endif
endif
ifneq (,$(nsight_systems_pkg))
	# clean up nsight-systems
	chrpath -d build/nsight-systems/Host-*/libicu*.so.56
endif
	touch $@

override_dh_auto_configure: $(AUTOGEN) unpack-stamp

override_dh_auto_build: build-stamp

override_dh_auto_install:
	mkdir debian/tmp
	cp -al build/ debian/tmp/usr

override_dh_installdocs-indep:
	dh_installdocs -pnvidia-cuda-doc --doc-main-package=nvidia-cuda-toolkit
	dh_installdocs --remaining-packages

override_dh_installman:
	dh_installman --language=C

override_dh_missing:
	dh_missing --fail-missing

override_dh_makeshlibs:
	dh_makeshlibs -X/stubs/

override_dh_shlibdeps:
	$(RM) -r libcuda
	mkdir libcuda
	ln -s ../nvidia-cuda/lib64/stubs/libcuda.so libcuda/libcuda.so.1
	echo 'libcuda 1 XXXlibcuda1XXX' > debian/shlibs.local
ifneq (,$(nsight_compute_pkg))
	dh_shlibdeps -p$(nsight_compute_pkg) -- -l/usr/lib/nsight-compute/host/$(NSIGHT_COMPUTE_DESKTOP_DIR.$(DEB_HOST_ARCH))
endif
ifneq (,$(nsight_systems_pkg))
	dh_shlibdeps -p$(nsight_systems_pkg) -- -l/usr/lib/nsight-systems/Host-x86_64/
endif
	dh_shlibdeps --remaining-packages -X/stubs/ -- -llibcuda
	$(RM) -r libcuda debian/shlibs.local
	sed -i -r 's/XXXlibcuda1XXX/$${package:libcuda1}/g' debian/*.substvars

# running dh_strip is not permitted by the NVIDIA license
override_dh_dwz:
override_dh_strip:
override_dh_strip_nondeterminism:

override_dh_compress:
	dh_compress -Xusr/share/doc/nvidia-cuda-toolkit/examples -Xusr/share/doc/libcupti-dev/examples

override_dh_gencontrol-indep:
	hardlink -v -t debian/nvidia-cuda-doc/usr/share/doc/nvidia-cuda-toolkit
	hardlink -v -t debian/libcupti-doc/usr/share/doc/libcupti-dev
ifneq (,$(thrust_pkg))
	dh_gencontrol -p$(thrust_pkg) -- -v$(THRUST_VERSION)~$(DEB_VERSION)
endif
	dh_gencontrol --remaining-packages

override_dh_gencontrol-arch:
ifneq (,$(nsight_compute_pkg))
	hardlink -v -t debian/$(nsight_compute_pkg)/usr/share/doc/$(nsight_compute_pkg)
endif
	dh_gencontrol --remaining-packages -- \
		-V'package:libcuda1=$(package_libcuda1)' \
		-V'cuda:SoVersion=$(CUDA_SOVERSION)' \
		-V'cudart:SoVersion=$(CUDART_SOVERSION)' \
		-V'nvidia:MinVersion=$(version_driver)' \
		-V'cuda:arch:has-no-driver=$(if $(and $(filter $(DEB_HOST_ARCH),$(CUDA_ARCH_LIST_NO_DRIVER)),$(filter $(DEB_HOST_ARCH),$(CUDA_ARCH_LIST_NO_TESLA_DRIVER))),,!)$(DEB_HOST_ARCH)' \
		-V'cuda:arch:has-driver=$(if $(filter $(DEB_HOST_ARCH),$(CUDA_ARCH_LIST_NO_DRIVER)),!,)$(DEB_HOST_ARCH)' \
		-V'cuda:arch:has-tesla-driver=$(if $(filter $(DEB_HOST_ARCH),$(CUDA_ARCH_LIST_NO_TESLA_DRIVER)),!,)$(DEB_HOST_ARCH)' \
		-V'cuda:arch:has-nsight-compute=$(if $(nsight_compute_pkg),,!)$(DEB_HOST_ARCH)' \
		-V'cuda:arch:has-nsight-systems=$(if $(nsight_systems_pkg),,!)$(DEB_HOST_ARCH)' \

override_dh_auto_clean:
	$(RM) -r .pc
	$(RM) -r nvidia-cuda-tree-amd64*
	$(RM) -r nvidia-cuda-tree-ppc64el*
	$(RM) nvidia-cuda*
	$(RM) -r build
	$(RM) -r libcuda
	$(RM) EULA.fmt EULA.tmp copyright.tmp

override_dh_clean:
	dh_clean
	$(RM) $(AUTOCLEAN)
	$(MAKE) -f debian/rules $(AUTOKEEP)


# Generating control files
%:: %.in debian/rules debian/rules.defs
	perl -p \
	-e 's{#VERSION_TOOLKIT#}{$(CUDA_VERSION_TOOLKIT)}g;' \
	-e 's{#SOVERSION#}{$(CUDA_SOVERSION)}g;' \
	-e 's{#RTSOVERSION#}{$(CUDART_SOVERSION)}g;' \
	-e 's{#CUDA_DOWNLOAD_URL#}{$(watch_url)}g;' \
	-e 's{#LIBDIR#}{$(libdir)}g;' \
	-e 's{#MAN_DATE#}{$(CUDA_MAN_DATE.$(DEB_HOST_ARCH))}g;' \
	-e 's{#!ppc64el#}{$(if $(filter ppc64el,$(DEB_HOST_ARCH)),#)}g;' \
	-e 's{#NSIGHT_COMPUTE_DESKTOP_DIR#}{$(NSIGHT_COMPUTE_DESKTOP_DIR.$(DEB_HOST_ARCH))}g;' \
	-e 's{#CUDA_JAVA_HOME#}{$(CUDA_JAVA_HOME)}g;' \
	< $< > $@

define sover-template
lib%$(CUDA_SOVERSION).$(1):: lib%SOVER.$(1)
	cp $$< $$@

lib%$(CUDART_SOVERSION).$(1):: lib%SORTVER.$(1)
	cp $$< $$@

endef
$(foreach suffix,install links lintian-overrides symbols,$(eval $(call sover-template,$(suffix))))

debian/g++: debian/gcc
	sed '/prog=/s/gcc-/g++-/g' < $< > $@
	chmod 0755 $@

# only invoked manually
check-whitespace:
	grep -r --exclude-dir=.svn --exclude-dir=.git --exclude '*.patch' -E '\s$$' debian || true

############################################################################

UPSTREAM_NAME		 = nvidia-cuda-toolkit
UPSTREAM_VERSION	 = $(CUDA_VERSION_TOOLKIT)
REPACK_LIST		 = $(CUDA_ARCH_LIST)
TARBALL_LIST		 = $(foreach a,empty $(REPACK_LIST),$(call get_tarball,$a))

CACHE			?= ../.cache
COMPRESS_EXT		?= gz
COMPRESS.gz		?= gzip -n9
COMPRESS.xz		?= xz -9
ORIGDIR			?= $(UPSTREAM_NAME)-$(UPSTREAM_VERSION).orig
TARBALL_BASE		?= $(UPSTREAM_NAME)_$(UPSTREAM_VERSION).orig
TARBALL_SUFFIX		?= .tar.$(COMPRESS_EXT)
TARBALL			?= $(TARBALL_BASE)$(TARBALL_SUFFIX)
ORIGDIR.empty		?= $(ORIGDIR)
TARBALL.empty		?= $(TARBALL)

# $1 = arch
get_origdir		 = $(or $(ORIGDIR.$(strip $1)),$(ORIGDIR)-$(strip $1))
get_tarball		 = $(or $(TARBALL.$(strip $1)),$(TARBALL_BASE)-$(strip $1)$(TARBALL_SUFFIX))
get_dl_filename		 = $(CUDA_DL_FILENAME.$(strip $1).$(CUDA_DL_TYPE.$(strip $1)))

get-orig-source-prepare:
	$(RM) -r get-orig-source
	mkdir get-orig-source

get-orig-source-prepare/%: get-orig-source-prepare
	$(RM) $(call get_tarball,$*)
	mkdir get-orig-source/$(call get_origdir,$*)

get-orig-source-download/%: get-orig-source-prepare
	ln -v $(CACHE)/$(call get_dl_filename,$*) get-orig-source/ 2>/dev/null || \
		wget $(CUDA_DL_URL_HOST)/$(CUDA_DL_URL_PATH.$*)/$(CUDA_URL_FILENAME.$*) -O get-orig-source/$(call get_dl_filename,$*)
	if [ -d $(CACHE) ] && [ ! -f $(CACHE)/$(call get_dl_filename,$*) ]; then \
		ln -v get-orig-source/$(call get_dl_filename,$*) $(CACHE) ; fi

# unpack the all-in-one .run cuda installer containing toolkit, driver and samples
get-orig-source-run-unpack/%: get-orig-source-prepare/% get-orig-source-download/%
	@set -e -x ; \
	    cd get-orig-source ; \
	    sh $(call get_dl_filename,$*) --noexec --keep --target tmp.$* ; \
	    find tmp.$* -ls ; \
	    if [ -d tmp.$*/builds/cuda-toolkit ]; then \
		mv tmp.$*/builds/cuda-toolkit/* $(call get_origdir,$*)/ ; \
	    else \
	        if [ -d tmp.$*/run_files ]; then \
	            mv -f tmp.$*/run_files/cuda-linux*.run tmp.$*/run_files/EULA.txt $(call get_origdir,$*)/ ; \
	        else \
	            mv "$(call get_dl_filename,$*)" $(call get_origdir,$*)/ ; \
	        fi ; \
	        chmod 0755 $(call get_origdir,$*)/*.run ; \
	    fi ; \
	    $(RM) -r tmp.$*

# unpack the all-in-one .deb local-repo installer
get-orig-source-deb-unpack/%: get-orig-source-prepare/% get-orig-source-download/%
	@set -e -x ; \
	    cd get-orig-source ; \
	    dpkg -X $(call get_dl_filename,$*) tmp.$* ; \
	    mv tmp.$*/var/cuda-repo-$(CUDA_VERSION_MAJOR)-$(CUDA_VERSION_MINOR)-*/*.deb $(call get_origdir,$*)/ ; \
	    : "# Remove undistributable files:" ; \
	    $(RM) -v $(call get_origdir,$*)/cuda-samples-$(CUDA_VERSION_MAJOR)-$(CUDA_VERSION_MINOR)_*.deb ; \
	    : "# Remove driver:" ; \
	    $(RM) -v $(call get_origdir,$*)/*_$(CUDA_VERSION_DRIVER_BUNDLED.$*)-*_*.deb ; \
	    chmod 0644 $(call get_origdir,$*)/*.deb ; \
	    $(RM) -r tmp.$*

get-orig-source-unpack/amd64: get-orig-source-$(CUDA_DL_TYPE.amd64)-unpack/amd64
get-orig-source-unpack/ppc64el: get-orig-source-$(CUDA_DL_TYPE.ppc64el)-unpack/ppc64el

get-orig-source-files/%: get-orig-source-unpack/%
	@set -e -x ; \
	    cd get-orig-source ; \
	    touch $(call get_origdir,$*).stamp -r $$(find $(call get_origdir,$*) -type f | xargs -r ls -tr | tail -n 1)

get-orig-source-pack/%: get-orig-source-files/%
	@set -e -x ; \
	    cd get-orig-source ; \
	    tar cfv - \
	        --clamp-mtime --mtime="./$(call get_origdir,$*).stamp" \
		--sort=name \
		--owner=root --group=src \
		$(call get_origdir,$*) \
		| $(COMPRESS.$(COMPRESS_EXT)) > $(call get_tarball,$*); \
	    touch $(call get_tarball,$*) -r $(call get_origdir,$*).stamp
	mv get-orig-source/$(call get_tarball,$*) .

get-orig-source-files/empty: get-orig-source-prepare/empty $(foreach a,$(REPACK_LIST),get-orig-source-pack/$a)
	touch get-orig-source/$(call get_origdir,empty).stamp -r $$(ls -tr $(foreach a,$(CUDA_ARCH_LIST),$(call get_tarball,$a)) | tail -n 1)

.PHONY: get-orig-source
get-orig-source: $(foreach a,empty $(REPACK_LIST),get-orig-source-pack/$a)
	$(RM) -r get-orig-source

############################################################################

# The tarballs are kept in a separate git repository and use the git-lfs
# storage backend. To keep the local disk usage low, this repository is
# checked out sparsely, only tarballs for versions that have been built
# locally will be available locally.

GIT			?= git
TARBALL_REPOSITORY	 = https://salsa.debian.org/nvidia-team/tarballs-$(UPSTREAM_NAME).git
TARBALL_TREE		 = ../tarballs-$(UPSTREAM_NAME)

define sparse-checkout-current
	grep '^$(UPSTREAM_VERSION)/' $(TARBALL_TREE)/.git/info/sparse-checkout || \
		echo '$(UPSTREAM_VERSION)/*' >> $(TARBALL_TREE)/.git/info/sparse-checkout

endef

define test-file-exists
	test -f $1

endef

$(TARBALL_TREE):
	$(RM) -r $@.tmp
	$(GIT) clone --no-checkout $(TARBALL_REPOSITORY) $@.tmp
	cd $@.tmp && $(GIT) lfs install --local
	cd $@.tmp && $(GIT) config --local core.sparsecheckout true
	echo .gitattributes >> $@.tmp/.git/info/sparse-checkout
	cd $@.tmp && $(GIT) checkout
	mv $@.tmp $@

$(TARBALL_TREE)/$(UPSTREAM_VERSION): | $(TARBALL_TREE)
	$(sparse-checkout-current)
	cd $(TARBALL_TREE) && $(GIT) pull --ff-only && $(GIT) checkout

tarball-tree: $(TARBALL_TREE)

# only invoked manually
writable-tarball-tree: tarball-tree
	cd $(TARBALL_TREE) && $(GIT) remote set-url origin $(patsubst https://salsa.debian.org/%,git@salsa.debian.org:%,$(TARBALL_REPOSITORY))

# invoked from gbp buildpackage preexport hook
check-current-tarballs: $(TARBALL_TREE)/$(UPSTREAM_VERSION)
	$(foreach t,$(TARBALL_LIST),$(call test-file-exists,$(TARBALL_TREE)/$(UPSTREAM_VERSION)/$t))

# only invoked manually
commit-current-tarballs: get-orig-source
	$(sparse-checkout-current)
	mkdir -p $(TARBALL_TREE)/$(UPSTREAM_VERSION)
	mv $(TARBALL_LIST) $(TARBALL_TREE)/$(UPSTREAM_VERSION)/
	cd $(TARBALL_TREE) && $(GIT) add $(UPSTREAM_VERSION) && if $(GIT) status -s $(UPSTREAM_VERSION) | grep ^A ; then $(GIT) commit -m "add $(UPSTREAM_VERSION) tarballs" $(UPSTREAM_VERSION) ; fi
