# emailwiz - Makefile
#
# On the server (needs `make`; or use `sudo sh install.sh && sudo emailwiz …`):
#   sudo make install setup
#   sudo make plan | apply | validate | dns | backup | audit
#   sudo make user NAME=alice
#
# From your workstation over SSH (the server does not need make):
#   make remote-install HOST=admin@mail.example.org
#   make remote-setup   HOST=admin@mail.example.org
#   make remote-plan | remote-apply | remote-validate | remote-dns HOST=…
#   make remote-user    HOST=… NAME=alice
#
# Development:
#   make lint | test | test-migration | test-clean | test-setup | test-certbot | release
#
# SPDX-License-Identifier: GPL-3.0-or-later

SHELL   := /bin/sh
PREFIX  ?= /usr/local
EMAILWIZ ?= $(PREFIX)/sbin/emailwiz
SSH     ?= ssh
HOST    ?=
NAME    ?=
DOMAIN  ?=
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || sed -n 's/^EMAILWIZ_VERSION=//p' emailwiz)
# Use sudo only when not already root.
SUDO    := $(shell [ "$$(id -u)" -eq 0 ] || echo sudo)

SCRIPTS := emailwiz install.sh $(wildcard lib/*.sh) $(wildcard tests/*.sh) templates/certbot-deploy-hook

.DEFAULT_GOAL := help
.PHONY: help install uninstall setup plan apply validate dns backup audit user \
	remote-install remote-setup remote-plan remote-apply remote-validate remote-dns remote-user \
	lint test test-migration test-clean test-setup test-certbot release check-host check-name check-domain

help: ## list targets
	@awk 'BEGIN {FS = ":.*## "} /^[a-z-]+:.*## / {printf "  %-16s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

# ---- on the server ----------------------------------------------------------
install: ## install/upgrade the tool into $(PREFIX) (config untouched)
	$(SUDO) sh ./install.sh $(PREFIX)

uninstall: ## remove the tool (keeps /etc/emailwiz, backups and mail config)
	$(SUDO) rm -f $(PREFIX)/sbin/emailwiz
	$(SUDO) rm -rf $(PREFIX)/lib/emailwiz $(PREFIX)/lib/emailwiz.prev

setup: check-domain install ## install, then run the interactive setup wizard
	$(SUDO) env $(if $(DOMAIN),DOMAIN=$(DOMAIN)) $(EMAILWIZ) setup

plan: ## show pending changes (read-only)
	$(SUDO) $(EMAILWIZ) plan
apply: ## back up, apply, validate, reload
	$(SUDO) $(EMAILWIZ) apply
validate: ## full validation incl. local delivery test
	$(SUDO) $(EMAILWIZ) validate --deliver-test
dns: ## print required DNS records
	$(SUDO) $(EMAILWIZ) dns
backup: ## snapshot config + DKIM key + mailboxes
	$(SUDO) $(EMAILWIZ) backup --with-mail
audit: ## read-only system report
	$(SUDO) $(EMAILWIZ) audit

user: check-name ## create mail account NAME and set its password (prompts)
	$(SUDO) $(EMAILWIZ) user add $(NAME)
	$(SUDO) passwd $(NAME)

# ---- from a workstation -------------------------------------------------------
# The tree is sent with git archive (committed files only) and installed by
# install.sh; later targets call emailwiz directly. -t keeps prompts working.
remote-install: check-host ## copy this checkout to HOST and install it
	git archive --format=tar HEAD | $(SSH) $(HOST) \
		'set -e; t=$$(mktemp -d); trap "rm -rf $$t" EXIT; tar -C $$t -xf -; sudo sh $$t/install.sh $(PREFIX)'

remote-setup: check-host check-domain remote-install ## install on HOST and run the setup wizard there
	$(SSH) -t $(HOST) 'sudo env $(if $(DOMAIN),DOMAIN=$(DOMAIN)) $(EMAILWIZ) setup'
remote-plan: check-host ## plan on HOST
	$(SSH) -t $(HOST) 'sudo $(EMAILWIZ) plan'
remote-apply: check-host ## apply on HOST (asks for confirmation)
	$(SSH) -t $(HOST) 'sudo $(EMAILWIZ) apply'
remote-validate: check-host ## validate on HOST
	$(SSH) -t $(HOST) 'sudo $(EMAILWIZ) validate --deliver-test'
remote-dns: check-host ## DNS records for HOST
	$(SSH) $(HOST) 'sudo $(EMAILWIZ) dns'
remote-user: check-host check-name ## create account NAME on HOST (password prompt)
	$(SSH) -t $(HOST) 'sudo $(EMAILWIZ) user add $(NAME) && sudo passwd $(NAME)'

# ---- development --------------------------------------------------------------
lint: ## shellcheck + sh -n + dash -n
	shellcheck -x -P lib $(SCRIPTS)
	for f in $(SCRIPTS); do sh -n "$$f"; done
	if command -v dash >/dev/null; then for f in emailwiz lib/*.sh; do dash -n "$$f"; done; fi

test: ## all container scenarios (docker)
	sh tests/run.sh all
test-migration: ## migration scenario only
	sh tests/run.sh migration
test-clean: ## clean-install scenario only
	sh tests/run.sh clean
test-setup: ## setup wizard scenario only
	sh tests/run.sh setup
test-certbot: ## certbot issuance scenario only (stubbed certbot)
	sh tests/run.sh certbot

release: lint ## build dist/emailwiz-VERSION.tar.gz (+ .sha256) from a clean tag
	@git diff --quiet HEAD || { echo "working tree is dirty"; exit 1; }
	@git describe --tags --exact-match >/dev/null 2>&1 || { echo "HEAD is not tagged"; exit 1; }
	mkdir -p dist
	git archive --format=tar.gz --prefix=emailwiz-$(VERSION)/ -o dist/emailwiz-$(VERSION).tar.gz HEAD
	cd dist && sha256sum emailwiz-$(VERSION).tar.gz > emailwiz-$(VERSION).tar.gz.sha256
	@echo "dist/emailwiz-$(VERSION).tar.gz"

# ---- argument checks (values end up in shell commands) -------------------------
check-host:
	@[ -n "$(HOST)" ] || { echo "set HOST=user@server"; exit 2; }
	@printf '%s' '$(HOST)' | grep -Eq '^[A-Za-z0-9._@-]+$$' || { echo "invalid HOST"; exit 2; }
check-name:
	@printf '%s' '$(NAME)' | grep -Eq '^[a-z][a-z0-9_-]{0,31}$$' || { echo "set NAME=<user> (a-z, 0-9, _ -)"; exit 2; }
check-domain:
	@[ -z "$(DOMAIN)" ] || printf '%s' '$(DOMAIN)' | grep -Eq '^[A-Za-z0-9.-]+$$' || { echo "invalid DOMAIN"; exit 2; }
