#!/usr/bin/make -f
# -*- makefile -*-

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

PKGDIR=debian/tmp
VERSION=$(shell grep "var Version" $(CURDIR)/shared/version/flex.go | cut -d'"' -f2)
include /usr/share/dpkg/default.mk

# temporary build path (see http://golang.org/doc/code.html#GOPATH)
export DH_GOPKG := github.com/lxc/lxd
export GOPATH := $(CURDIR)/obj-$(DEB_BUILD_GNU_TYPE)
export USE_EMBEDDED := true
export USE_SHLIBS := false
export CGO_LDFLAGS=-L$(CURDIR)/dist/sqlite/.libs/
export CGO_CFLAGS=-I$(CURDIR)/dist/sqlite/

ifeq ($(USE_SHLIBS), true)
export DH_GOLANG_LINK_SHARED := 1
endif

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

override_dh_auto_build:
	# Build our own sqlite3
	cd dist/sqlite && ./configure --enable-replication && make

	# And then build using it
	dh_auto_build -- -tags libsqlite3

# Ugly workaround for bundled dependencies
override_dh_auto_configure:
	dh_auto_configure

	# dh-golang's configure has copied the source tree into GOPATH. But
	# because lxd gets some dependencies from the archive and some from
	# the copies bundled in dist, we have to unpick a bunch of what it has
	# done and set it up again.

	# Remove the extra copy of dist dh-golang has copied onto GOPATH (or
	# when dh-golang tries to run go install github.com/lxc/lxd/... things
	# get very confused).
	rm -Rf ${GOPATH}/src/github.com/lxc/lxd/dist

	# Move the lxd source aside while we do this.
	mv ${GOPATH}/src/github.com/lxc/lxd ${GOPATH}/lxd.tmp

	# Clean GOPATH.
ifeq ($(USE_SHLIBS), true)
	rm -Rf ${GOPATH}/src/srclibs
else
	rm -Rf ${GOPATH}/src
endif

ifeq ($(USE_EMBEDDED), true)
	# If we get all dependencies from dist, just copy it onto GOPATH.
	cp -R dist/src ${GOPATH}

	# But not the symlink for lxd.
	rm -f ${GOPATH}/src/github.com/lxc/lxd
else
	# If not, link depedencies from dist or from where the distro package
	# has installed it, as appropriate.

	# Packaged dependencies
	debian/helpers/link-from-installed github.com/dustinkirkland/golang-petname
	debian/helpers/link-from-installed github.com/golang/protobuf
	debian/helpers/link-from-installed github.com/gorilla/context
	debian/helpers/link-from-installed github.com/gorilla/mux
	debian/helpers/link-from-installed github.com/gorilla/websocket
	debian/helpers/link-from-installed github.com/gosexy/gettext
	debian/helpers/link-from-installed github.com/mattn/go-colorable
	debian/helpers/link-from-installed github.com/mattn/go-sqlite3
	debian/helpers/link-from-installed github.com/olekukonko/tablewriter
	debian/helpers/link-from-installed github.com/pborman/uuid
	debian/helpers/link-from-installed github.com/syndtr/gocapability
	debian/helpers/link-from-installed golang.org/x/crypto
	debian/helpers/link-from-installed gopkg.in/flosch/pongo2.v3
	debian/helpers/link-from-installed gopkg.in/inconshreveable/log15.v2
	debian/helpers/link-from-installed gopkg.in/lxc/go-lxc.v2
	debian/helpers/link-from-installed gopkg.in/tomb.v2
	debian/helpers/link-from-installed gopkg.in/yaml.v2
endif

	# And put the lxd source back again.
	mkdir -p ${GOPATH}/src/github.com/lxc/
	mv ${GOPATH}/lxd.tmp ${GOPATH}/src/github.com/lxc/lxd

