# Copyright Noah Meyerhans <noahm@debian.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

pkgname=debian-ami-validator
version=0.0.5

export GO111MODULE=on
SOURCES:=$(shell find . -name '*.go' ! -name '*_test.go')
BINARIES:=ami-wiki-generator

.PHONY: help
help:
	@egrep -h '\s##\s' $(MAKEFILE_LIST) | sort | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

all: $(BINARIES) ## build all binaries

$(BINARIES): $(SOURCES)
	go build

test: ## run tests
	go test -v

clean: ## clean build artifacts
	go clean
	-rm -f *~ $(BINARIES)

.PHONY: uncommitted-check
uncommitted-check:
	@if ! git update-index --refresh --unmerged || \
		! git diff-index --name-only --exit-code HEAD; then \
	echo "*** ERROR: Uncommitted changes in above files"; exit 1; fi

.PHONY: head-check
head-check:
	@if ! git diff --name-only --exit-code v${version} HEAD > /dev/null; then \
		echo "*** ERROR: Git checkout not at version ${version}"; exit 1; fi ; \

.PHONY: tag
tag: uncommitted-check ## Tag a new release
	@if git rev-parse --verify v$(version) > /dev/null 2>&1; then \
		echo "*** ERROR: Version $(version) is already tagged"; exit 1; fi
	git tag v${version}

.PHONY: sources
sources: head-check uncommitted-check ## generate a release tarball
	git archive --format=tar --prefix=${pkgname}-${version}/ v${version} \
		| lzma -c > ../${pkgname}-${version}.tar.lzma

.PHONY: scratch-sources
scratch-sources: version=$(shell git describe --dirty --tags | sed "s,^v,,")
scratch-sources: ## generate a tarball based on the current working copy
	tar cf ../${pkgname}-${version}.tar.lzma --lzma --exclude=.git --transform 's,^./,./${pkgname}-${version}/,' .

.PHONY: test all clean
