VERSION=0.0.1
FILTER=
PARAMS=
# Set debug
ifeq ($(debug),1)
PARAMS+=debug=1
endif

# Set filter pid
ifdef pid
	ifneq ($(pid),0)
		PARAMS+=pid=$(pid)
		FILTER=FILTER_PID=${pid}
	endif
endif

# Set send data
ifeq ($(send),1)
FILTER+= SEND=1
endif

# Set eBPF build kernel
ifdef kernel
	PARAMS+= kernel=${kernel}
endif

ifdef version
	VERSION=${version}
endif

GIT_COMMIT=$(shell git rev-parse --short HEAD)
BUILD_DATE=$(shell date '+%Y-%m-%d-%H:%M:%S')

# Set Version
TARGET_FILE=dist/package_dir/bin/euspace
GO_LD_FLAGS_VERSION=-X github.com/coroot/coroot-node-agent/flags.AgentVersion=${VERSION}
GO_LD_FLAGS_GIT_COMMIT=-X github.com/coroot/coroot-node-agent/flags.GitCommit=${GIT_COMMIT}
GO_LD_FLAGS_BUILD_DATE=-X github.com/coroot/coroot-node-agent/flags.BuildDate=${BUILD_DATE}
GO_LD_FLAGS=-ldflags="${GO_LD_FLAGS_VERSION} ${GO_LD_FLAGS_GIT_COMMIT} ${GO_LD_FLAGS_BUILD_DATE}"

all: c-build go-build

build:
	CGO_ENABLED=1 go build -gcflags="all=-N -l" ${GO_LD_FLAGS} -buildvcs=false -o ${TARGET_FILE}
c:
	docker exec -it 62d0676aa0b7 sh -c 'cd /opt/github/euspace/ebpftracer && make all ${PARAMS}'
c-build: c

go-build:
	docker exec -it 62d0676aa0b7 bash -c 'cd /opt/github/euspace && source ~/.g/env && make build'
go: go-build run

run:
	ssh root@10.211.55.27 'cd /opt/github/euspace && CONFIG_ENDPOINT=10.0.16.250:18080 && TRACES_ENDPOINT=http://10.0.16.250:18080/docp/api/v2/data/receive ${FILTER} ./euspace --listen="0.0.0.0:8123"'

docker-build-c:
	cd /opt/github/euspace/ebpftracer && make all ${PARAMS}
docker-build-go:
	source ~/.g/env; CGO_ENABLED=1 go build -a -gcflags="all=-N -l" ${GO_LD_FLAGS} -buildvcs=false -o ${TARGET_FILE}
docker-build: docker-build-c docker-build-go
	@echo Target file: ${TARGET_FILE}
	@echo Euspaces build success!