# Makefile for xCloudify Ansible Infrastructure

.PHONY: help install test lint clean package deploy-test

# Default target
help:
	@echo "xCloudify Ansible Infrastructure Management"
	@echo ""
	@echo "Available targets:"
	@echo "  install      - Install dependencies and set up development environment"
	@echo "  test         - Run integration tests"
	@echo "  lint         - Run linting and validation"
	@echo "  clean        - Clean up test artifacts"
	@echo "  package      - Package roles for distribution"
	@echo "  deploy-test  - Deploy test infrastructure"
	@echo "  help         - Show this help message"

# Install dependencies
install:
	@echo "Installing Ansible dependencies..."
	ansible-galaxy install -r requirements.yml
	@echo "Installing Python dependencies..."
	pip install ansible-lint yamllint
	@echo "Setup complete!"

# Run tests
test:
	@echo "Running xCloudify infrastructure tests..."
	@if [ -z "$$TEST_VDC_ID" ]; then \
		echo "ERROR: TEST_VDC_ID environment variable not set"; \
		exit 1; \
	fi
	@if [ -z "$$XCLOUDIFY_BEARER_TOKEN" ]; then \
		echo "ERROR: XCLOUDIFY_BEARER_TOKEN environment variable not set"; \
		exit 1; \
	fi
	ansible-playbook tests/test-infrastructure.yml

# Run linting
lint:
	@echo "Running ansible-lint..."
	ansible-lint roles/
	@echo "Running yamllint..."
	yamllint -d relaxed .
	@echo "Linting complete!"

# Clean up
clean:
	@echo "Cleaning up test artifacts..."
	rm -f ansible.log
	rm -rf .ansible/
	@echo "Cleanup complete!"

# Package roles
package:
	@echo "Packaging xCloudify roles..."
	mkdir -p dist/
	tar -czf dist/xcloudify-infrastructure-$(shell date +%Y%m%d-%H%M%S).tar.gz \
		roles/xcloudify_infrastructure/ \
		examples/ \
		README.md \
		requirements.yml \
		ansible.cfg
	tar -czf dist/xcloudify-admin-$(shell date +%Y%m%d-%H%M%S).tar.gz \
		roles/xcloudify_admin/ \
		README.md \
		requirements.yml \
		ansible.cfg
	@echo "Packages created in dist/"

# Deploy test infrastructure
deploy-test:
	@echo "Deploying test infrastructure..."
	@if [ -z "$$TEST_VDC_ID" ]; then \
		echo "ERROR: TEST_VDC_ID environment variable not set"; \
		exit 1; \
	fi
	ansible-playbook examples/simple-web-app.yml \
		-e "my_vdc_id=$$TEST_VDC_ID" \
		-e "xcloudify_debug=true"

# Quick validation
validate:
	@echo "Validating role structure..."
	@for role in roles/*/; do \
		echo "Checking $$role..."; \
		if [ ! -f "$$role/meta/main.yml" ]; then \
			echo "ERROR: Missing meta/main.yml in $$role"; \
			exit 1; \
		fi; \
		if [ ! -f "$$role/tasks/main.yml" ]; then \
			echo "ERROR: Missing tasks/main.yml in $$role"; \
			exit 1; \
		fi; \
	done
	@echo "Role structure validation complete!"

# Development helpers
dev-setup:
	@echo "Setting up development environment..."
	python -m venv venv
	. venv/bin/activate && pip install ansible ansible-lint yamllint
	@echo "Development environment ready!"
	@echo "Activate with: source venv/bin/activate"

# Galaxy publishing (when ready)
publish:
	@echo "Publishing to Ansible Galaxy..."
	ansible-galaxy role import --api-key $$GALAXY_API_KEY xcloudify ansible-infrastructure
	@echo "Published to Galaxy!"