.PHONY: all precommit setup lint test integration_test build run fix jenkins
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
SERVICE = graphdb
MIGRATION_DIR = "cmd/graphdb-migrate"
RESYNC_DIR = "cmd/graphdb-resync"
GOMETALINTER_REV="dcb9b10f17"
include $(SELF_DIR)vendor/code.justin.tv/feeds/service-common/CommonMakefile.mk

lint:
	gometalinter -s proto --vendor --dupl-threshold=150 --cyclo-over=15 --min-confidence=.3 --tests --deadline=90s --disable-all -Egolint -Etest -Eineffassign -Etestify -Eunconvert -Estaticcheck -Egoconst -Egocyclo -Eerrcheck -Egofmt -Evet -Edupl -Einterfacer -Estructcheck -Evetshadow -Egosimple -Egoimports -Evarcheck -Emisspell -Emaligned -Etest --exclude=/usr/local/go/src --exclude='/mocks/[^/]+\.go.*\((dupl)|(golint)\)' ./...

install_protoc_linter:
	go get github.com/ckaznocha/protoc-gen-lint

protoc:
	protoc --lint_out=. --proto_path=$(GOPATH)/src:. --twirp_out=$(GOPATH)/src --go_out=$(GOPATH)/src proto/graphdb.proto
	protoc --lint_out=. --proto_path=$(GOPATH)/src:. --twirp_out=$(GOPATH)/src --go_out=$(GOPATH)/src proto/datastore.proto
	protoc --lint_out=. --proto_path=$(GOPATH)/src:. --twirp_out=$(GOPATH)/src --go_out=$(GOPATH)/src proto/private.proto
	protoc --lint_out=. --proto_path=$(GOPATH)/src:. --twirp_out=$(GOPATH)/src --go_out=$(GOPATH)/src proto/dynamoevent.proto
	protoc --lint_out=. --proto_path=$(GOPATH)/src:. --twirp_out=$(GOPATH)/src --go_out=$(GOPATH)/src proto/admin.proto

jenkins: setup lint test static_build static_build_migration static_build_resync integration_test

static_build_migration:
	go build -ldflags "-linkmode external -extldflags -static" -o ./cmd/graphdb-migrate/graphdb-migrate ./cmd/graphdb-migrate
	go build -ldflags "-linkmode external -extldflags -static" -race -o ./cmd/graphdb-migrate/graphdb-migrate_race ./cmd/graphdb-migrate
	touch -mat 201701010000.00 ./cmd/graphdb-migrate/graphdb-migrate
	touch -mat 201701010000.00 ./cmd/graphdb-migrate/graphdb-migrate_race
	[ ! -e config ] || touch -mat 201701010000.00 config/

static_build_resync:
	go build -ldflags "-linkmode external -extldflags -static" -o ./cmd/graphdb-resync/graphdb-resync ./cmd/graphdb-resync
	go build -ldflags "-linkmode external -extldflags -static" -race -o ./cmd/graphdb-resync/graphdb-resync_race ./cmd/graphdb-resync
	touch -mat 201701010000.00 ./cmd/graphdb-resync/graphdb-resync
	touch -mat 201701010000.00 ./cmd/graphdb-resync/graphdb-resync_race
	[ ! -e config ] || touch -mat 201701010000.00 config/

run_migrate:
	env ENVIRONMENT=$(ENVIRONMENT) go run  $(MIGRATION_DIR)/main.go

run_resync:
	env ENVIRONMENT=$(ENVIRONMENT) go run  $(RESYNC_DIR)/main.go

package_lambda:
	GOOS=linux GOARCH=amd64 go build -o graphdb_dynamo_lambda cmd/graphdb-dynamo-lambda/main.go
	zip graphdb_dynamo_lambda.zip graphdb_dynamo_lambda

deploy_lambda_integration: package_lambda
	aws --profile twitch-feed-dev  --region us-west-2 lambda update-function-code --function-name=graphdb-integration-graphdb_dynamo_lambda --zip-file=fileb://graphdb_dynamo_lambda.zip
	aws --profile twitch-feed-dev  --region us-east-1 lambda update-function-code --function-name=graphdb-integration-graphdb_dynamo_lambda --zip-file=fileb://graphdb_dynamo_lambda.zip
	rm -f graphdb_dynamo_lambda.zip graphdb_dynamo_lambda

deploy_lambda_staging: package_lambda
	aws --profile twitch-feed-dev  --region us-west-2 lambda update-function-code --function-name=graphdb-staging-graphdb_dynamo_lambda --zip-file=fileb://graphdb_dynamo_lambda.zip
	aws --profile twitch-feed-dev  --region us-east-1 lambda update-function-code --function-name=graphdb-staging-graphdb_dynamo_lambda --zip-file=fileb://graphdb_dynamo_lambda.zip
	rm -f graphdb_dynamo_lambda.zip graphdb_dynamo_lambda

deploy_lambda_production: package_lambda
	aws --profile twitch-feed-aws  --region us-west-2 lambda update-function-code --function-name=graphdb-production-graphdb_dynamo_lambda --zip-file=fileb://graphdb_dynamo_lambda.zip
	aws --profile twitch-feed-aws  --region us-east-1 lambda update-function-code --function-name=graphdb-production-graphdb_dynamo_lambda --zip-file=fileb://graphdb_dynamo_lambda.zip
	rm -f graphdb_dynamo_lambda.zip graphdb_dynamo_lambda

package_lambda_slack:
	GOOS=linux GOARCH=amd64 go build -o graphdb_slack_lambda cmd/graphdb-slack-lambda/*.go
	zip graphdb_slack_lambda.zip graphdb_slack_lambda

deploy_slack_lambda: package_lambda_slack
	aws --profile twitch-feed-aws  --region us-west-2 lambda update-function-code --function-name=graphdb-slack-lambda-production-graphdb_slack_lambda --zip-file=fileb://graphdb_slack_lambda.zip
	rm -f graphdb_slack_lambda.zip graphdb_slack_lambda
