# 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"
	bingo build

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::
	echo "\nThe install directive in this makefile is deprecated, please use bingo build.\n\n"

	go install ${all_go_packages}

test::
	bingo test

lint::
	bingo lint

cover::
	rm -rf _testresults | true
	mkdir -p _testresults
	bingo test --coverage _testresults/cover.out
	go tool cover -html=_testresults/cover.out >> /dev/null


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

exec_profile::
	./bingo-out/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'))
	./bingo-out/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'))
	./bingo-out/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'))
	./bingo-out/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'))
	./bingo-out/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) --use_origin=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'))
	./bingo-out/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