override_dh_install:
	# Install lxc-to-lxd
	cp scripts/lxc-to-lxd $(PKGDIR)/usr/bin/lxc-to-lxd

	# Install the manpages
	mkdir -p $(PKGDIR)/usr/share/man/man1/
	help2man $(PKGDIR)/usr/bin/fuidshift -n "uid/gid shifter" --no-info > $(PKGDIR)/usr/share/man/man1/fuidshift.1
	help2man $(PKGDIR)/usr/bin/lxc-to-lxd -n "Convert LXC containers to LXD" --no-info --version-string=$(VERSION) > $(PKGDIR)/usr/share/man/man1/lxc-to-lxd.1
	help2man $(PKGDIR)/usr/bin/lxd-benchmark -n "The container lightervisor - benchmark" --no-info --no-discard-stderr > $(PKGDIR)/usr/share/man/man1/lxd-benchmark.1
	$(PKGDIR)/usr/bin/lxd manpage $(PKGDIR)/usr/share/man/man1/
	$(PKGDIR)/usr/bin/lxc manpage $(PKGDIR)/usr/share/man/man1/

	# Install bash completion scripts
	mkdir -p $(PKGDIR)/usr/share/bash-completion/completions/
	cp scripts/bash/lxd-client $(PKGDIR)/usr/share/bash-completion/completions/lxc

	# Prepare dev package
	rm -Rf $(PKGDIR)/usr/share/gocode/src/github.com/lxc/
	mkdir -p $(PKGDIR)/usr/share/gocode/src/github.com/lxc/
	cp -RL ${GOPATH}/src/github.com/lxc/lxd $(PKGDIR)/usr/share/gocode/src/github.com/lxc/
	rm -Rf $(PKGDIR)/usr/share/gocode/src/github.com/lxc/lxd/dist

	# Copy dnsmasq configuration
	mkdir -p $(PKGDIR)/etc/dnsmasq.d-available
	cp debian/lxd.dnsmasq $(PKGDIR)/etc/dnsmasq.d-available/lxd

	# Setup logrotate
	mkdir -p $(PKGDIR)/etc/logrotate.d
	cp debian/lxd.logrotate $(PKGDIR)/etc/logrotate.d/lxd

	# Setup sysctl
	mkdir -p $(PKGDIR)/etc/sysctl.d
	cp debian/lxd.sysctl $(PKGDIR)/etc/sysctl.d/10-lxd-inotify.conf

	# Include wrappers and sqlite3 library
	mkdir -p $(PKGDIR)/usr/lib/lxd/
	mv $(PKGDIR)/usr/bin/lxd $(PKGDIR)/usr/lib/lxd/
	cp debian/wrappers/lxd $(PKGDIR)/usr/bin/
	cp debian/wrappers/shutdown $(PKGDIR)/usr/lib/lxd/
	cp debian/wrappers/upgrade-bridge $(PKGDIR)/usr/lib/lxd/
	cp dist/sqlite/.libs/libsqlite3.so.0* $(PKGDIR)/usr/lib/lxd/

	# Trigger normal dh_install
	dh_install

override_dh_installinit:
ifeq (,$(findstring $(DEB_DISTRIBUTION), trusty xenial zesty))
	# Disable upstart integration on artful+
	rm -f debian/lxd.upstart
	cp debian/lxd.maintscript.in debian/lxd.maintscript
else
	# Enable upstart integration for backports
	cp debian/lxd.upstart.in debian/lxd.upstart
	sed -i "s#@LIBEXECDIR@#/usr/lib/${DEB_HOST_MULTIARCH}#g" debian/lxd.upstart
	grep -v /etc/init debian/lxd.maintscript.in > debian/lxd.maintscript
endif
	# We handle starting and restarting things ourselves
	dh_systemd_enable -plxd --name=lxd-containers lxd-containers.service
	dh_installinit -plxd --no-start
	dh_installinit -plxd --name=lxd-containers --no-start --noscripts
	dh_systemd_start -plxd lxd-containers.service --no-restart-on-upgrade --no-restart-after-upgrade

	# Update arch-specific paths
	sed -i "s#@LIBEXECDIR@#/usr/lib/${DEB_HOST_MULTIARCH}#g" debian/lxd/lib/systemd/system/lxd.service
	sed -i "s#@LIBEXECDIR@#/usr/lib/${DEB_HOST_MULTIARCH}#g" debian/lxd/etc/init.d/lxd

override_dh_systemd_start:
	echo "Not running dh_systemd_start"

override_dh_auto_test:
	echo "The testsuite requires privileges and so is run through autopkgtest"
