OS := $(shell uname | awk '{print tolower($$0)}')
MACHINE := $(shell uname -m)

# DEV_ROCKS = "busted 2.0.0" "busted-htest 1.0.0" "luacheck 0.26.1" "lua-llthreads2 0.1.6" "http 0.4" "ldoc 1.4.6"
DEV_ROCKS = "busted 2.0.0" "busted-htest 1.0.0" "luacheck 0.26.1" "lua-llthreads2 0.1.6" "ldoc 1.4.6"
WIN_SCRIPTS = "bin/busted" "bin/kong"
BUSTED_ARGS ?= -v
TEST_CMD ?= bin/busted $(BUSTED_ARGS)

ifeq ($(OS), darwin)
OPENSSL_DIR ?= /usr/local/opt/openssl
GRPCURL_OS ?= osx
else
OPENSSL_DIR ?= /home/runner/install-root/openssl
GRPCURL_OS ?= $(OS)
endif

ifeq ($(MACHINE), aarch64)
GRPCURL_MACHINE ?= arm64
else
GRPCURL_MACHINE ?= $(MACHINE)
endif


route-test:
	@./scripts/ci/check-container-environment.sh
	go test -v ./tests/route

plugin-test:
	@./tests/plugin/bin/busted -v -o htest tests/plugin/cases

fmt:
	@lua-format plugins/*/* -i --no-keep-simple-control-block-one-line --no-keep-simple-function-one-line
	@lua-format tests/plugin/cases/*/* -i --no-keep-simple-control-block-one-line --no-keep-simple-function-one-line
	@prettier --write '**/*.{js,jsx,tsx,ts}'
	@black .

install:
	@luarocks make OPENSSL_DIR=$(OPENSSL_DIR) CRYPTO_DIR=$(OPENSSL_DIR)

remove:
	-@luarocks remove kong

dependencies:
	@for rock in $(DEV_ROCKS) ; do \
		if luarocks list --porcelain $$rock | grep -q "installed" ; then \
		echo $$rock already installed, skipping ; \
		else \
		echo $$rock not found, installing via luarocks... ; \
		luarocks install $$rock OPENSSL_DIR=$(OPENSSL_DIR) CRYPTO_DIR=$(OPENSSL_DIR) || exit 1; \
		fi \
	done;

plugin-dev: remove install dependencies
