# Test Makefile für rsync debugging (Fixed)

.PHONY: test-rsync debug-sync upload restart quick-deploy

# Teste manuellen rsync
test-rsync:
	@echo "🔍 Testing manual rsync..."
	@SERVER_IP=$$(grep ansible_host inventory/hosts.yml | awk '{print $$2}'); \
	echo "Server IP: $$SERVER_IP"; \
	APP_PATH=$$(grep local_app_path inventory/hosts.yml | awk '{print $$2}' | tr -d '"'); \
	echo "Local path: $$APP_PATH"; \
	echo ""; \
	echo "=== Testing dry-run rsync ==="; \
	rsync -av --dry-run \
		--exclude='ansible' \
		--exclude='.git' \
		--exclude='vendor' \
		--exclude='node_modules' \
		--exclude='storage/logs' \
		--exclude='cache' \
		--exclude='logs' \
		--exclude='dist' \
		--exclude='.archive' \
		$$APP_PATH/ root@$$SERVER_IP:/opt/myapp/; \
	echo ""; \
	echo "If this shows files, then rsync should work"

# Debug was in lokalen Dateien ist
debug-local:
	@echo "📁 Local files debug:"
	@APP_PATH=$$(grep local_app_path inventory/hosts.yml | awk '{print $$2}' | tr -d '"'); \
	echo "Path: $$APP_PATH"; \
	echo ""; \
	if [ -z "$$APP_PATH" ]; then \
		echo "❌ APP_PATH is empty!"; \
		echo "Raw line from hosts.yml:"; \
		grep local_app_path inventory/hosts.yml; \
		exit 1; \
	fi; \
	echo "=== Root files ==="; \
	ls -la "$$APP_PATH" | head -10; \
	echo ""; \
	echo "=== Public files ==="; \
	ls -la "$$APP_PATH/public" | head -10; \
	echo ""; \
	echo "=== Does index.php exist locally? ==="; \
	if [ -f "$$APP_PATH/public/index.php" ]; then \
		echo "✅ index.php exists locally"; \
		echo "Size: $$(wc -c < $$APP_PATH/public/index.php) bytes"; \
		echo "Content preview:"; \
		head -5 "$$APP_PATH/public/index.php"; \
	else \
		echo "❌ index.php NOT found locally!"; \
		echo "Checking if public folder exists:"; \
		if [ -d "$$APP_PATH/public" ]; then \
			echo "Public folder exists, contents:"; \
			ls -la "$$APP_PATH/public/"; \
		else \
			echo "Public folder does not exist!"; \
		fi; \
	fi

# Test direkt mit absoluten Pfaden
debug-direct:
	@echo "📁 Direct path test:"
	@echo "=== Current directory ==="
	pwd
	@echo ""
	@echo "=== Going to project root ==="
	cd ../.. && pwd
	@echo ""
	@echo "=== Files in project root ==="
	cd ../.. && ls -la | head -10
	@echo ""
	@echo "=== Public folder ==="
	cd ../.. && ls -la public/ | head -10
	@echo ""
	@echo "=== Index.php check ==="
	cd ../.. && if [ -f "public/index.php" ]; then \
		echo "✅ index.php found!"; \
		echo "Size: $$(wc -c < public/index.php) bytes"; \
	else \
		echo "❌ index.php not found"; \
	fi

# Test Ansible synchronize mit debug
debug-sync:
	@echo "🔍 Testing Ansible synchronize with debug..."
	ansible-playbook -i inventory/hosts.yml debug-sync.yml -v

# Upload files only (no infrastructure setup)
upload:
	@echo "📤 Uploading files only..."
	ansible-playbook -i inventory/hosts.yml upload-only.yml

# Restart application after upload
restart:
	@echo "🔄 Restarting application..."
	ansible-playbook -i inventory/hosts.yml restart-app.yml

# Quick upload and restart
quick-deploy:
	@echo "⚡ Quick deploy: upload + restart..."
	ansible-playbook -i inventory/hosts.yml upload-only.yml
	ansible-playbook -i inventory/hosts.yml restart-app.yml

# Alle Standard-Befehle
deploy:
	@echo "🚀 Deploying project to Netcup..."
	chmod +x deploy.sh
	./deploy.sh

check:
	@echo "🔍 Testing configuration..."
	ansible all -m ping

logs:
	@echo "📋 Showing container logs..."
	ansible all -m shell -a "cd /opt/myapp && (docker compose logs --tail 100 || docker-compose logs --tail 100)"

help:
	@echo "📖 Debug commands:"
	@echo "  make debug-local     - Check local files"
	@echo "  make debug-direct    - Check with direct paths"
	@echo "  make test-rsync      - Test manual rsync"
	@echo "  make debug-sync      - Test Ansible sync"
	@echo ""
	@echo "📖 Deploy commands:"
	@echo "  make deploy          - Full deployment (infrastructure + app)"
	@echo "  make upload          - Upload files only (no infrastructure)"
	@echo "  make restart         - Restart application containers"
	@echo "  make quick-deploy    - Upload files + restart (fastest)"
	@echo ""
	@echo "📖 Utility commands:"
	@echo "  make logs            - Show container logs"
	@echo "  make check           - Test connection"
