# default target is "all"
#
# Shorcuts to type less on local machines
# Golang tools (go build,test,install,etc,) should be used directly in production

all::

BUILD=dev

export GOBIN=${PWD}/_libexec
HOSTOS := $(shell go env GOHOSTOS)
HOSTARCH := $(shell go env GOHOSTARCH)
NATIVE_OUTPUT := ${GOBIN}/${HOSTOS}_${HOSTARCH}
LDFLAGS='-X main.GITCOMMIT="${BUILD}"'

# Our swift credentials
export SWIFT_USER=system:root
export SWIFT_KEY=testpass

# All packages, except the vendor directory
all_go_packages := $(shell go list ./... | grep -v vendor)

.PHONY: all

all:: build test

build::
	echo "\nLocal box build shortcuts -  use go tools in production\n\n"

	GOARCH= GOOS= ${GODEP} go install -ldflags ${LDFLAGS} -v ./...
	mkdir -p "${NATIVE_OUTPUT}"
	find ${GOBIN} -maxdepth 1 -type f -exec mv {} "${NATIVE_OUTPUT}" \;
	go build -o ./bin/gotranscoder ./cmd/gotranscoder
	go build -o ./bin/mocktwitchtranscoder ./cmd/mocktwitchtranscoder

docs::
	godoc -html code.justin.tv/video/gotranscoder/cmd/gotranscoder > ./docs/got.html
	godoc -html code.justin.tv/video/gotranscoder/pkg/twitchtranscoder > ./docs/twitchtranscoder.html
	godoc -html code.justin.tv/video/gotranscoder/pkg/config > ./docs/config.html

install::
	go install ${all_go_packages}

test::
	go test ${all_go_packages}

lint::
	gometalinter ${all_go_packages:code.justin.tv/video/gotranscoder/%=%} --tests \
		--deadline 60s \
		--disable-all \
		--enable deadcode \
		--enable goconst \
		--enable gofmt \
		--enable goimports \
		--enable gosimple \
		--enable vetshadow \
		--enable errcheck

cover::
	rm -rf _testresults | true
	mkdir -p _testresults
	echo "mode: set" >> _testresults/cover.out
	for d in ./pkg/*; do \
 		echo $$d ;\
		go test code.justin.tv/video/gotranscoder/$$d  -coverpkg=code.justin.tv/video/gotranscoder/$$d -coverprofile=_testresults/tmp.out >> /dev/null ;\
		cat ./_testresults/tmp.out | (read; cat) >> ./_testresults/cover.out ;\
	done >> /dev/null
	go tool cover -html=_testresults/cover.out >> /dev/null


exec::
	./bin/gotranscoder --gotranscoder_config config/localdev_config.json --channel twitchmedia2  --job_key 0 -target_segment_duration=2  -transcode_profile=2015TranscodeMain --stateless_mode=true

exec_profile::
	./bin/gotranscoder --gotranscoder_config config/localdev_config.json --channel twitchmedia2  --job_key 0 -target_segment_duration=2  -transcode_profile=$(profile) --stateless_mode=true

exec_channel::
	$(eval RTMP_ENDPOINT:=$(shell curl -s -X POST http://digestion.video.justin.tv/v1/DigestionV1/GetChannel -d '{"channel":"$(channel)"}' -H "Content-Type: application/json" | grep -Eo 'rtmp:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}\/app'))
	./bin/gotranscoder --gotranscoder_config config/localdev_config.json --channel $(channel)  --job_key 0 -target_segment_duration=2  -transcode_profile=2015TranscodeMain --stateless_mode=false --rtmp_endpoint=$(RTMP_ENDPOINT)

exec_stateless::
	$(eval RTMP_ENDPOINT:=$(shell curl -s -X POST http://digestion.video.justin.tv/v1/DigestionV1/GetChannel -d '{"channel":"twitchmedia2"}' -H "Content-Type: application/json" | grep -Eo 'rtmp:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}\/app'))
	./bin/gotranscoder --gotranscoder_config config/localdev_config.json --channel twitchmedia2  --job_key 0 -target_segment_duration=2  -transcode_profile=2015TranscodeMain --stateless_mode=true --rtmp_endpoint=$(RTMP_ENDPOINT)

vod_test::
	$(eval RTMP_ENDPOINT:=$(shell curl -s -X POST http://digestion.video.justin.tv/v1/DigestionV1/GetChannel -d '{"channel":"$(channel)"}' -H "Content-Type: application/json" | grep -Eo 'rtmp:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}\/app'))
	./bin/gotranscoder --gotranscoder_config config/localdev_config.json --channel $(channel)  --job_key 0 --target_segment_duration=2  --transcode_profile=2015TranscodeMain --vod_pusher_enabled=true --stateless_mode=true --rtmp_endpoint=$(RTMP_ENDPOINT)

origin_test::
	$(eval RTMP_ENDPOINT:=$(shell curl -s -X POST http://digestion.video.justin.tv/v1/DigestionV1/GetChannel -d '{"channel":"$(channel)"}' -H "Content-Type: application/json" | grep -Eo 'rtmp:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}\/app'))
	./bin/gotranscoder --gotranscoder_config config/localdev_config.json --channel $(channel)  --job_key 0 --target_segment_duration=2  --transcode_profile=2015TranscodeMain --vod_pusher_enabled=true --stateless_mode=true --rtmp_endpoint=$(RTMP_ENDPOINT) --origin_endpoint=http://localhost:9090 --shadow_suffix=_shadow --s3_bucket=twitch-video-aws-origin-dev --disable_vinyl=true

lvs_test::
	$(eval RTMP_ENDPOINT:=$(shell curl -s -X POST http://digestion.video.justin.tv/v1/DigestionV1/GetChannel -d '{"channel":"$(channel)"}' -H "Content-Type: application/json" | grep -Eo 'rtmp:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{1,5}\/app'))
	./bin/gotranscoder --gotranscoder_config config/localdev_config.json --channel $(channel)  --job_key 0 --target_segment_duration=2  --transcode_profile=2015TranscodeMain --vod_pusher_enabled=true --stateless_mode=true --rtmp_endpoint=$(RTMP_ENDPOINT) --sns_endpoint=arn:aws:sns:us-west-2:848744099708:TestLVSStreamNotifications --disable_vinyl=true --s3_bucket=test-lvs-vods --s3_prefix=twitch-lvs-test


run :: all test exec_stateless

fullrun ::build test exec

chanrun :: all test exec_channel
