NAME=jira

OS=$(shell uname -s)
ifeq ($(filter CYGWIN%,$(OS)),$(OS))
	export CWD=$(shell cygpath -wa .)
	export SEP=\\
	export CYGWIN=winsymlinks:native
	BIN ?= $(GOBIN)$(SEP)$(NAME).exe
else
	export CWD=$(shell pwd)
	export SEP=/
	BIN ?= $(GOBIN)$(SEP)$(NAME)
endif

DIST=$(CWD)$(SEP)dist

GOBIN ?= $(CWD)

CURVER ?= $(patsubst v%,%,$(shell [ -d .git ] && git describe --abbrev=0 --tags || grep ^\#\# CHANGELOG.md | awk '{print $$2; exit}'))
LDFLAGS:= -w

build:
	go build -gcflags="-e" -v -ldflags "$(LDFLAGS) -s" -o '$(BIN)' cmd/jira/main.go

vet:
	@go vet .
	@go vet ./jiracli
	@go vet ./jiracmd
	@go vet ./jiradata
	@go vet ./cmd/jira

lint:
	@go get github.com/golang/lint/golint
	@golint .
	@golint ./jiracli
	@golint ./jiracmd
	@golint ./jiradata
	@golint ./cmd/jira

all:
	go get -u github.com/karalabe/xgo
	rm -rf dist
	mkdir -p dist
	xgo --go 1.9.0 --targets="freebsd/amd64,linux/386,linux/amd64,windows/386,windows/amd64,darwin/amd64" -dest ./dist -ldflags="-w -s" ./cmd/jira

install:
	${MAKE} GOBIN=$$HOME/bin build

NEWVER ?= $(shell echo $(CURVER) | awk -F. '{print $$1"."$$2"."$$3+1}')
TODAY  := $(shell date +%Y-%m-%d)

changes:
	@git log --pretty=format:"* %s [%cn] [%h]" --no-merges ^v$(CURVER) HEAD *.go jiracli/*.go jiradata/*.go jiracmd/*.go cmd/*/*.go *.lock | grep -vE 'gofmt|go fmt|version bump'

update-changelog:
	@echo "# Changelog" > CHANGELOG.md.new; \
	echo >> CHANGELOG.md.new; \
	echo "## $(NEWVER) - $(TODAY)" >> CHANGELOG.md.new; \
	echo >> CHANGELOG.md.new; \
	$(MAKE) --no-print-directory --silent changes | \
	perl -pe 's{\[([a-f0-9]+)\]}{[[$$1](https://github.com/Netflix-Skunkworks/go-jira/commit/$$1)]}g' | \
	perl -pe 's{\#(\d+)}{[#$$1](https://github.com/Netflix-Skunkworks/go-jira/issues/$$1)}g' >> CHANGELOG.md.new; \
	tail -n +2 CHANGELOG.md >> CHANGELOG.md.new; \
    perl -pi -e 's{VERSION = "$(CURVER)"}{VERSION = "$(NEWVER)"}' jira.go; \
	mv CHANGELOG.md.new CHANGELOG.md; \
	$(NULL)

release:
	perl -pi -e 'undef $$/; s/\n```\nusage.*```//sg' README.md
	echo '```' >> README.md
	./jira --help >> README.md 2>&1 || true
	echo '```' >> README.md
	git diff --exit-code --quiet README.md || git commit -m "Updated Usage" README.md
	git commit -m "Updated Changelog" CHANGELOG.md
	git commit -m "version bump" jira.go
	git tag v$(NEWVER)
	git push --tags

version:
	@echo $(CURVER)

clean:
	rm -rf ./$(NAME)

prove:
	chmod -R g-rwx,o-rwx $(CWD)/t/.gnupg
	OSHT_VERBOSE=1 prove -v 

generate:
	cd schemas && ./fetch-schemas.py
	grep -h slipscheme jiradata/*.go | grep json | sort | uniq | awk -F\/\/ '{print $$2}' | while read cmd; do $$cmd; done
