GIT_COMMIT ?= $(shell git rev-parse --verify HEAD)
ENVIRONMENT ?= development
MAIN_DIR ?= ./cmd/watchers
WORKER_DIR ?= ./cmd/worker
TEST_PACKAGES ?= $(shell go list ./... | grep -v /vendor/)

setup:
	go get -u golang.org/x/tools/cmd/goimports
	go get -u github.com/smartystreets/goconvey
	go get -u github.com/alecthomas/gometalinter
	gometalinter --install

precommit: fix fast_lint integration_test_cover

test:
	go test $(TEST_PACKAGES)

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

integration_test_cover:
	mkdir -p build/cover
	env ENVIRONMENT=$(ENVIRONMENT) go test -race -tags=integration -coverprofile build/cover/cover.out -coverpkg ./cmd/...,./internal/...,./external/... $(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

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

revendor:
	dep ensure

lint:
	gometalinter ./...

fast_lint:
	gometalinter --fast ./...

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: 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 build -ldflags "-X main.CodeVersion=$(GIT_COMMIT)" $(WORKER_DIR)
	go env
	go version
	env
	mkdir /build/service
	mkdir /build/worker
	cp $(MAIN_DIR)/Dockerfile /build/service
	cp $(WORKER_DIR)/Dockerfile /build/worker
	cp watchers /build/service
	cp worker /build/worker
	cp -R ./config /build/service
	cp -R ./config /build/worker

TWITCH_CB_AWS = 989470033077

create_watchers_rds_snapshot_src_production:
	python scripts/validate_aws.py ${TWITCH_CB_AWS}
	aws cloudformation create-stack --template-body file://cloudformation/database/rds_snapshot_src.yaml --cli-input-json file://cloudformation/database/create_rds_snapshot_src_production.json

update_watchers_rds_snapshot_src_production:
	python scripts/validate_aws.py ${TWITCH_CB_AWS}
	aws cloudformation update-stack --template-body file://cloudformation/database/rds_snapshot_src.yaml --cli-input-json file://cloudformation/database/update_rds_snapshot_src_production.json

create_watchers_rds_snapshot_dest_production:
	python scripts/validate_aws.py ${TWITCH_CB_AWS}
	aws cloudformation create-stack --template-body file://cloudformation/database/rds_snapshot_dest.yaml --cli-input-json file://cloudformation/database/create_rds_snapshot_dest_production.json

update_watchers_rds_snapshot_dest_production:
	python scripts/validate_aws.py ${TWITCH_CB_AWS}
	aws cloudformation update-stack --template-body file://cloudformation/database/rds_snapshot_dest.yaml --cli-input-json file://cloudformation/database/update_rds_snapshot_dest_production.json

validate_template:
	aws cloudformation validate-template --template-body file://cloudformation/database/rds_snapshot_src.yaml
	aws cloudformation validate-template --template-body file://cloudformation/database/rds_snapshot_dest.yaml
