SHELL := /bin/bash
S3_BUCKET := $(if $(S3_BUILD_ARTIFACTS),$(S3_BUILD_ARTIFACTS),$(shell aws cloudformation describe-stack-resource --stack-name shared-resources --logical-resource-id BuildArtifacts | jq -r '.StackResourceDetail.PhysicalResourceId'))
APP_ENV := $(if $(ENV_NAME),$(ENV_NAME),vhs-staging-app)
NOW=$(shell date +%Y%m%d%H%M%S)

env:
	$(eval LAMBDA := $(shell aws cloudformation describe-stack-resource --stack-name ${APP_ENV}-app --logical-resource-id CreateUpload | jq -r '.StackResourceDetail.PhysicalResourceId'))
	@aws lambda get-function-configuration --function-name ${LAMBDA} | jq -r '.Environment.Variables | to_entries[] | "export "+.key+"="+.value'
	@echo export VHS_URL=`aws cloudformation describe-stacks --stack-name ${APP_ENV}-app | jq -r '.Stacks[0].Outputs[0].OutputValue'`

clean:
	rm -rf build

ensure:
	dep ensure

install: ensure

update:
	dep ensure -update

vet:
	go vet ./...

test:
	go generate ./...
	go test -cover ./...

configs:
	mkdir -p build
	cp -R config/cloudformation build/cloudformation

compile:
	@for basedir in integ src/api; do\
		for dir in $$(find $$basedir -type d -not -path $$basedir); do\
			NAME=$$(basename $$dir);\
			GOOS=linux GOARCH=amd64 go build -o build/serverless/bin/$$NAME $$basedir/$$NAME/$$NAME.go;\
			[[ $$? -ne 0 ]] && break;\
			echo GOOS=linux GOARCH=amd64 go build -o build/serverless/bin/$$NAME $$basedir/$$NAME/$$NAME.go;\
		done;\
	done

build: clean vet test compile configs package

integ: test
	export VHS_ENDPOINT=$$(aws cloudformation describe-stacks --stack-name ${APP_ENV}-app | jq -r '.Stacks[0].Outputs[0].OutputValue');\
	go run integ/test_upload/test_upload.go

integlocal: test

package: configs
	cp config/cloudformation/serverless.yaml build/serverless/
	sed -i.bak \
		-e "s/__S3BUCKET__/${S3_BUCKET}/" \
		-e "s/__S3PREFIX__/${APP_ENV}/" \
		build/serverless/serverless.yaml
	aws s3 cp config/swagger/swagger.yaml s3://${S3_BUCKET}/${APP_ENV}/swagger.yaml
	cd build/serverless && aws cloudformation package \
		--template-file serverless.yaml \
		--s3-bucket ${S3_BUCKET} \
		--s3-prefix ${APP_ENV} \
		--output-template-file transformed.yaml

cfdeploy:
	$(eval STACK := ${APP_ENV}-app)
	aws cloudformation deploy \
		--stack-name ${STACK} \
		--template-file build/serverless/transformed.yaml \
		--parameter-overrides \
			EnvironmentName=${APP_ENV} \
			VideoBucketName=${APP_ENV}.${AWS_REGION}.videos \
			UploadBucketName=${APP_ENV}.${AWS_REGION}.uploads \
			StreamKeySecretsBucketName=${APP_ENV}.${AWS_REGION}.streamkey-secrets \
	--capabilities CAPABILITY_IAM
	echo `aws cloudformation describe-stacks --stack-name ${STACK} | jq -r '.Stacks[0].Outputs[0].OutputValue'`

deploy: build cfdeploy

publish:
	$(eval ARTIFACT_URI := $(shell echo ${CODEBUILD_SOURCE_VERSION} | sed -e "s@arn:aws:s3:::@s3://@"))
	$(eval TARGET_URI := s3://${S3_BUILD_ARTIFACTS}/release/latest.zip)
	aws s3 cp ${ARTIFACT_URI} ${TARGET_URI}

buildstack:
	aws cloudformation create-stack \
		--stack-name ${APP_ENV} \
		--template-body file://config/cloudformation/build.yaml \

