#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

export DH_VERBOSE := 1

# Exclude "cover" and "vet" since they're included in core as of Go 1.5+
export DH_GOLANG_EXCLUDES := \
	golang.org/x/tools/cmd/cover \
	golang.org/x/tools/cmd/vet

# Install all files into the build GOPATH, not only .go files.
# This is necessary because go.tools contains golden files that are necessary
# for running the testsuite.
export DH_GOLANG_INSTALL_ALL := 1

# go.tools installs godoc into $GOROOT/bin, so create a temporary $GOROOT into
# which it is actually allowed to write (as opposed to /usr/lib/go).
export GOROOT_FINAL := /usr/lib/go
export GOROOT := $(CURDIR)/temproot

override_dh_auto_clean:
	dh_auto_clean
	rm -rf '$(GOROOT)'

override_dh_auto_build:
	mkdir -p '$(GOROOT)'
	set -ex && for f in '$(GOROOT_FINAL)'/*; do \
		ln -sT "$$f" "$(GOROOT)/$$(basename "$$f")"; \
	done
	# recreate 'bin' for godoc
	rm '$(GOROOT)/bin' && mkdir '$(GOROOT)/bin'
	dh_auto_build

override_dh_auto_install:
	dh_auto_install
	# Rename “eg” to “golang-eg” (#753978)
	mv debian/tmp/usr/bin/eg debian/tmp/usr/bin/golang-eg
	# Rename “stress” to “golang-stress” (#793693)
	mv debian/tmp/usr/bin/stress debian/tmp/usr/bin/golang-stress
	# Rename “bundle” to “golang-bundle” (#818551)
	mv debian/tmp/usr/bin/bundle debian/tmp/usr/bin/golang-bundle
	# Remove extra LICENSE file, which is installed because we run with DH_GOLANG_INSTALL_ALL=1.
	rm debian/tmp/usr/share/gocode/src/golang.org/x/tools/LICENSE
	rm debian/tmp/usr/share/gocode/src/golang.org/x/tools/.gitattributes
	# Make scripts executable.
	find debian/tmp/usr/share/gocode/src/golang.org/x/tools -name '*.bash' -exec chmod +x '{}' +
	# Replace embedded copies of jquery and jquery-ui with a symlink.
	rm debian/tmp/usr/share/gocode/src/golang.org/x/tools/godoc/static/jquery.js && \
		ln -s \
			/usr/share/javascript/jquery/jquery.min.js \
			debian/tmp/usr/share/gocode/src/golang.org/x/tools/godoc/static/jquery.js
	rm debian/tmp/usr/share/gocode/src/golang.org/x/tools/cmd/present/static/jquery-ui.js && \
		ln -s \
			/usr/share/javascript/jquery-ui/jquery-ui.min.js \
			debian/tmp/usr/share/gocode/src/golang.org/x/tools/cmd/present/static/jquery-ui.js

override_dh_auto_test:
	# use short tests where possible (especially to skip tests like TestWebIndex which are very slow)
	dh_auto_test -- -test.short

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