#!/usr/bin/make -f

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

DEB_HOST_ARCH		?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH	?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
VENDOR			:= $(shell dpkg-vendor --derives-from Ubuntu && echo Ubuntu || echo Debian)

include debian/rules.defs
CUDA_VERSION_TOOLKIT	:= $(shell dpkg-parsechangelog | sed -nr '/^Version:/s/Version: (.*:)?(.*)-(.*)/\2/p')
CUDA_VERSION_MAJOR	?= $(word 1,$(subst ., ,$(CUDA_VERSION_TOOLKIT)))
CUDA_VERSION_MINOR	?= $(word 2,$(subst ., ,$(CUDA_VERSION_TOOLKIT)))
version_driver		 = $(CUDA_VERSION_DRIVER)
version_driver_major	 = $(word 1,$(subst ., ,$(version_driver)))
watch_url		 = $(if $(CUDA_BETA),$(CUDA_WATCH_URL_BETA),$(CUDA_WATCH_URL))

package_libcuda1 \
	= libcuda1 (>= $${nvidia:MinVersion}) \
	| libcuda-$${cuda:SoVersion}-1
ifeq ($(VENDOR),Ubuntu)
package_libcuda1	+= | no-libcuda1 [ppc64el]
endif
        
package_nvidia-libopencl1 \
	= nvidia-libopencl1 (>= $${nvidia:MinVersion}) \
	| libopencl-1.2-1

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

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


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


nvidia-cuda-amd64: debian/rules debian/rules.shuffle
	dh_testdir
	$(RM) -r $@ $@.tmp
	sh $(CUDA_FILENAME.amd64) --noexec --keep --target $@.tmp
	$(MAKE) -C $@.tmp -f ../debian/rules.shuffle shuffle-nvidia-cuda-amd64
	mv $@.tmp $@

nvidia-cuda-ppc64el: debian/rules debian/rules.shuffle
	dh_testdir
	$(RM) -r $@ $@.tmp
	mkdir $@.tmp
	@set -e -x ; for deb in ppc64el/cuda*.deb ; do dpkg -x $$deb $@.tmp/ubuntutree ; done
	$(MAKE) -C $@.tmp -f ../debian/rules.shuffle shuffle-nvidia-cuda-ppc64el
	mv $@.tmp $@

