.PHONY: build build-bin generate clean go-clean bin/server bin/cli bin/seed bin/migrate

# Go build
build: build-bin
build-bin: bin/server

generate: db/entity/autogenerated_entity.go
	@(cd .. && ./hack/gen_mock.sh)
	@(make -C ../idl generate)

# Rebuild when the build tags change
# We set build tags from the variables DBAAS_CLOUD and DBAAS_RUNTIME
ifneq "$(DBAAS_CLOUD)$(DBAAS_RUNTIME)" "$(shell mkdir -p .make && test -f .make/build_tags && cat .make/build_tags || echo new build tags)"
.make/build_tags: go-clean
	@mkdir -p .make
	@echo "saving new build tags $(DBAAS_CLOUD) $(DBAAS_RUNTIME)"
	@echo "$(DBAAS_CLOUD)$(DBAAS_RUNTIME)" > .make/build_tags
endif

clean: go-clean
	rm -f ./build/server/server
	rm -f ./db/entity/autogenerated_entity.go

go-clean:
	go clean

# Binary build
bin/server: .make/build_tags generate
	./scripts/go-build -o build/server/server cmd/server/main.go

# We don't need to compile cli as linux binary
bin/cli: .make/build_tags cmd/cli/main.go
	go build -o bin/dbaas-cli cmd/cli/main.go

bin/seed: .make/build_tags cmd/seed/main.go
	./scripts/go-build -o build/seed/seed cmd/seed/main.go

bin/migrate: .make/build_tags cmd/migrate/main.go
	./scripts/go-build -o build/server/migrate cmd/migrate/main.go

bin/sem-migrate: .make/build_tags cmd/sem-migrate/main.go
	./scripts/go-build -o build/sem-migrate/sem-migrate cmd/sem-migrate/main.go

../tools/bin/goqueryset:
	cd ../tools/ && make bin/goqueryset

GENQS := PATH="../../../tools/bin:$(PATH)" go generate entity.go

db/entity/autogenerated_entity.go: db/entity/entity.go ../tools/bin/goqueryset
	cd db/entity && ($(GENQS) || \
	  (  go build entity.go && $(GENQS) \
	  ))

swagger-combine:
	./scripts/swagger-combine 'swagger-combine ./docs/config.json -o ./docs/mergedSwagger.json --includeDefinitions'
