#!/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

export DH_GOPKG := golang.org/x/tools

# The go/types/... packages currently don’t pass tests.
export DH_GOLANG_EXCLUDES := golang.org/x/tools/go/types

# 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 into $GOROOT, 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_build:
	mkdir -p $(CURDIR)/temproot
	# We need a writable copy of pkg, go.tools installs into that.
	cp -ar /usr/lib/go/pkg $(CURDIR)/temproot/pkg
	ln -s /usr/lib/go/src $(CURDIR)/temproot/src
	ln -s /usr/share/go/test $(CURDIR)/temproot/test
	ln -s /usr/share/doc/golang-doc/html $(CURDIR)/temproot/doc
	echo "listing:"
	ls -hlR $(CURDIR)
	# TODO(stapelberg): remove the next line once dh-golang calls go generate
	(cd obj-$(DEB_BUILD_GNU_TYPE) && GOPATH=$$PWD go generate golang.org/x/tools/benchmark/parse golang.org/x/tools/blog golang.org/x/tools/blog/atom golang.org/x/tools/cmd/benchcmp golang.org/x/tools/cmd/callgraph golang.org/x/tools/cmd/cover golang.org/x/tools/cmd/digraph golang.org/x/tools/cmd/eg golang.org/x/tools/cmd/fiximports golang.org/x/tools/cmd/godex golang.org/x/tools/cmd/godoc golang.org/x/tools/cmd/goimports golang.org/x/tools/cmd/gomvpkg golang.org/x/tools/cmd/gorename golang.org/x/tools/cmd/gotype golang.org/x/tools/cmd/html2article golang.org/x/tools/cmd/oracle golang.org/x/tools/cmd/present golang.org/x/tools/cmd/ssadump golang.org/x/tools/cmd/stress golang.org/x/tools/cmd/stringer golang.org/x/tools/cmd/tipgodoc golang.org/x/tools/cmd/vet golang.org/x/tools/cmd/vet/whitelist golang.org/x/tools/container/intsets golang.org/x/tools/cover golang.org/x/tools/go/ast/astutil golang.org/x/tools/go/buildutil golang.org/x/tools/go/callgraph golang.org/x/tools/go/callgraph/cha golang.org/x/tools/go/callgraph/rta golang.org/x/tools/go/callgraph/static golang.org/x/tools/go/exact golang.org/x/tools/go/gccgoimporter golang.org/x/tools/go/gcimporter golang.org/x/tools/go/importer golang.org/x/tools/go/loader golang.org/x/tools/go/pointer golang.org/x/tools/go/ssa golang.org/x/tools/go/ssa/interp golang.org/x/tools/go/ssa/ssautil golang.org/x/tools/go/vcs golang.org/x/tools/godoc golang.org/x/tools/godoc/analysis golang.org/x/tools/godoc/redirect golang.org/x/tools/godoc/static golang.org/x/tools/godoc/util golang.org/x/tools/godoc/vfs golang.org/x/tools/godoc/vfs/gatefs golang.org/x/tools/godoc/vfs/httpfs golang.org/x/tools/godoc/vfs/mapfs golang.org/x/tools/godoc/vfs/zipfs golang.org/x/tools/imports golang.org/x/tools/oracle golang.org/x/tools/oracle/serial golang.org/x/tools/playground golang.org/x/tools/playground/socket golang.org/x/tools/present golang.org/x/tools/refactor/eg golang.org/x/tools/refactor/importgraph golang.org/x/tools/refactor/lexical golang.org/x/tools/refactor/rename golang.org/x/tools/refactor/satisfy)
	dh_auto_build $@

override_dh_auto_install:
	dh_auto_install $@
	# Install all binaries from bin/ and pkg/ to /usr/bin/ and
	# /usr/lib/go/pkg, respectively.
	echo "listing CURDIR=$(CURDIR):"
	ls -hlR $(CURDIR)
	# Rename “eg” to “golang-eg” (#753978)
	mv $(CURDIR)/debian/tmp/usr/bin/eg $(CURDIR)/debian/tmp/usr/bin/golang-eg
	(cd $(CURDIR)/temproot/bin; \
	 for file in $$(find . -type f -newer $$(find $(CURDIR)/temproot/pkg/tool/ -name "fix")); do \
		mkdir -p $(CURDIR)/debian/tmp/usr/bin/$$(dirname $$file); \
		install $$file $(CURDIR)/debian/tmp/usr/bin/$$file; \
	done)
	(cd $(CURDIR)/temproot/pkg; \
	 for file in $$(find . -type f -newer $$(find $(CURDIR)/temproot/pkg/tool/ -name "fix")); do \
		mkdir -p $(CURDIR)/debian/tmp/usr/lib/go/pkg/$$(dirname $$file); \
		install $$file $(CURDIR)/debian/tmp/usr/lib/go/pkg/$$file; \
	done)
	# Remove extra LICENSE file, which is installed because we run with DH_GOLANG_INSTALL_ALL=1.
	-rm $(CURDIR)/debian/tmp/usr/share/gocode/src/golang.org/x/tools/LICENSE
	-rm $(CURDIR)/debian/tmp/usr/share/gocode/src/golang.org/x/tools/.gitattributes
	# Make scripts executable.
	chmod +x $(CURDIR)/debian/tmp/usr/share/gocode/src/golang.org/x/tools/cmd/godoc/setup-godoc-app.bash
	chmod +x $(CURDIR)/debian/tmp/usr/share/gocode/src/golang.org/x/tools/cmd/oracle/emacs-test.bash
	# Replace embedded copies of jquery and jquery-ui with a symlink.
	-rm $(CURDIR)/debian/tmp/usr/share/gocode/src/golang.org/x/tools/godoc/static/jquery.js && \
	ln -s /usr/share/javascript/jquery/jquery.min.js $(CURDIR)/debian/tmp/usr/share/gocode/src/golang.org/x/tools/godoc/static/jquery.js
	-rm $(CURDIR)/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 $(CURDIR)/debian/tmp/usr/share/gocode/src/golang.org/x/tools/cmd/present/static/jquery-ui.js

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