PROJECT := github.com/juju/utils/v2

.PHONY: check-licence check-go check

check: check-licence check-go
	go test $(PROJECT)/...

check-licence:
	@(grep -rFl "Licensed under the LGPLv3" .;\
		grep -rFl "MACHINE GENERATED BY THE COMMAND ABOVE; DO NOT EDIT" .;\
		grep -rFl "license that can be found in the LICENSE.ricochet2200 file" .; \
		find . -name "*.go") | sed -e 's,\./,,' | sort | uniq -u | \
		xargs -I {} echo FAIL: licence missed: {}

check-go:
	$(eval GOFMT := $(strip $(shell gofmt -l .| sed -e "s/^/ /g")))
	@(if [ x$(GOFMT) != x"" ]; then \
		echo go fmt is sad: $(GOFMT); \
		exit 1; \
	fi )
	@(go vet -all -composites=false -copylocks=false .)

# Install packages required to develop in utils and run tests.
install-dependencies: install-snap-dependencies install-mongo-dependencies
	@echo Installing dependencies
	@echo Installing bzr
	@sudo apt install bzr --yes
	@echo Installing zip
	@sudo apt install zip --yes

install-snap-dependencies:
## install-snap-dependencies: Install the supported snap dependencies
	@echo Installing go-1.14 snap
	@sudo snap install go --channel=1.14/stable --classic

install-mongo-dependencies:
## install-mongo-dependencies: Install Mongo and its dependencies
	@echo Adding juju PPA for mongodb
	@sudo apt-add-repository --yes ppa:juju/stable
	@sudo apt-get update
	@echo Installing mongodb
	@sudo apt-get --yes install  \
	$(strip $(DEPENDENCIES)) \
	$(shell apt-cache madison mongodb-server-core juju-mongodb3.2 juju-mongodb mongodb-server | head -1 | cut -d '|' -f1)
