TEST?=$$(go list ./... | grep -v 'vendor')
HOSTNAME=basistech.ru
NAMESPACE=tf
SECONDNAMESPACE=dynamix
NAME=terraform-provider-dynamix
BINDIR = ./bin
ZIPDIR = ./zip
BINARY=${NAME}
WORKPATH= ./examples/terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${SECONDNAMESPACE}/${VERSION}/${OS_ARCH} 
MAINPATH = ./cmd/dynamix/
VERSION=1.0.0
OS_ARCH=$(shell go env GOHOSTOS)_$(shell go env GOHOSTARCH)

FILES = ${BINARY}_${VERSION}_darwin_amd64\
		${BINARY}_${VERSION}_darwin_arm64\
		${BINARY}_${VERSION}_freebsd_386\
		${BINARY}_${VERSION}_freebsd_amd64\
		${BINARY}_${VERSION}_freebsd_arm\
		${BINARY}_${VERSION}_linux_386\
		${BINARY}_${VERSION}_linux_amd64\
		${BINARY}_${VERSION}_linux_arm\
		${BINARY}_${VERSION}_linux_arm64\
		${BINARY}_${VERSION}_openbsd_386\
		${BINARY}_${VERSION}_openbsd_amd64\
		${BINARY}_${VERSION}_solaris_amd64\
		${BINARY}_${VERSION}_windows_386.exe\
		${BINARY}_${VERSION}_windows_amd64.exe\

BINS = $(addprefix bin/, $(FILES))

default: install

image:
	GOOS=linux GOARCH=amd64 go build -o terraform-provider-dynamix ./cmd/basis/
	docker build . -t rudecs/tf:3.2.2
	rm terraform-provider-dynamix

lint:
	golangci-lint run --timeout 600s

st:
	go build -o ${BINARY} ${MAINPATH}
	cp ${BINARY} ${WORKPATH}
	rm ${BINARY}

build:
	go build -o ${BINARY} ${MAINPATH}

release: $(FILES)

$(FILES) : $(BINDIR) $(ZIPDIR) $(BINS)
	zip -r $(ZIPDIR)/$@.zip $(BINDIR)/$@
	zip -rj $(ZIPDIR)/$@.zip scripts/install.bat scripts/install.sh

$(BINDIR):
	mkdir $@

$(ZIPDIR):
	mkdir $@

$(BINS):
	GOOS=darwin GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_darwin_amd64 $(MAINPATH)
	GOOS=darwin GOARCH=arm64 go build -o ./bin/${BINARY}_${VERSION}_darwin_arm64 $(MAINPATH)
	GOOS=freebsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_freebsd_386 $(MAINPATH)
	GOOS=freebsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_freebsd_amd64 $(MAINPATH)
	GOOS=freebsd GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_freebsd_arm $(MAINPATH)
	GOOS=linux GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_linux_386 $(MAINPATH)
	GOOS=linux GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_linux_amd64 $(MAINPATH)
	GOOS=linux GOARCH=arm go build -o ./bin/${BINARY}_${VERSION}_linux_arm $(MAINPATH)
	GOOS=linux GOARCH=arm64 go build -o ./bin/${BINARY}_${VERSION}_linux_arm64 ${MAINPATH}
	GOOS=openbsd GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_openbsd_386 $(MAINPATH)
	GOOS=openbsd GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_openbsd_amd64 $(MAINPATH)
	GOOS=solaris GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_solaris_amd64 $(MAINPATH)
	GOOS=windows GOARCH=386 go build -o ./bin/${BINARY}_${VERSION}_windows_386.exe $(MAINPATH)
	GOOS=windows GOARCH=amd64 go build -o ./bin/${BINARY}_${VERSION}_windows_amd64.exe $(MAINPATH)

install: build
	mkdir -p ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${SECONDNAMESPACE}/${VERSION}/${OS_ARCH}
	mv ${BINARY} ~/.terraform.d/plugins/${HOSTNAME}/${NAMESPACE}/${SECONDNAMESPACE}/${VERSION}/${OS_ARCH}

test:
	go test -i $(TEST) || exit 1
	echo $(TEST) | xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4

testacc:
	TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
