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

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

# GOPKG is the upstream path which you would normally “go get”.
# Using it allows us to build applications without patching locations.
GOPKG := code.google.com/p/codesearch

# Temporary working directory to which the source will be copied, then
# compiled.
TMPGOPATH = $(CURDIR)/debian/tmp/usr/share/gocode

override_dh_auto_install:
	mkdir -p ${TMPGOPATH}/src/${GOPKG}
	# Copy all .go files to /usr/share/gocode (we compile and ship).
	find . -path ./debian -prune -o -type f -name "*.go" -exec tar cf - {} + | (cd "${TMPGOPATH}/src/${GOPKG}" && tar xvf -)
	# Ensure that GOPATH is clean: It should only contain the temporary
	# /usr/src/golang containing the package we want to install and the
	# /usr/src/golang of the system we are building on. It should
	# specifically NOT contain the user’s local ~/gocode.
	GOPATH=${TMPGOPATH}:/usr/share/gocode go install -v ${GOPKG}/...
	deps=`dpkg-query -f='$${source:Package} (= $${source:Version}), ' -W $$(perl -MParse::DebControl -E 'my $$p = Parse::DebControl->new(); my $$data = $$p->parse_file("./debian/control"); my @deps = grep { /^\s*golang-/ } split(",", $$data->[0]->{"Build-Depends"}); say join(" ", @deps)')`; \
        for pkg in `dh_listpackages`; do \
                echo "misc:Built-Using=$$deps" >> debian/$$pkg.substvars; \
        done

%:
	dh $@ 
