SHELL := /bin/bash
# we append the `_tool` directory to our path so that the commands know where to
# look for our tools.
export PATH := $(CURDIR)/_tools/bin:$(PATH)

.PHONY: help docker stopserver server specs manta push extract-strings upload-strings download-strings generate lint manta-assets

help:           ## Show this help.
	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrepgrep | sed -e 's/\\$$//' | sed -e 's/##//'

# currently this should be run before any of the tools in our repo to ensure the
# binary is created and updated.
setup-tools:
	@retool build &> /dev/null

# currently this should be run before any of the tools in our repo to ensure the
# binary is created and updated in vagrant.
vsetup-tools:
	@vagrant ssh -c 'retool build'

stopserver: ## Stop Docker go server
	-@docker ps -q -f ancestor=dev-server | xargs docker kill

vserver: ## Start vagrant go server and tail logs
	@vagrant ssh -c 'python vagrant/env.py' &> /dev/null
	@vagrant ssh -c 'sudo sed -ie "/fs.inotify.max_user_watches/d" /etc/sysctl.conf; echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf' &> /dev/null
	@vagrant ssh -c 'sudo sysctl -p' &> /dev/null
	@touch main.go
	@vagrant ssh -c 'tail -f -n0 /var/log/supervisor/clips.log'

vproxy: ## Start vagrant go server, overwrite ASSETS_PROXY_URL with ngrok proxy URL and tail logs
	@vagrant ssh -c 'python vagrant/env.py ngrok' &> /dev/null
	@touch main.go
	@vagrant ssh -c 'tail -f -n0 /var/log/supervisor/clips.log'

vspecs: vsetup-tools generate ## run ginkgo in vagrant
	vagrant ssh -c 'PATH=$$PWD/_tools/bin:$$PATH ginkgo -r --race'

vwatch: vsetup-tools generate ## run ginkgo in vagrant with file watching for changes
	vagrant ssh -c 'PATH=$$PWD/_tools/bin:$$PATH ginkgo watch -r'

vsetup: ## run the vagrant setup script
	vagrant/setup.sh

generate: setup-tools
	go generate $$(go list ./... | grep -v /vendor/)
	counterfeiter -o ./clientfakes/users-service/fake_usersservice_client.go vendor/code.justin.tv/web/users-service/client Client
	counterfeiter -o ./clientfakes/moneypenny/fake_moneypenny_client.go vendor/code.justin.tv/revenue/moneypenny/client Client
	counterfeiter -o ./clientfakes/aws/fake_aet_client.go vendor/github.com/aws/aws-sdk-go/service/elastictranscoder/elastictranscoderiface ElasticTranscoderAPI
	counterfeiter -o ./clientfakes/aws/fake_sns_client.go vendor/github.com/aws/aws-sdk-go/service/sns/snsiface SNSAPI
	counterfeiter -o ./utilsfakes/slugs/fake_generator.go utils/slugs Generator
	counterfeiter -o ./clientfakes/feeds/fake_feeds_client.go vendor/code.justin.tv/feeds/clients/edge Client
	# remove the _vendor portion of the import to make the fake happy
	# if not on a Mac, swap `-i ""` => `-i`
	sed -i "" "s|code.justin.tv/video/clips-upload/_vendor/||g" ./clientfakes/feeds/fake_feeds_client.go
	sed -i "" "s|code.justin.tv/video/clips-upload/_vendor/||g" ./clientfakes/moneypenny/fake_moneypenny_client.go

manta: ## run manta
	@manta -v -f ./manta/server.json \
		-e AWS_SECRET_KEY \
		-e AWS_ACCESS_KEY \
		-e ENVIRONMENT

prune: docker ## rm all docker containers and remove all docker images that are not tagged
	-@docker rm $(docker ps -a -q)
	@docker rmi --force `docker images | grep "^<none>" | awk "{print $3}"`

lint: setup-tools ## run errcheck and go vet
	errcheck -blank $$(go list ./... | grep -v /vendor/)
	# go vet $$(go list ./... | grep -v /vendor/)

push: lint specs ## run npm tests and push to origin
	npm test
	git push origin head

extract-strings:
	go-i18n extract \
		--html-dir ./templates/... \
		--html-func='{{\s*i18n(WithValues)?\s*"(?P<phrase>[^"\\]*(\\.[^"\\]*)*)"\s+"(?P<context>[^"\\]*(\\.[^"\\]*)*).*?}}' \
		--pkg ./i18n \
		--tmpl ./i18n/extract.tmpl \
		--out="translations.json"

upload-strings: extract-strings
	go-i18n upload \
		--api-key="c8db7e45-64cb-4b51-91e3-9fb39b710610" \
		--project-id="3e0f109a6" \
		--file="translations.json"

download-strings:
	go-i18n compile \
		--file-name translations.json \
		--go-out i18n/translations.go \
		--api-key="c8db7e45-64cb-4b51-91e3-9fb39b710610" \
		--project-id="3e0f109a6" \
		--go-pkg i18n

manta-assets:
	@manta -v -f ./manta/assets.json

worker:
	source=$$(find cmd/worker -name '*.go' -not -name '*test.go'); go run $$source

#generate-api: ## generate the api files
#	go run cmd/routes/*.go -api
#
#routes: ## print out the routes we have available
#	go run cmd/routes/*.go -routes
