.PHONY: upgrade help test
OSTAG=$(shell uname -s | tr '[:upper:]' '[:lower:]')

# TODO is there a way to autogenerate help?
help:
	@echo ""
	@echo "Targets:"
	@echo "\tsetup		        - install tools"
	@echo "\tupdate-go-dependencies - update go dependencies"
	@echo "\ttwirp                  - regenerate go files from twirp model"
	@echo "\tmocks                  - regenerate mocks from interfaces"
	@echo "\ttest		        - run go test and gometalinter locally in a docker container"
	@echo "\tlocal		        - run after 'make test' to run the built service on your laptop"
	@echo "		                  'AWS_PROFILE=twitch_ds_private make local' will emit cloudwatch metrics to your personal account"
	@echo ""


tools: setup

setup:
	./bin/install_proto.bash
	GOPATH=$(CURDIR)/_tools GOBIN=$(CURDIR)/_tools/bin go install github.com/twitchtv/retool/...
	$(CURDIR)/_tools/bin/retool build
	GOPATH=$(CURDIR)/_tools GOBIN=$(CURDIR)/_tools/bin go install -v --tags "libsqlite3 $(OSTAG)" all

tools-clean:
	rm -rf $(CURDIR)/_tools/bin
	rm -rf $(CURDIR)/_tools/pkg

update-go-dependencies: setup
	dep ensure

clean:
	go clean ./...

twirp: setup
	# Auto-generate code
	clang-format -i cmd/discovery/rpc/discovery.proto 
	$(CURDIR)/_tools/bin/retool do protoc --proto_path=. --twirp_out=. --go_out=. cmd/discovery/rpc/discovery.proto

dev:
	go run cmd/discovery/main.go -local

mocks: setup
	go generate ./...

test:
	docker build -t discovery:latest-test -f Dockerfile-localtest .

local: test
	# set AWS_PROFILE=twitch-ext-disco-dev so the process can fetch the db login secrets from AWS Secrets Manager
	# To make a connection, make local requires you to have a connection made to the bastion host in the same
	# account and for port-forwarding to have been set up between localhost:5432 and the remote database endpoint
	# through the bastion host
	# Failure to have an AWS_PROFILE set will result in this error:
	#    panic: Unable to load DB config for discoman postgres database: NoCredentialProviders: no valid providers in chain. Deprecated.
	# Failure to have that port-forwarding set up will result in this error:
	#    panic: Unable to connect to discoman postgres database: dial tcp 192.168.65.2:5432: connect: connection refused
	# curl -kvi https://localhost:8443/debug/running to confirm it is running
	# curl http://localhost:8080/debug/running to confirm it is running
	docker run -v ~/.aws:/root/.aws -p 8080:8080/tcp -p 9443:8443/tcp -e AWS_PROFILE -e AWS_REGION -e AWS_DEFAULT_REGION discovery:latest-test /go/bin/discovery
