SHELL := /bin/bash
export GOSRC := $(GOPATH)/src
export GOBIN := $(PWD)/bin
export PATH := $(GOBIN):$(PATH)

# Default to not using any 3P proxies.
export GOPROXY := direct
export GOPRIVATE := *

# The AWS account the templates are uploaded to.
export AWS_PROFILE := twitch-shoryuken-aws

generate: install_tools
	go generate -x .

install_tools:
	@mkdir -p bin
	go install github.com/golang/protobuf/protoc-gen-go
	go install github.com/twitchtv/twirp/protoc-gen-twirp

build_lambda:
	# Build the Lambda function for the EventBus template.
	GOOS=linux go build -o ./lambda_handler internal/lambda/main.go

package_lambda: build_lambda
	# Build and package the Lambda as a zip.
	zip lambda_handler.zip ./lambda_handler

upload_lambda: package_lambda ada_creds
	# Upload the Lambda to an S3 bucket.
	aws s3api put-object --bucket vod-event-bus-templates --key lambda_handler.zip --body lambda_handler.zip

upload_lambda_template: ada_creds
	# Upload the Lambda template to an S3 bucket.
	aws s3api put-object --bucket vod-event-bus-templates --key lambda_template.yaml --body cloudformation/lambda_template.yaml

ada_creds:
	# Fetch twitch-shoryuken-aws account credentials with ada.
	ada credentials update --profile $(AWS_PROFILE) --account=085436627129 --provider=Isengard --role=Admin --once && aws configure set region us-west-2 --profile $(AWS_PROFILE)

validate_cloudformation: ada_creds
	# Validating AWS CloudFormation templates.
	aws cloudformation validate-template --template-body file://cloudformation/bucket.yaml 1>/dev/null
	aws cloudformation validate-template --template-body file://cloudformation/lambda_template.yaml 1>/dev/null

update_bucket_stack: ada_creds
	# Update the CloudFormation stack that controls the VOD EventBus bucket.
	aws cloudformation deploy --stack-name vod-eventbus-bucket \
		--template-file cloudformation/bucket.yaml
