# This makefile is setup to work from both a host directly with
# make local
# and with manta with
# make
#
# This causes a bit of oddness in setup as we have multiple entry points and the build
# is actually re-entrant as make on host calls manta which in turn calls manta inside of the
# docker container


all: generate fmt manta_bootstrap

export GOBIN=${GOPATH}/bin

# Remove manta artifacts
manta_cleanup:
	rm -rf .manta/bin
	rm -rf .manta/courier
	rm -rf .manta/config
	rm -rf .manta/country
	rm -rf .manta/experiments
	rm -rf .manta/template
	rm -rf .manta/action

# Kick off a full manta build
manta_bootstrap: manta_cleanup
	manta -v -f .manta.json

# Task run inside of the manta build to do the actual build work
manta: generate vet errcheck test install prune courier save_cmd

# Task run inside of the manta build for running integration tests
manta_integration: go_integration_tests prune

# Run integration tests
integration_tests: manta_cleanup
	manta -v -f .manta_integration.json

# Build the executables we want to distribute
install:
	go install -v -a -tags 'netgo' -ldflags "-X code.justin.tv/common/golibs/bininfo.revision=$$GIT_COMMIT" code.justin.tv/commerce/tachanka/cmd/...

# Remove all build artifacts that we don't care about to minimize distro size
prune:
	find . -mindepth 1 -maxdepth 1 -not -name bin -not -name config -not -name country -not -name experiments -not -name action -not -name template -exec rm -rf {} \;

# Copy files for courier
courier:
	#mkdir -p ./courier && mv ${GOPATH}/bin/restart.sh ./courier/
	mv -f ${GOPATH}/bin/tachanka .

# Save cmd binary files we want deployed
save_cmd:
	mkdir -p ./cmd
	#mv -f ${GOPATH}/bin/message_dumper ./cmd
	#mv -f ${GOPATH}/bin/db_test ./cmd
	#mv -f ${GOPATH}/bin/tuid_report_generator ./cmd
	#mv -f ${GOPATH}/bin/sign_amendment ./cmd

# Run errcheck on all source (Currently on just cmd)
errcheck:
	# godep errcheck ./cmd/...

# Format the code
fmt:
	go fmt code.justin.tv/commerce/tachanka/...

# Vet everything
vet:
	go vet ./...

# If the dynamodb_local folder does not exist, download and unpack
dynamodb_local:
	mkdir -p dynamodb_local
	wget -q http://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest.tar.gz
	tar -C dynamodb_local -xzf dynamodb_local_latest.tar.gz

# Stop all running dynamo local instances and wait for them to shut down
dynamo_cleanup_%:
	pkill -f "^(\/usr\/bin\/)?java.*DynamoDBLocal.jar" ; true
	sleep 1

# Start a dynamo local in-memory instance
dynamo_start: dynamodb_local dynamo_cleanup_1
	java -Djava.library.path=dynamodb_local/DynamoDBLocal_lib -jar dynamodb_local/DynamoDBLocal.jar -port 9001 -inMemory &

# Runs before tests
test_setup: #dynamo_start

# Runs after tests
test_cleanup: dynamo_cleanup_2

# Run go tests
go_test:
	go test -race code.justin.tv/commerce/tachanka/...

# Run go integration tests
go_integration_tests:
	go test -tags=integration code.justin.tv/commerce/tachanka/integration/... -v

install_server:
	go install code.justin.tv/commerce/tachanka/cmd/tachanka/...

install_smoke_test:
	go install code.justin.tv/commerce/tachanka/cmd/smoke_test/...

smoke_tests: install_server install_smoke_test
	echo `killall tachanka`
	${GOPATH}/bin/tachanka &
	sleep 3
	${GOPATH}/bin/smoke_test
	killall tachanka

# Run tests with setup and cleanup
test: test_setup go_test smoke_tests test_cleanup

# Generate Mocks
generate_mocks:
	rm -rf mocks

# Core generate task that all generates flow off of
generate: generate_mocks

# Makes sure all necessary prerequisites are installed locally
ensure_local_dependencies:
	command -v java >/dev/null 2>&1 || { echo >&2 "java required, but is not configured on your path. Please configure java and rerun."; exit 1; }
	command -v wget >/dev/null 2>&1 || { echo >&2 "wget required, but it is not installed. Install with command 'brew install wget'. Please install wget and rerun."; exit 1; }

local_no_mocks: ensure_local_dependencies fmt vet test

# Local build if you want to work locally or have to. Currently only does up to the test loop
local: fmt generate vet test

# Utility commands

local_server: install_server
	${GOPATH}/bin/tachanka -e development

godep_save:
	#godep save code.justin.tv/commerce/tachanka/...
