export PATH := $(CURDIR)/_tools/bin:$(PATH)
SHELL := /bin/bash

GO_BUILD=mkdir -p libexec && cd libexec && go build -race
GO_TEST=go test -race

all:: help

.PHONY: help glide setup-tools

help:
	@echo "Missing make target:"
	@echo " - make setup: Setup dev environment"
	@echo " - make go: Build Golang TMI programs"
	@echo " - make fmt: Run goimports to fix formatting"
	@echo " - make lint: Run golint"
	@echo " - make test: Run TMI unit tests"
	@echo " - make mocks: Generate interface mocks for tests"
	@echo " - make proto: Compile proto files into relevant source locations"

setup:
	brew install geoip go haproxy mercurial postgresql redis ruby glide
	go get -u github.com/golang/protobuf/proto
	go get -u github.com/bmizerany/pq
	go get -u github.com/vektra/mockery
	go get -u github.com/sgotti/glide-vc
	gem install foreman

setup-tools:
	GOPATH=$(CURDIR)/_tools go install all

glide:
	glide cc
	glide up --strip-vendor
	glide-vc --only-code --no-tests --use-lock-file --keep '**/*.json'

.PHONY: go room pubsub clue edge http_edge firehose logger

go: room pubsub edge clue http_edge firehose logger
room:
	$(GO_BUILD) code.justin.tv/chat/tmi/room
pubsub:
	$(GO_BUILD) code.justin.tv/chat/tmi/pubsub
edge:
	$(GO_BUILD) code.justin.tv/chat/tmi/irc
clue:
	$(GO_BUILD) -o clue code.justin.tv/chat/tmi/clue
http_edge:
	$(GO_BUILD) code.justin.tv/chat/tmi/api
firehose:
	$(GO_BUILD) code.justin.tv/chat/tmi/firehose
logger:
	$(GO_BUILD) code.justin.tv/chat/tmi/logger

.PHONY: fmt lint vet

fmt:
	goimports -w=true -l=true `find $(CURDIR) -name "*.go" | grep -v "/vendor/"`
lint:
	golint `go list code.justin.tv/chat/tmi/... | grep -v /vendor/`
vet:
	go vet `go list code.justin.tv/chat/tmi/... | grep -v /vendor/`

.PHONY: check_version test test_coverage test_go test_room test_edge test_http_edge test_clue test_message test_firehose

check_version:
	case `go version` in "go version go1.8"*) exit 0;; *) echo "go version 1.8 required" >&2; exit 1;; esac
test: check_version vet mocks test_go

test_coverage: check_version vet mocks
	mkdir -p test-results/
	gocov test -race -v `go list code.justin.tv/chat/tmi/... | grep -v /vendor/` | gocov-xml > test-results/coverage.xml

test_go:
	$(GO_TEST) `go list code.justin.tv/chat/tmi/... | grep -v /vendor/`
test_room:
	$(GO_TEST) code.justin.tv/chat/tmi/room/...
test_edge:
	$(GO_TEST) code.justin.tv/chat/tmi/irc/...
test_http_edge:
	$(GO_TEST) code.justin.tv/chat/tmi/api/...
test_clue:
	$(GO_TEST) code.justin.tv/chat/tmi/clue/...
test_message:
	$(GO_TEST) code.justin.tv/chat/tmi/message/...
test_firehose:
	$(GO_TEST) code.justin.tv/chat/tmi/firehose/...

.PHONY: mocks proto

GO_MOCK=mockery -note @generated -inpkg -dir $(GOPATH)/src/code.justin.tv/chat/tmi
mocks: setup-tools
	rm -f $(GOPATH)/src/code.justin.tv/chat/tmi/clue/logic/mock_*.go
	rm -f $(GOPATH)/src/code.justin.tv/chat/tmi/clue/models/mock_*.go
	rm -f $(GOPATH)/src/code.justin.tv/chat/tmi/clue/server/mock_*.go
	#go install github.com/vektra/mockery/...
	$(GO_MOCK)/client -name Client
	$(GO_MOCK)/clients -name IMStore
	$(GO_MOCK)/clue/logic -name UserData
	$(GO_MOCK)/clue/logic -name User
	$(GO_MOCK)/clue/logic -name Room
	$(GO_MOCK)/clue/data -name SiteDB
	$(GO_MOCK)/clue/data -name TmiDB
	$(GO_MOCK)/clue/data -name LocalCache
	$(GO_MOCK)/clue/data -name CohesionChatData
	$(GO_MOCK)/clue/logic -name BatchUserData
	$(GO_MOCK)/clue/logic -name ChannelData
	$(GO_MOCK)/clue/logic -name ChatProperties
	$(GO_MOCK)/clue/logic -name MessageProperties
	$(GO_MOCK)/clue/logic -name IPProperties
	$(GO_MOCK)/clue/logic -name ClueLogic
	$(GO_MOCK)/clue/logic -name GroupRoomData
	$(GO_MOCK)/clue/logic -name RateTracker
	$(GO_MOCK)/clue/logic -name goldenKappaHelpers
	$(GO_MOCK)/clue/logic -name ClueSampleRates
	$(GO_MOCK)/irc/borders -name Pub
	$(GO_MOCK)/clue/models -name BatchDataHelper
	find . -type f -name "*.go" | grep -v 'mock_Client.go' | grep -v -e vendor -e _tools | xargs -I{} perl -p -i -e 's/github\.com\/stretchr\/testify/github.com\/brildum\/testify/g' {}

fakes:
	counterfeiter -o api/fake_querier.go api Querier

proto:
	go install github.com/golang/protobuf/...
	cd proto && protoc --plugin=$(GOPATH)/bin/protoc-gen-go --go_out=.. room.proto

PROJECT_ID=af0697ec9
API_KEY=a08f43f6-f58b-44a5-ac60-24d523e2b15b

extract-strings:
	go-i18n extract --pkg ./clue/translation --out ./clue/translation/translations.json --tmpl ./clue/translation/extract.tmpl

upload-strings:
	go-i18n upload --project-id $(PROJECT_ID) --api-key $(API_KEY) --file ./clue/translation/translations.json

download-strings:
	go-i18n compile --project-id $(PROJECT_ID) --api-key $(API_KEY) --file-name ./clue/translation/translations.json --go-out ./clue/translation/translations.go --go-pkg translation
	cp ./clue/translation/temp_translations ./clue/translation/translations.go # using template until translations are complete, tested

sandstorm:
	sandstorm-agent run local_conf
develop: sandstorm
	foreman start
