# =============================================================================
# Minecraft Podman Deployment - Makefile
# =============================================================================
# Usage:
#   make install      # Install dependencies
#   make deploy       # Deploy Minecraft server
#   make check        # Dry-run (check mode)
#   make debug        # Verbose output
#   make status       # Check server status
#   make backup       # Run manual backup
#   make restore      # Restore from latest backup
#   make logs         # View server logs
#   make stop         # Stop Minecraft server
#   make start        # Start Minecraft server
#   make restart      # Restart Minecraft server
#   make update       # Update Minecraft to latest version
#   make uninstall    # Remove Minecraft deployment (dangerous!)
# =============================================================================

PLAYBOOK := site.yml
LIMIT ?= external-apps
INVENTORY := inventory/hosts

.PHONY: help install deploy check debug status backup restore logs stop start restart update uninstall

help:
	@echo "Minecraft Podman Deployment Makefile"
	@echo ""
	@echo "Usage: make [target]"
	@echo ""
	@echo "Targets:"
	@echo "  install    Install Ansible collections and dependencies"
	@echo "  deploy     Deploy Minecraft server (default: $(LIMIT))"
	@echo "  check      Dry-run deployment (--check mode)"
	@echo "  debug      Deploy with verbose output"
	@echo "  status     Check server status on remote"
	@echo "  backup     Run manual backup on remote"
	@echo "  restore    Restore from latest backup on remote"
	@echo "  logs       View Minecraft server logs"
	@echo "  stop       Stop Minecraft server"
	@echo "  start      Start Minecraft server"
	@echo "  restart    Restart Minecraft server"
	@echo "  update     Update Minecraft to latest version"
	@echo "  uninstall  Remove Minecraft deployment (DANGEROUS)"
	@echo ""
	@echo "Override target host: make deploy LIMIT=my-server"

install:
	@echo "Installing Ansible collections..."
	ansible-galaxy collection install -r requirements.yml

deploy:
	@echo "Deploying Minecraft to $(LIMIT)..."
	ansible-playbook $(PLAYBOOK) --limit $(LIMIT)

check:
	@echo "Dry-run deployment for $(LIMIT)..."
	ansible-playbook $(PLAYBOOK) --limit $(LIMIT) --check

debug:
	@echo "Deploying with debug output for $(LIMIT)..."
	ansible-playbook $(PLAYBOOK) --limit $(LIMIT) -vvv

status:
	@echo "Checking server status on $(LIMIT)..."
	ansible $(LIMIT) -m shell -a 'systemctl status minecraft --no-pager'

backup:
	@echo "Running backup on $(LIMIT)..."
	ansible $(LIMIT) -m shell -a '/opt/minecraft/backup.sh'

restore:
	@echo "Running restore on $(LIMIT)..."
	ansible $(LIMIT) -m shell -a '/opt/minecraft/restore.sh'

logs:
	@echo "Viewing logs for $(LIMIT)..."
	ansible $(LIMIT) -m shell -a 'journalctl -u minecraft -n 100 --no-pager'

stop:
	@echo "Stopping Minecraft server on $(LIMIT)..."
	ansible $(LIMIT) -m systemd -a 'name=minecraft state=stopped'

start:
	@echo "Starting Minecraft server on $(LIMIT)..."
	ansible $(LIMIT) -m systemd -a 'name=minecraft state=started'

restart:
	@echo "Restarting Minecraft server on $(LIMIT)..."
	ansible $(LIMIT) -m systemd -a 'name=minecraft state=restarted'

update:
	@echo "Updating Minecraft on $(LIMIT)..."
	ansible-playbook $(PLAYBOOK) --limit $(LIMIT) --tags update

uninstall:
	@echo "WARNING: This will remove the Minecraft deployment from $(LIMIT)"
	@read -p "Are you sure? Type 'yes' to continue: " CONFIRM; \
	if [ "$$CONFIRM" = "yes" ]; then \
		ansible-playbook $(PLAYBOOK) --limit $(LIMIT) --tags uninstall; \
	else \
		echo "Aborted."; \
	fi