unpack-stamp: nvidia-cuda-$(DEB_HOST_ARCH)
	ln -s nvidia-cuda-$(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 CP1255 -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 clean install
binary binary-arch build clean install:
	dh $@

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

override_dh_auto_configure: $(AUTOGEN) unpack-stamp

override_dh_auto_install:
	mkdir debian/tmp
	cp -al nvidia-cuda-$(DEB_HOST_ARCH) debian/tmp/usr
	chmod -x debian/tmp/usr/libnvvp/*.xpm
	chmod -x debian/tmp/usr/libnsight/*.xpm
	sed -i '/^-vm$$/ d; /^..\/jre\/bin\/java$$/ d' debian/tmp/usr/libnvvp/nvvp.ini debian/tmp/usr/libnsight/nsight.ini
	chmod -x debian/tmp/usr/bin/crt/link.stub
	chmod -x debian/tmp/usr/bin/crt/prelink.stub
	chmod -x debian/tmp/usr/nvvm/include/*.h
	chmod -x debian/tmp/usr/nvvm/libnvvm-samples/build.bat
ifeq ($(DEB_HOST_ARCH),ppc64el)
	chrpath -d debian/tmp/usr/bin/cuda-gdb
endif
	# remove tracking scripts
	rm -rfv debian/tmp/usr/doc/html/common/scripts
	# remove tracking images
	find debian/tmp/usr/doc/html -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 -->,' \
		{} +
	find debian/tmp/usr/libnsight debian/tmp/usr/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 -->,' \
		{} +
	# reduce 'dh_install --list-missing' noise
	rm -rf debian/tmp/usr/include/thrust
	rm -rf debian/tmp/usr/lib32
	rm -rf debian/tmp/usr/*/lib32

override_dh_install:
	dh_install --list-missing
	rm -rf debian/nvidia-visual-profiler/usr/share/nvidia-visual-profiler/plugins/org.eclipse.equinox.launcher.gtk.linux.*
	rm -rf debian/nvidia-nsight/usr/share/nvidia-nsight/plugins/org.eclipse.equinox.launcher.gtk.linux.*

override_dh_installman:
	dh_installman --language=C

override_dh_makeshlibs:
	dh_makeshlibs -X/stubs/

override_dh_shlibdeps:
	mkdir libcuda
	ln -s ../nvidia-cuda/lib/stubs/libcuda.so libcuda/libcuda.so.1
	echo 'libcuda 1 XXXlibcuda1XXX' > debian/shlibs.local
	dh_shlibdeps -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_strip:
override_dh_strip_nondeterminism:

override_dh_compress:
	dh_compress -Xusr/share/doc/nvidia-cuda-doc/examples

override_dh_gencontrol:
	dh_gencontrol -- \
		-V'package:libcuda1=$(package_libcuda1)' \
		-V'package:nvidia-libopencl1=$(package_nvidia-libopencl1)' \
		-V'cuda:SoVersion=$(CUDA_SOVERSION)' \
		-V'nvidia:MinVersion=$(version_driver)'

override_dh_auto_clean:
	$(RM) -r .pc
	$(RM) nvidia-cuda
	$(RM) -r nvidia-cuda-amd64
	$(RM) -r nvidia-cuda-ppc64el
	$(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{#CUDA_DOWNLOAD_URL#}{$(watch_url)}g;' \
	-e 's{#LIBDIR#}{$(libdir)}g;' \
	< $< > $@

lib%$(CUDA_SOVERSION).install:: lib%SOVER.install
	cp $< $@

lib%$(CUDA_SOVERSION).links:: lib%SOVER.links
	cp $< $@

lib%$(CUDA_SOVERSION).lintian-overrides:: lib%SOVER.lintian-overrides
	cp $< $@

lib%$(CUDA_SOVERSION).symbols:: lib%SOVER.symbols
	cp $< $@

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

CACHE			?= ../.cache
ORIGDIR			?= nvidia-cuda-toolkit-$(CUDA_VERSION_TOOLKIT).orig
TARBALL_BASE		?= nvidia-cuda-toolkit_$(CUDA_VERSION_TOOLKIT).orig
TARBALL_SUFFIX		?= .tar.xz
TARBALL			?= $(TARBALL_BASE)$(TARBALL_SUFFIX)
ORIGDIR.amd64		?= $(ORIGDIR)
TARBALL.amd64		?= $(TARBALL)
ORIGDIR.ppc64el		?= $(ORIGDIR)-ppc64el
TARBALL.ppc64el		?= $(TARBALL_BASE)-ppc64el$(TARBALL_SUFFIX)

CUDA_DL_FILENAME.amd64	?= cuda_$(CUDA_VERSION_TOOLKIT)_linux.run
CUDA_DL_FILENAME.ppc64el?= ???

get-orig-source-prepare:
	$(RM) $(TARBALL)
	$(RM) $(TARBALL.amd64)
	$(RM) $(TARBALL.ppc64el)
	$(RM) -r get-orig-source || true
	mkdir get-orig-source

get-orig-source-download-%: get-orig-source-prepare
	ln -v $(CACHE)/$(CUDA_DL_FILENAME.$*) get-orig-source/ 2>/dev/null || \
		wget $(CUDA_DL_URL_BASE)/$(CUDA_DL_FILENAME.$*) -P get-orig-source/
	if [ -d $(CACHE) ] && [ ! -f $(CACHE)/$(CUDA_DL_FILENAME.$*) ]; then \
		ln -v get-orig-source/$(CUDA_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-download-%
	@set -e -x ; \
	    cd get-orig-source ; \
	    mkdir $(ORIGDIR.$*) ; \
	    sh $(CUDA_DL_FILENAME.$*) --noexec --keep --target tmp ; \
	    find tmp -ls ; \
	    if [ -d tmp/run_files ]; then \
	        mv -f tmp/run_files/cuda-linux*-rel*.run tmp/run_files/EULA.txt $(ORIGDIR.$*)/ ; \
	    else \
	        mv "$(CUDA_DL_FILENAME.$*)" $(ORIGDIR.$*)/ ; \
	    fi ; \
	    rm -rf tmp ; \
	    chmod 0755 $(ORIGDIR.$*)/*.run

# unpack the all-in-one .deb local-repo installer
get-orig-source-deb-unpack-%: get-orig-source-download-%
	@set -e -x ; \
	    cd get-orig-source ; \
	    mkdir $(ORIGDIR.$*) ; \
	    dpkg -X $(CUDA_DL_FILENAME.$*) tmp.$* ; \
	    mv tmp.$*/var/cuda-repo-$(CUDA_VERSION_MAJOR)-$(CUDA_VERSION_MINOR)-local/*.deb $(ORIGDIR.$*) ; \
	    rm -rf tmp.$* ; \
	    : "# Remove undistributable files:" ; \
	    rm -fv $(ORIGDIR.$*)/cuda-samples-$(CUDA_VERSION_MAJOR)-$(CUDA_VERSION_MINOR)_*.deb ; \
	    : "# Remove driver:" ; \
	    rm -fv $(ORIGDIR.$*)/libcuda1-$(version_driver_major)_*_*.deb ; \
	    rm -fv $(ORIGDIR.$*)/nvidia-$(version_driver_major)*_*_*.deb ; \
	    rm -fv $(ORIGDIR.$*)/gpu-deployment-kit_*_*.deb ; \
	    chmod 0644 $(ORIGDIR.$*)/*.deb

get-orig-source-unpack-amd64: get-orig-source-run-unpack-amd64
get-orig-source-unpack-ppc64el: get-orig-source-deb-unpack-ppc64el

get-orig-source-pack-%: get-orig-source-unpack-%
	@set -e -x ; \
	    cd get-orig-source ; \
	    latestfile=$$(ls -tr $$(find $(ORIGDIR.$*) -type f) | tail -n 1) ; \
	    XZ_OPT=-9 tar cfvJ $(TARBALL.$*) \
	        --clamp-mtime --mtime="./$$latestfile" \
		--sort=name \
		--owner=root --group=src \
		$(ORIGDIR.$*) \
		; \
	    touch $(TARBALL.$*) -r $$latestfile
	mv get-orig-source/$(TARBALL.$*) .

get-orig-source-pack-main: get-orig-source-pack-amd64

.PHONY: get-orig-source
get-orig-source: get-orig-source-pack-main get-orig-source-pack-ppc64el
	rm -rf get-orig-source
