.PHONY: all clean norm test test-elisp test-js author author-install author-start author-stop author-stop-legacy author-restart author-status author-logs author-health author-diagnostics help

VENV := .venv
PY := $(VENV)/bin/python
PIP := $(VENV)/bin/pip
OUTPUT_DIR ?= $(if $(SITE_OUTPUT_DIR),$(SITE_OUTPUT_DIR),output)
PERM_DIR := /home/zaine/master-folder/projects/scripts/bash-scripts

AUTHOR_SERVICE_DIR := /home/zaine/master-folder/org-platform/authoring-service

all: set-perms clean-output build search clean-venv 

build:
	@echo "Building project..."
	emacs -Q --script build-site.el

set-perms:
	@echo "Setting permissions for output directory..."
	cd $(PERM_DIR) && sudo ./master-perms.sh

search: $(VENV)
	@echo "Generating search index"
	$(PY) search-index-json.py

test:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" test

test-elisp:
	emacs -Q --batch -l lisp/tests/build-site-tests.el -f ert-run-tests-batch-and-exit

test-js:
	node --test tests/*.test.js

$(VENV):
	@echo "Generating virtual environment"
	python3 -m venv $(VENV)
	$(PIP) install -r requirements.txt

clean-output:
	@echo "Cleaning output directory..."
	rm -rf "$(OUTPUT_DIR)"

clean-venv:
	@echo "Cleaning virtual environment..."
	rm -rf $(VENV)

norm:
	@echo "sorting out the backups..."
	find . -path ./backups -prune -o -type f -name '*~' -exec mv {} backups/ \;

author: author-start

author-install:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" author-install

author-start:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" author-start

author-stop:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" author-stop

author-stop-legacy:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" author-stop-legacy

author-restart:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" author-restart

author-health:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" author-health

author-status:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" author-status

author-diagnostics:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" author-diagnostics

author-logs:
	$(MAKE) -C "$(AUTHOR_SERVICE_DIR)" author-logs

help:
	@echo "Available targets:"
	@echo "  make                 - Full rebuild"
	@echo "  make build           - Rebuild the output directory"
	@echo "  make set-perms       - Set permissions for output directory"
	@echo "  make clean-output    - Remove output directory"
	@echo "  make clean-venv      - Remove venv directory"
	@echo "  make norm            - Move backup files"
	@echo "  make test            - Run authoring server tests"
	@echo "  make test-elisp      - Run Emacs Lisp build tests"
	@echo "  make test-js         - Run browser-model JavaScript tests"
	@echo "  make author          - Install and start the authoring UI service"
	@echo "  make author-install  - Install and enable the authoring UI service"
	@echo "  make author-stop     - Stop the authoring UI service"
	@echo "  make author-restart  - Restart the authoring UI service"
	@echo "  make author-status   - Show authoring UI service status and page count"
	@echo "  make author-diagnostics - Show authoring UI runtime diagnostics"
	@echo "  make author-logs     - Show recent authoring UI service logs"
	@echo "  make search          - Create the search index"
	@echo "  make help            - Show this help message"
