#!/usr/bin/make -f

export DH_VERBOSE=1

PARALLEL=$(shell echo $$DEB_BUILD_OPTIONS|xargs -n 1|grep ^parallel=|cut -d= -f2)
ifneq ($(strip $(PARALLEL)),)
        MAKE_ARGS=-j$(PARALLEL)
endif

%:
	dh $@ --builddirectory=_build --buildsystem=golang --with=golang

override_dh_auto_install:
	dh_auto_install -- --no-source

override_dh_auto_test:

override_dh_auto_clean:
	rm -rf cuda2go cuda/cuda2go
	dh_auto_clean --buildsystem=golang

override_dh_auto_build:
	make -C cuda realclean
	DH_GOLANG_BUILDPKG="github.com/mumax/3/util" dh_auto_build $@ --builddirectory=_build --buildsystem=golang
	mkdir -p cuda2go
	tail -n +3 cuda/cuda2go.go > cuda2go/cuda2go.go
	cd cuda2go && GOPATH=$(CURDIR)/_build GOCACHE=$(CURDIR)/_build/go-build GOMODCACHE=$(CURDIR)/_build/pkg/mod GO111MODULE=off go build .
	cp cuda2go/cuda2go cuda/
	make -C cuda wrappers CUDA_CC="50 52 53 60 61 62 70" $(MAKE_ARGS)
	dh_auto_build $@ --builddirectory=_build --buildsystem=golang
	for i in doc test ; do mv _build/bin/$$i _build/bin/mumax3-$$i ; done
	for i in mumax3 mumax3-convert mumax3-httpfsd mumax3-plot mumax3-script mumax3-server mumax3-doc mumax3-test ; do PATH=_build/bin:$$PATH help2man --output=$$i.1 --version-string=$$(dpkg-parsechangelog -S Version | sed s/-[^-]*$$//) --help-option -help --no-discard-stderr $$i ; done
