# CloudPanel — operator and developer shortcuts.
#
# On the server everything runs out of the panel virtualenv at
# /usr/local/CloudPanel/venv. In a development checkout the same targets work
# against ./venv, so PY resolves to whichever exists.

PREFIX      ?= /usr/local/CloudPanel
VENV        ?= $(firstword $(wildcard $(PREFIX)/venv) $(wildcard ./venv) $(PREFIX)/venv)
PY          := $(VENV)/bin/python
PIP         := $(VENV)/bin/pip
MANAGE      := $(PY) manage.py
SYSTEMCTL   ?= systemctl

UNITS := cloudpanel-daemon cloudpanel cloudpanel-terminal cloudpanel-celery cloudpanel-celerybeat

.DEFAULT_GOAL := help
.PHONY: help install migrate makemigrations run test lint collectstatic doctor \
        shell dbshell restart status logs seed backup incremental renewssl monitor \
        syncvhosts quota version check clean dist

help: ## Show this help
	@echo "CloudPanel make targets"
	@echo
	@grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
	  | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-16s\033[0m %s\n", $$1, $$2}'
	@echo
	@echo "Using virtualenv: $(VENV)"

# -- setup -------------------------------------------------------------------
install: ## Install Python dependencies into the virtualenv
	$(PIP) install --upgrade pip setuptools wheel
	$(PIP) install -r requirements.txt

migrate: ## Apply database migrations
	$(MANAGE) migrate --noinput

makemigrations: ## DEVELOPMENT ONLY: generate migrations after a models.py change, then commit them
	@if [ -f /etc/cloudpanel/cloudpanel.conf ]; then \
	  echo "refusing: this is a CloudPanel server. Migrations ship with the release;"; \
	  echo "generating them here creates files no other server has (see docs/INSTALL.md)."; \
	  exit 1; \
	fi
	$(MANAGE) makemigrations

collectstatic: ## Gather static files into STATIC_ROOT
	$(MANAGE) collectstatic --noinput

seed: ## Seed ACLs, the Default package and the first administrator
	$(MANAGE) cloudpanel_seed

# -- running -----------------------------------------------------------------
run: ## Run the development server on :8090
	$(MANAGE) runserver 0.0.0.0:8090

shell: ## Django shell
	$(MANAGE) shell

dbshell: ## MariaDB shell for the panel database
	$(MANAGE) dbshell

# -- quality -----------------------------------------------------------------
test: ## Run the test suite
	$(PY) -m pytest -q

lint: ## Static checks (ruff if present, otherwise compileall)
	@if [ -x "$(VENV)/bin/ruff" ]; then \
	    $(VENV)/bin/ruff check . ; \
	else \
	    echo "ruff not installed; falling back to a syntax check"; \
	    $(PY) -m compileall -q cloudpanel plogical cloudPanelDaemon install \
	      loginSystem websiteFunctions databases dnsManager mailServer \
	      ftpManager sslManager backupManager packages userManagement \
	      serverStatus firewallManager fileManager api ; \
	fi

check: ## Django system checks, including deployment warnings
	$(MANAGE) check --deploy

# -- operations --------------------------------------------------------------
doctor: ## Full health check — services, DB, ports, certs, drift
	$(MANAGE) cloudpanel_doctor

status: ## systemd status for every CloudPanel unit
	@for u in $(UNITS); do \
	    printf '\n=== %s ===\n' "$$u"; \
	    $(SYSTEMCTL) --no-pager --lines=3 status "$$u" || true; \
	done

restart: ## Restart the panel (daemon first — the panel depends on it)
	$(SYSTEMCTL) restart cloudpanel-daemon
	$(SYSTEMCTL) restart cloudpanel
	$(SYSTEMCTL) restart cloudpanel-terminal
	$(SYSTEMCTL) restart cloudpanel-celery cloudpanel-celerybeat
	@echo "restarted: $(UNITS)"

logs: ## Follow the panel and daemon journals
	journalctl -u cloudpanel -u cloudpanel-daemon -f --no-pager

backup: ## Back up every site (same path the systemd timer uses)
	$(MANAGE) cloudpanel_backup --all

incremental: ## Run every due incremental (restic) backup schedule
	$(MANAGE) cloudpanel_incremental

renewssl: ## Renew certificates expiring within 30 days
	$(MANAGE) cloudpanel_renewssl --days 30

monitor: ## Sample resource usage and service state once
	$(MANAGE) cloudpanel_monitor

syncvhosts: ## Reconcile OpenLiteSpeed vhosts against the Websites table
	$(MANAGE) cloudpanel_syncvhosts

quota: ## Recompute per-package disk and bandwidth usage
	$(MANAGE) cloudpanel_quota --all

version: ## Print panel and component versions
	$(MANAGE) cloudpanel_version

dist: ## Build dist/cloudpanel-<version>.tar.gz, its .sha256 and the pinned install.sh (BASE_URL=https://... to bake in the download location)
	python3 packaging/build_dist.py $(if $(BASE_URL),--base-url "$(BASE_URL)")

clean: ## Remove caches and compiled files
	find . -name '__pycache__' -type d -prune -exec rm -rf {} +
	find . -name '*.py[co]' -delete
	rm -rf .pytest_cache .ruff_cache
