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

SRCDIR := /usr/src/packages/SOURCES
#export GOCACHE=/tmp/gocache

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

# This has to be exported to make some magic below work.
export DH_OPTIONS
export DH_GOPKG := github.com/grafana/loki

#dh_golang doesn't do this for you
ifeq ($(DEB_HOST_ARCH), i386)
	export GOARCH := 386
else ifeq ($(DEB_HOST_ARCH), amd64)
	export GOARCH := amd64
else ifeq ($(DEB_HOST_ARCH), armhf)
	export GOARCH := arm
else ifeq ($(DEB_HOST_ARCH), arm64)
	export GOARCH := arm64
endif
BUILD_DIR := obj-$(DEB_HOST_GNU_TYPE)
export PATH := $(CURDIR)/$(BUILD_DIR)/bin:$(PATH)

export CGO_ENABLED=0

#export DH_GOPKG := github.com/Debian/dcs
export GOFLAGS=-mod=vendor -buildmode=pie -tags=netgo
export GOLDFLAGS=-s -w -X github.com/grafana/loki/pkg/build.Version=2.2.1 \
                        -X github.com/grafana/loki/pkg/build.Revision=1 \
                        -X github.com/grafana/loki/pkg/build.Branch=NA \
                        -X github.com/grafana/loki/pkg/build.BuildUser=NA \
                        -X github.com/grafana/loki/pkg/build.BuildDate=NA
                        

# Exclude extra files in -dev pacakge to make it slimmer.
EXTRA_EXCLUDES += vendor
EXTRA_EXCLUDES += $(wildcard cmd/*/*.go)
#EXTRA_EXCLUDES += $(filter-out pkg/cri/annotations,$(wildcard pkg/cri/*))

export DH_GOLANG_EXCLUDES := $(EXCLUDES)

# 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


#override_dh_auto_configure:
#	cp -rvT debian/vendor ./vendor/
#	dh_auto_configure

## TODO: Remove hardcoded version & release
override_dh_auto_build:
##	go install -tags netgo std
#	dh_auto_build
	go build '-ldflags=$(GOLDFLAGS)' ./cmd/loki
	go build '-ldflags=$(GOLDFLAGS)' ./cmd/logcli
	CGO_ENABLED=1 go build '-ldflags=$(GOLDFLAGS)' ./cmd/promtail
	pwd
	ls -l
	ls -l /usr/src/packages/SOURCES/

override_dh_auto_test:

override_dh_auto_install:
	pwd
#	ls -l $(BUILD_DIR)/src/github.com/grafana/loki
	ls -l debian
	mkdir -p debian/grafana-loki/usr/bin
	## Service files for Loki and promtail
	## TODO: Remove hardcoded `grafana-loki`
	install -p -Dm644 $(SRCDIR)/loki.service debian/grafana-loki/usr/lib/systemd/system/loki.service
	install -p -Dm644 $(SRCDIR)/promtail.service debian/grafana-loki/usr/lib/systemd/system/promtail.service
	#install -Dm644 sysconfig.loki
	#install -Dm644 sysconfig.promtail
	# Binaries
	#TODO: Use `$(DESTDIR)` instead of `debian/`
	install -Dm755 loki $(CURDIR)/debian/grafana-loki/usr/bin/
	install -Dm755 promtail $(CURDIR)/debian/grafana-loki/usr/bin/
	install -Dm755 logcli $(CURDIR)/debian/grafana-loki/usr/bin/
	# Config files
	install -Dm644 cmd/loki/loki-local-config.yaml $(CURDIR)/debian/grafana-loki/etc/loki/loki.yaml
	install -Dm644 cmd/promtail/promtail-local-config.yaml $(CURDIR)/debian/grafana-loki/etc/loki/promtail.yaml
	ls -l $(CURDIR)/debian/grafana-loki/
	#DH_GOLANG_EXCLUDES="$(EXCLUDES) $(EXTRA_EXCLUDES)" dh_auto_install
	dh_auto_install

%:
# Makefile build
# GoLanguage build
	mkdir -p /tmp/gocache
	GO111MODULE=on
	GOCACHE=/tmp/gocache
#	dh $@ --buildsystem=golang --with=golang
	dh $@
