GIT_COMMIT ?= $(shell git rev-parse --verify HEAD)
ENVIRONMENT ?= development
MAIN_DIR ?= ./cmd/meepo
TEST_PACKAGES ?= $(shell go list ./... | grep -v /vendor/)
GOBIN := $(CURDIR)/_bin
PATH := $(GOBIN):$(PATH)

# As GO111MODULE=on is the default as of Go 1.16, this line helps keep
# this makefile from having weird errors with newer Go versions, since
# this project uses Go Dep instead of Go Modules.
export GO111MODULE := off

APPNAME := meepo
ACCOUNTNAME := twitch-feed-aws
ACCOUNTID := 914569885343
ACCOUNTROLE := admin
AWSREGION := us-west-2
AWSPROFILE := $(shell AWS_STS_REGIONAL_ENDPOINTS=regional aws sts get-caller-identity --region $(AWSREGION) >/dev/null 2>&1 || echo "--profile $(ACCOUNTNAME)")
DEVACCOUNTNAME := twitch-feed-dev
DEVACCOUNTID := 724951484461
DEVAWSPROFILE := $(shell AWS_STS_REGIONAL_ENDPOINTS=regional aws sts get-caller-identity --region $(AWSREGION) >/dev/null 2>&1 || echo "--profile $(DEVACCOUNTNAME)")

SECURITY_GROUP := sg-5c05723a
SUBNET_A := subnet-e488ff80
SUBNET_B := subnet-8f4ac4f9
SUBNET_C := subnet-128b5d4a
DEVSECURITY_GROUP := sg-71057217
DEVSUBNET_A := subnet-9e4ac4e8
DEVSUBNET_B := subnet-0a88ff6e
DEVSUBNET_C := subnet-668b5d3e

.PHONY: rpc

ada_creds:
	ada credentials update $(AWSPROFILE) --account=$(ACCOUNTID) --provider=Isengard --role=$(ACCOUNTROLE) --once && aws configure set region $(AWSREGION) $(AWSPROFILE) || echo "No profile specified"

ada_creds_dev:
	ada credentials update $(DEVAWSPROFILE) --account=$(DEVACCOUNTID) --provider=Isengard --role=$(ACCOUNTROLE) --once && aws configure set region $(AWSREGION) $(DEVAWSPROFILE) || echo "No profile specified"

setup:
	go get -u golang.org/x/tools/cmd/goimports
	go get -u github.com/smartystreets/goconvey
	go get -u github.com/vektra/mockery/v2/.../
	brew tap alecthomas/homebrew-tap
	brew reinstall golangci/tap/golangci-lint
	go install google.golang.org/protobuf/cmd/protoc-gen-go
	go get -u github.com/twitchtv/twirp/protoc-gen-twirp
	go get -u github.com/ckaznocha/protoc-gen-lint
	go get -u -d github.com/lib/pq
	brew reinstall golang-migrate
	brew reinstall clang-format

precommit: fix fast_lint integration_test_cover

test:
	go test -v $(TEST_PACKAGES)

integration_test:
	env ENVIRONMENT=$(ENVIRONMENT) go test -race -v -tags=integration $(TEST_PACKAGES)

integration_test_no_cache:
	env ENVIRONMENT=$(ENVIRONMENT) go test -race -count=1 -tags=integration $(TEST_PACKAGES)

integration_test_cover:
	mkdir -p build/cover
	cd -P . && env ENVIRONMENT=$(ENVIRONMENT) go test -v -race -tags=integration -coverprofile build/cover/cover.out -coverpkg ./cmd/...,./internal/api/...,./internal/auth/...,./internal/cache/...,./internal/datastore/...,./internal/playerstore/... $(TEST_PACKAGES)
	@go tool cover -func build/cover/cover.out > build/cover/function_coverage.txt
	@# pipe to cat to swallow the error code
	@grep '\s0.0%' build/cover/function_coverage.txt | grep -v '\smain\s' | cat > build/cover/uncovered_functions.txt
	@if [ -s build/cover/uncovered_functions.txt ]; then (echo "### There are uncovered functions ###" && cat build/cover/uncovered_functions.txt && exit 1) fi;

run:
	env ENVIRONMENT=$(ENVIRONMENT) go run -race -ldflags "-X main.CodeVersion=$(GIT_COMMIT)" $(MAIN_DIR)/main.go

update:
	dep ensure

lint:
	golangci-lint run

fast_lint:
	golangci-lint run --fast

proto_lint:
	clang-format -style="{BasedOnStyle: Google, IndentWidth: 4}" -verbose -i rpc/*.proto

fix:
	find . -iname '*.go' -not -path '*/vendor/*' -print0 | xargs -0 gofmt -s -w
	find . -iname '*.go' -not -path '*/vendor/*' -print0 | xargs -0 goimports -w

jenkins: jenkins_setup lint integration_test_cover
	@# I have no idea why, but trying to get jenkins to run a build target works with local manta but fails with jenkins build agent manta...
	@# just going to bypass the whole issue and move the build command directly into the jenkins target in the makefile
	go build -ldflags "-X main.CodeVersion=$(GIT_COMMIT)" $(MAIN_DIR)
	go env
	go version
	env
	mkdir /build/service
	cp $(MAIN_DIR)/Dockerfile /build/service
	cp meepo /build/service
	cp -R ./config /build/service

jenkins_setup:
	@wget -O - -q https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOBIN) v1.17.1

channel_livecheck_lambda_build:
	GOOS=linux GOARCH=amd64 go build -o build/lambda/channel_livecheck/main lambda/channel_livecheck/main.go

eventbus_lambda_build:
	GOOS=linux GOARCH=amd64 go build -o build/lambda/eventbus/main lambda/eventbus/main.go

ttl_deletion_lambda_build:
	GOOS=linux GOARCH=amd64 go build -o build/lambda/ttl_deletion/main lambda/ttl_deletion/main.go

rpc: proto_lint
	go generate -x ./rpc

mocks:
	go generate -x ./internal/mocks

validate_templates: ada_creds
	echo "Checking AWS CloudFormation templates..."
	aws cloudformation validate-template $(AWSPROFILE) --template-body file://cloudformation/infra/event_bus.yaml 1>/dev/null
	echo "AWS CloudFormation validation passed."

update_event_bus: validate_templates ada_creds
	# Usage: make update_event_bus
	aws cloudformation deploy --stack-name $(APPNAME)-production-event-bus \
		--template-file cloudformation/infra/event_bus.yaml \
		--profile $(ACCOUNTNAME) \
		--parameter-overrides AppName=$(APPNAME) \
		                      Environment=production \
		                      SubnetA=$(SUBNET_A) \
		                      SubnetB=$(SUBNET_B) \
		                      SubnetC=$(SUBNET_C) \
		                      SecurityGroup=$(SECURITY_GROUP) \
		--capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND

update_event_bus_dev: validate_templates ada_creds_dev
	# Usage: make update_event_bus_dev
	aws cloudformation deploy --stack-name $(APPNAME)-staging-event-bus \
		--template-file cloudformation/infra/event_bus.yaml \
		--profile $(DEVACCOUNTNAME) \
		--parameter-overrides AppName=$(APPNAME) \
		                      Environment=staging \
		                      SubnetA=$(DEVSUBNET_A) \
		                      SubnetB=$(DEVSUBNET_B) \
		                      SubnetC=$(DEVSUBNET_C) \
		                      SecurityGroup=$(DEVSECURITY_GROUP) \
		--capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND
