.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 -Estructcheck -Evetshadow -Egoimports -Evarcheck -Emisspell -Etest --exclude=/usr/local/go/src --exclude='/mocks/[^/]+\.go.*\((dupl)|(golint)\)' ./...

install_protoc_linter:
	go get github.com/ckaznocha/protoc-gen-lint
	go get -u code.justin.tv/hygienic/twirpserviceslohook/cmd/slogenerator || true
	go install code.justin.tv/hygienic/twirpserviceslohook/cmd/slogenerator

# override common's makefile "setup" so we can fetch gometalinter differently
setup:
	$(call go_get_rev,"golang.org/x/tools/cmd/goimports",$(GOIMPORTS_REV))
	$(call go_get_rev,"github.com/wadey/gocovmerge",$(GOCOVMERGE_REV))
	go get -u github.com/alecthomas/gometalinter
	go get -u github.com/jstemmer/go-junit-report

	gometalinter --install
	echo $(PATH)
	which go
	which gcc
	go version
	which gometalinter

test:
	go test -v -race ./...

test_with_coverage:
	go test -v -race ./... -coverprofile=coverage.txt

integration_test:
	AWS_REGION=us-west-2 AWS_STS_REGIONAL_ENDPOINTS=regional AWS_PROFILE=twitch-feed-dev ENVIRONMENT=integration go test -race -run=. -v -tags=integration ./...

run:
	AWS_REGION=us-west-2 AWS_STS_REGIONAL_ENDPOINTS=regional AWS_PROFILE=twitch-feed-dev ENVIRONMENT=development go run -race ./cmd/graphdb/main.go

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
	slogenerator proto/datastorerpc/datastore.twirp.go datastorerpc > proto/datastorerpc/slo.go
	slogenerator proto/graphdb/graphdb.twirp.go graphdb > proto/graphdb/slo.go
	slogenerator proto/dynamoevent/dynamoevent.twirp.go dynamoevent > proto/dynamoevent/slo.go


jenkins: setup integration_test test static_build static_build_migration static_build_resync

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

cloudformation_infra_staging:
	aws --profile twitch-feed-dev  --region us-west-2 cloudformation deploy  --stack-name feeds-staging-infra  --template-file cloudformation/infra/infra.yaml  --parameter-overrides Environment=staging  --capabilities CAPABILITY_IAM

cloudformation_infra_canary:
	aws --profile twitch-feed-aws  --region us-west-2 cloudformation deploy  --stack-name feeds-canary-infra  --template-file cloudformation/infra/infra.yaml  --parameter-overrides Environment=canary  --capabilities CAPABILITY_IAM

cloudformation_infra_production:
	aws --profile twitch-feed-aws  --region us-west-2 cloudformation deploy  --stack-name feeds-production-infra  --template-file cloudformation/infra/infra.yaml  --parameter-overrides Environment=production  --capabilities CAPABILITY_IAM

cloudformation_aws_endpoints_staging:
	aws --profile twitch-feed-dev  --region us-west-2 cloudformation create-stack  --stack-name aws-endpoints-staging  --template-body file://./cloudformation/aws-endpoints/aws-endpoints.yaml  --parameters file://./cloudformation/aws-endpoints/staging.json --capabilities CAPABILITY_IAM

cloudformation_aws_endpoints_production:
	aws --profile twitch-feed-aws  --region us-west-2 cloudformation create-stack  --stack-name aws-endpoints-production  --template-body file://./cloudformation/aws-endpoints/aws-endpoints.yaml  --parameters file://./cloudformation/aws-endpoints/production.json --capabilities CAPABILITY_IAM
