.PHONY: docker clean publish check-tag create-tag
clean: clean-build

clean-build:
	-rm -fr dist
	-rm -rf build

clean-pyc:
	sudo find . -name '*.pyc' -exec rm -f {} +
	sudo find . -name '*.pyo' -exec rm -f {} +
	sudo find . -name '*~' -exec rm -f {} +
	sudo find . -name '__pycache__' -exec rm -fr {} +


WHL_FOLDER=/data/users/Public/$(USER)/wheels
setup: clean
	uv build --wheel
	rm -rf build

copy:
	$(eval WHL=$(shell ls dist))
	cp dist/${WHL} ${WHL_FOLDER}/${WHL}

build: setup copy

hadoop: build
	$(eval WHL=$(shell ls dist))
	-hadoop fs -rm /tapdata/worldcover/dist/${WHL}
	hadoop fs -copyFromLocal ./dist/${WHL} /tapdata/worldcover/dist/${WHL}

VERSION = $(shell python -c "from lcfm_shapefiles._version import __version__; print(__version__)")

check-tag:
	@echo "Checking if tag v$(VERSION) already exists..."
	@if git tag -l | grep -q "^v$(VERSION)$$"; then \
		echo "❌ Tag v$(VERSION) already exists! Aborting."; \
		echo "   Existing tags:"; \
		git tag -l | grep -E "^v[0-9]+\.[0-9]+\.[0-9]+$$" | sort -V; \
		exit 1; \
	else \
		echo "✅ Tag v$(VERSION) does not exist. Proceeding..."; \
	fi

create-tag:
	@echo "Creating and pushing tag v$(VERSION)..."
	git tag -a v$(VERSION) -m "Release version $(VERSION)"
	git push origin v$(VERSION)
	@echo "✅ Successfully created and pushed tag v$(VERSION)"

publish: check-tag
	@echo "Building and publishing version $(VERSION)..."
	@echo "Running: uv build --wheel"
	uv build --wheel
	@echo "Publishing only the latest wheel file..."
	uv publish --index vito-atf-public dist/*$(VERSION)*.whl
	@if [ $$? -eq 0 ]; then \
		echo "✅ Build and publish successful!"; \
		$(MAKE) create-tag; \
	else \
		echo "❌ Build or publish failed! Not creating tag."; \
		exit 1; \
	fi
	@echo "🎉 Release $(VERSION) completed successfully!"