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

PACKAGE = golang-weekly

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

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

override_dh_auto_clean:
	rm -rf bin
	rm -f debian/*+
	rm -f debian/build.stamp
	: NEEDSWORK: update src/pkg/Makefile.
	#cd src/pkg && $(GOBIN)/gomake exp/ogle.clean
	rm -f src/pkg/Make.deps
	rm -f test/pass.out test/run.out test/times.out

override_dh_auto_build: debian/build.stamp

override_dh_auto_test:
	:
#	$(no_check) || { cd src && bash ./run.bash --no-rebuild; }

override_dh_compress:
	dh_compress -Xusr/share/doc/$(PACKAGE)-doc/html -Xusr/share/doc/$(PACKAGE)-doc/godoc

override_dh_install:
	dh_install --fail-missing
	for f in $$(find src/cmd -name doc.go ; find src/pkg -name \*.go); do \
	  install -D -m 0644 $$f debian/$(PACKAGE)-src/$(datadir)/$$f; \
	done
	make -f debian/native-symlinks.mk PACKAGE=$(PACKAGE)

override_dh_strip:
	dh_strip -X".a" -Xgoinstall -Xgodoc -Xgoyacc -Xbin/cgo -Xebnflint -Xgofix -Xgofmt -Xgovet -Xgotest --dbg-package=$(PACKAGE)-dbg

debian/build.stamp:
	rm -f debian/build.stamp
	mkdir -p $(GOBIN)
	+cd src && bash ./make.bash
	>debian/build.stamp

opt_no_act =
no_check = ! :

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)))
    no_check = :
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)
    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)
    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
    $(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)
    GOARM := 5
else ifeq ($(DEB_HOST_ARCH), armhf)
    GOARM := 6
else
    $(error unrecognized arm architecture $(DEB_HOST_ARCH)!)
endif
else
    $(error unrecognized instruction set $(DEB_HOST_ARCH_CPU)!)
endif

export GOROOT GOROOT_FINAL GOOS GOARCH GOARM GOBIN GOHOSTARCH GOHOSTOS

REPO = https://go.googlecode.com/hg/
GOLANG_ORIG_DIR = $(CURDIR)/../go-weekly
get-orig-source:
	-set -e; \
	if [ ! -d $(GOLANG_ORIG_DIR) ]; then \
		hg clone -r"weekly" "$(REPO)" "$(GOLANG_ORIG_DIR)"; \
	fi
	cd "$(GOLANG_ORIG_DIR)"; \
	hg pull; \
	hg update "weekly"; \
	VERSION=$$(hg identify -t -r"weekly" | perl -e '<> =~ /weekly\.(\d+)-(\d+)-(\d+)([\.\d]*)/; print "$$1.$$2.$$3$$4"'); \
	rm -rf "$(CURDIR)/../$(PACKAGE)-$$VERSION/"; \
	hg archive -tfiles -r"weekly" -X'.hg*' -X'doc/talks/go_talk-20091030.pdf' "$(CURDIR)/../$(PACKAGE)-$$VERSION/"; \
	src/version.bash -save; \
	cp VERSION "$(CURDIR)/../$(PACKAGE)-$$VERSION/"; \
	cd ..; \
	tar -czf "$(CURDIR)/../$(PACKAGE)_$$VERSION.orig.tar.gz" "$(PACKAGE)-$$VERSION/"; \
	rm -rf "$(CURDIR)/../$(PACKAGE)-$$VERSION/"; \
	echo "Now run git-import-orig $(CURDIR)/../$(PACKAGE)_$$VERSION.orig.tar.gz"

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