export GOPROXY=https://proxy.golang.org

GO ?= go
EPOCH_TEST_COMMIT ?= 394c06f491fe9f1c28a410e3b0b91916a5119406
DESTDIR ?=
LIBEXECDIR ?= ${PREFIX}/libexec/cni
PREFIX ?= /usr/local


FIRST_GOPATH := $(firstword $(subst :, ,$(GOPATH)))
GOPKGDIR := $(FIRST_GOPATH)/src/$(PROJECT)
GOPKGBASEDIR ?= $(shell dirname "$(GOPKGDIR)")

SELINUXOPT ?= $(shell test -x /usr/sbin/selinuxenabled && selinuxenabled && echo -Z)

GO_BUILD=$(GO) build
# Go module support: set `-mod=vendor` to use the vendored sources
ifeq ($(shell go help mod >/dev/null 2>&1 && echo true), true)
	GO_BUILD=GO111MODULE=on $(GO) build -mod=vendor
endif

GOBIN := $(shell $(GO) env GOBIN)
ifeq ($(GOBIN),)
GOBIN := $(FIRST_GOPATH)/bin
endif

COMMIT_NO ?= $(shell git rev-parse HEAD 2> /dev/null || true)
GIT_COMMIT ?= $(if $(shell git status --porcelain --untracked-files=no),${COMMIT_NO}-dirty,${COMMIT_NO})

LDFLAGS_MACHINE ?= -X main.gitCommit=$(GIT_COMMIT)

all: binaries

validate: install.tools gofmt .gitvalidation lint

gofmt:
	find . -name '*.go' ! -path './vendor/*' -exec gofmt -s -w {} \+
	git diff --exit-code


binaries:
	$(GO_BUILD) -ldflags '$(LDFLAGS_MACHINE)' -o bin/podman-machine github.com/containers/podman-machine-cni/plugins/meta/podman-machine

.PHONY: .gitvalidation
.gitvalidation:
	GIT_CHECK_EXCLUDE="./vendor" $(GOBIN)/git-validation -v -run DCO,short-subject,dangling-whitespace -range $(EPOCH_TEST_COMMIT)..$(HEAD)

.PHONY: install.tools
install.tools: .install.gitvalidation .install.golangci-lint

lint: .install.golangci-lint
	$(GOBIN)/golangci-lint run

define go-get
	env GO111MODULE=off \
		$(GO) get -u ${1}
endef


.install.gitvalidation:
	if [ ! -x "$(GOBIN)/git-validation" ]; then \
		$(call go-get,github.com/vbatts/git-validation); \
	fi

.install.golangci-lint:
	if [ ! -x "$(GOBIN)/golangci-lint" ]; then \
		curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOBIN)/ v1.23.6; \
	fi

install:
	install ${SELINUXOPT} -d -m 755 $(DESTDIR)$(LIBEXECDIR)
	install ${SELINUXOPT} -m 755 bin/podman-machine $(DESTDIR)$(LIBEXECDIR)/podman-machine

clean:
	rm -fr bin/

vendor:
	export GO111MODULE=on \
		$(GO) mod tidy && \
		$(GO) mod vendor && \
		$(GO) mod verify

.PHONY: vendor-in-container
vendor-in-container:
	podman run --privileged --rm --env HOME=/root -v `pwd`:/src -w /src docker.io/library/golang:1.13 make vendor

.PHONY: \
	binaries \
	test \
	gofmt \
	lint \
	validate \
	vendor
