LINTBIN := $(shell go env GOPATH)/bin/golangci-lint
BUILD_DATE := $(shell date +'%FT%T%:z')
COMMIT_SHA := $(shell git rev-parse --short=11 HEAD)

build: clobber .bin/client/public .bin/gopherhole

dev: clean .bin/gopherhole
	GOPHERHOLE_UPSTREAM="1.1.1.1:53" \
	GOPHERHOLE_MIN_TTL="60s" \
	.bin/gopherhole -dns-address=:5353 -http-address=:8000

client-dev:
	docker run -it --rm --name='client-dev' \
		--workdir /opt/client \
		-v $$PWD/client:/opt/client \
		--entrypoint ash \
		--user $${UID}:$${GID} \
		--memory=4g \
		-p 5000:5000 \
		node:lts-alpine -c 'npm i && npm run dev'

clean:
	@rm -rf .bin/gopherhole .bin/client

clobber: clean
	@rm -rf .bin ./client/node_modules ./client/public/build

vdhsn/gopherhole:
	docker build \
		--label="org.opencontainers.image.created=$(BUILD_DATE)" \
		--label="org.opencontainers.image.source=https://github.com/adamveld12/gopherhole.git" \
		--label="org.opencontainers.image.url=https://github.com/adamveld12/gopherhole" \
		--label="org.opencontainers.image.revision=$(COMMIT_SHA)" \
		--label="org.opencontainers.image.licenses=MIT" \
		--label="org.opencontainers.image.authors=Adam Veldhousen <adam@vdhsn.com>" \
		-t vdhsn/gopherhole:$(COMMIT_SHA) \
		-t vdhsn/gopherhole:latest .

test:
	dig -p 5353 twitter.com @localhost
	dig -p 5353 google.com @localhost
	dig -p 5353 loki.veldhousen.ninja @localhost
	dig -p 5353 www.liveauctioneers.com @localhost

lint: tidy
# lint: $(LINTBIN) tidy
# 	golangci-lint run -p bugs \
# 					  -p performance \
# 					  -p unused \
# 					  -p complexity \
# 					  --sort-results \
# 					  --max-same-issues 5 \
# 					  --no-config \
# 					  --verbose \
# 					  --timeout 30s \
# 					  --concurrency 4 \
# 					  --issues-exit-code 1

tidy:
	go mod tidy

.PHONY: build clean clobber client-dev dev lint tidy test vdhsn/gopherhole



.bin:
	mkdir -p .bin

.bin/gopherhole: .bin
	@go build --tags "fts5" -v -o .bin/gopherhole .

.bin/client/public: .bin client/public/build
	mkdir -p .bin/client/public
	cp -R ./client/public/ .bin/client/

client/public/build: client/node_modules
	cd ./client && npm run build

client/node_modules:
	cd ./client && npm install

$(LINTBIN):
	echo "installing golangci-lint to $(LINTBIN)..."
	curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin latest
	chmod +x $(LINTBIN)

