#!/usr/bin/make -f
# This file is in the public domain.
# You may freely use, modify, distribute, and relicense it.

libexecdir := /usr/lib/go
datadir := /usr/share/go
bindir := /bin

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
golang_archs = armel armhf amd64 i386 arm64 ppc64el

build-arch build-indep build clean install binary-arch binary-indep binary: debian/control
	+dh --parallel $(opt_no_act) $@

override_dh_auto_clean:
	rm -rf bin pkg
	rm -f debian/*+
	rm -f debian/build.stamp
	rm -f test/pass.out test/run.out test/times.out
	rm -rf debian/home

override_dh_auto_build: debian/build.stamp

override_dh_auto_test:
ifneq (,$(filter $(DEB_HOST_ARCH),$(golang_archs)))
ifeq ($(RUN_TEST), true)
	cd $(CURDIR)/src && \
	$(CURDIR)/debian/test.bash
else
	:
endif
endif

override_dh_compress:
	dh_compress -Xusr/share/doc/golang-doc/html -Xusr/share/doc/golang-doc/godoc

override_dh_install:
ifneq (,$(filter $(DEB_HOST_ARCH),$(golang_archs)))
	# Install the native runtime/cgo.a to the golang-go package.
	# runtime/cgo is only built for the native architecture, but the
	# golang-go-OS-ARCH packages are built on one particular buildd, which
	# might either be i386 or amd64.
	echo "listing of $$(pwd):"; ls -hlR .
	echo "listing of GOBIN: ($(GOBIN))"; ls -hlR $(GOBIN)
	for file in pkg/*/runtime/cgo.a; do \
		mkdir -p $(CURDIR)/debian/golang-go/usr/lib/go/$$(dirname $$file); \
		mv $$file $(CURDIR)/debian/golang-go/usr/lib/go/$$file; \
	done
	dh_install --fail-missing
	# Remove .syso files of the race detector; it is not used yet and the files are arch-dependent.
	find $(CURDIR)/debian/golang-src/usr/share/go/src/runtime/race -type f -name '*.syso' -delete
	# Remove Plan9 rc(1) scripts
	find $(CURDIR)/debian/golang-src/usr/share/go/src -type f -name "*.rc" -delete
	# Remove empty /usr/share/go/src from golang-go, it is provided by golang-src
	find $(CURDIR)/debian/golang-go/usr/share/go/src -type d -delete
	# Touch built and installed files and directories to have same timestamp
	touch $(CURDIR)/debian/golang-go/usr/lib/go/pkg
	find $(CURDIR)/debian/golang-go/usr/lib/go/pkg -exec touch -r $(CURDIR)/debian/golang-go/usr/lib/go/pkg {} \;
endif

override_dh_strip:
	# strip disabled as golang upstream doesn't support it and it makes go
	# crash. See http://bugs.debian.org/717172

override_dh_installdeb:
	dh_installdeb
ifeq (,$(filter $(DEB_HOST_ARCH),$(golang_archs)))
	: # these are empty dependency packages
	rm -f debian/golang-go/DEBIAN/p*
	rm -rf debian/golang-go/usr/share/man
	install -d -m 755 debian/golang-src/usr/share/go/src
	install -d -m 755 debian/golang-src/usr/share/go/include
	install -d -m 755 debian/golang-src/usr/share/go/test
endif

override_dh_builddeb:
	dh_builddeb -- -Zxz

override_dh_shlibdeps:
	dh_shlibdeps -Xtestdata -Xtest

debian/build.stamp:
ifneq (,$(filter $(DEB_HOST_ARCH),$(golang_archs)))
	[ -f VERSION ] || echo "debian snapshot +$$(dpkg-parsechangelog -SVersion)" > VERSION
	rm -f debian/build.stamp
	mkdir -p $(GOBIN)
	export GOROOT_BOOTSTRAP=$$(env -i go env GOROOT) && cd src && bash ./make.bash --no-banner
	>debian/build.stamp
else
	touch debian/build.stamp
endif

opt_no_act =
RUN_TEST := true

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
    $(warning no support for disabling optimization)
endif

ifneq (,$(findstring n,$(MAKEFLAGS)))
    opt_no_act = --no-act
endif

ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
    RUN_TEST := false
endif

GOROOT := $(CURDIR)
GOROOT_FINAL := $(libexecdir)
GOBIN := $(CURDIR)$(bindir)
GOARM :=

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH 2>/dev/null)
DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS 2>/dev/null)
DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU 2>/dev/null)
DEB_BUILD_ARCH_OS := $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS 2>/dev/null)
DEB_BUILD_ARCH_CPU := $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU 2>/dev/null)

ifeq ($(DEB_BUILD_ARCH_OS), kfreebsd)
    RUN_TEST := false
    GOHOSTOS := freebsd
else ifeq ($(DEB_BUILD_ARCH_OS), linux)
    GOHOSTOS := linux
else
    $(error unrecongized build host kernel $(DEB_BUILD_ARCH_OS)!)
endif

ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
    RUN_TEST := false
    GOOS := freebsd
else ifeq ($(DEB_HOST_ARCH_OS), linux)
    GOOS := linux
else
    $(error unrecognized kernel $(DEB_HOST_ARCH_OS)!)
endif

ifeq ($(DEB_BUILD_ARCH_CPU), i386)
    GOHOSTARCH := 386
else ifeq ($(DEB_BUILD_ARCH_CPU), amd64)
    GOHOSTARCH := amd64
else ifeq ($(DEB_BUILD_ARCH_CPU), arm)
    GOHOSTARCH := arm
else ifeq ($(DEB_BUILD_ARCH_CPU), armhf)
    GOHOSTARCH := arm
else ifeq ($(DEB_BUILD_ARCH_CPU), arm64)
    GOHOSTARCH := arm64
else ifeq ($(DEB_BUILD_ARCH_CPU), ppc64el)
    GOHOSTARCH := ppc64le
else
    #$(error unrecognized build host instruction set $(DEB_HOST_ARCH_CPU)!)
endif

ifeq ($(DEB_HOST_ARCH_CPU), i386)
    GOARCH := 386
    GOPREFIX := 8
else ifeq ($(DEB_HOST_ARCH_CPU), amd64)
    GOARCH := amd64
    GOPREFIX := 6
else ifeq ($(DEB_HOST_ARCH_CPU), arm)
    GOARCH := arm
    GOPREFIX := 5
ifeq ($(DEB_HOST_ARCH), armel)
    RUN_TEST := false
    GOARM := 5
else ifeq ($(DEB_HOST_ARCH), armhf)
    GOARM := 6
else
    $(error unrecognized arm architecture $(DEB_HOST_ARCH)!)
endif
else ifeq ($(DEB_HOST_ARCH_CPU), arm64)
    GOHOSTARCH := arm64
else ifeq ($(DEB_HOST_ARCH_CPU), ppc64el)
    GOHOSTARCH := ppc64le
else
    #$(error unrecognized instruction set $(DEB_HOST_ARCH_CPU)!)
endif

export GOROOT GOROOT_FINAL GOOS GOARCH GOARM GOBIN GOHOSTARCH GOHOSTOS GO386

.PHONY: build clean install binary-arch binary-indep binary
