SHELL := /bin/bash

HOST ?= root@optovia
SSH ?= ssh $(HOST)

REMOTE_PROD ?= root@optovia:/srv/git/odoo.git
BRANCH ?= main
MODULES ?= all

# Remote bare repo used to sync addons into the mounted /mnt/extra-addons path
REMOTE_BARE_REPO ?= /srv/git/odoo.git

ODOO_SERVICE_PREFIX ?= optoviaproject-odoo
ODOO_DB_NAME ?= odoo
ODOO_DB_USER ?= odoo
ODOO_DB_PASSWORD ?=
ODOO_DB_HOST ?=
ODOO_DB_PORT ?= 5432

.PHONY: update-modules install-modules update-modules-remote install-modules-remote check-vars

check-vars:
	@if [ -z "$(REMOTE_BARE_REPO)" ]; then echo "REMOTE_BARE_REPO is required"; exit 1; fi

update-modules: check-vars
	GIT_SSH_COMMAND=ssh git push origin $(BRANCH) && GIT_SSH_COMMAND=ssh git push $(REMOTE_PROD) $(BRANCH)
	$(MAKE) update-modules-remote MODULES=$(MODULES) HOST=$(HOST)

update-modules-remote: check-vars
	@container="$$( $(SSH) "docker ps --filter 'name=$(ODOO_SERVICE_PREFIX)' --format '{{.Names}}' | head -1" )"; \
	if [ -z "$$container" ]; then \
		echo "No Odoo container found for prefix: $(ODOO_SERVICE_PREFIX)"; \
		exit 1; \
	fi; \
	addons_dir="$$( $(SSH) "docker inspect $$container --format '{{range .Mounts}}{{if eq .Destination \"/mnt/extra-addons\"}}{{.Source}}{{end}}{{end}}'" )"; \
	if [ -z "$$addons_dir" ]; then \
		echo "No /mnt/extra-addons mount found for $$container"; \
		exit 1; \
	fi; \
	if [ -z "$(ODOO_DB_HOST)" ] || [ -z "$(ODOO_DB_PASSWORD)" ]; then \
		remote_env="$$( $(SSH) "docker inspect $$container --format '{{range .Config.Env}}{{println .}}{{end}}'" )"; \
		if [ -z "$(ODOO_DB_HOST)" ]; then \
			odoo_db_host="$$( echo "$$remote_env" | awk -F= '/^DB_HOST=/{print $$2}' | head -1 )"; \
		else \
			odoo_db_host="$(ODOO_DB_HOST)"; \
		fi; \
		if [ -z "$(ODOO_DB_PASSWORD)" ]; then \
			odoo_db_password="$$( echo "$$remote_env" | awk -F= '/^DB_PASSWORD=/{print $$2}' | head -1 )"; \
		else \
			odoo_db_password="$(ODOO_DB_PASSWORD)"; \
		fi; \
		if [ -z "$$odoo_db_host" ] || [ -z "$$odoo_db_password" ]; then \
			remote_args="$$( $(SSH) "docker exec $$container ps aux" | grep -m1 /usr/bin/odoo )"; \
			[ -z "$$odoo_db_host" ] && odoo_db_host="$$( echo "$$remote_args" | sed -n 's/.*--db_host=\\([^ ]*\\).*/\\1/p' )"; \
			[ -z "$$odoo_db_password" ] && odoo_db_password="$$( echo "$$remote_args" | sed -n 's/.*--db_password=\\([^ ]*\\).*/\\1/p' )"; \
		fi; \
	else \
		odoo_db_host="$(ODOO_DB_HOST)"; \
		odoo_db_password="$(ODOO_DB_PASSWORD)"; \
	fi; \
	if [ -z "$$odoo_db_host" ] || [ -z "$$odoo_db_password" ]; then \
		echo "ODOO_DB_HOST/ODOO_DB_PASSWORD not found in remote container env. Set them explicitly."; \
		exit 1; \
	fi; \
	$(SSH) "git --git-dir=$(REMOTE_BARE_REPO) --work-tree=$$addons_dir checkout -f $(BRANCH)"; \
	$(SSH) "docker exec $$container odoo -d '$(ODOO_DB_NAME)' -r '$(ODOO_DB_USER)' -w '$$odoo_db_password' --db_host='$$odoo_db_host' --db_port='$(ODOO_DB_PORT)' -u '$(MODULES)' --http-port=0 --stop-after-init"; \
	service="$$( $(SSH) "docker service ls --format '{{.Name}}' | grep -m1 '^$(ODOO_SERVICE_PREFIX)'" )"; \
	if [ -z "$$service" ]; then \
		echo "No Odoo service found for prefix: $(ODOO_SERVICE_PREFIX)"; \
		exit 1; \
	fi; \
	$(SSH) "docker service update --force --update-parallelism 1 --update-order stop-first $$service"

install-modules: check-vars
	GIT_SSH_COMMAND=ssh git push origin $(BRANCH) && GIT_SSH_COMMAND=ssh git push $(REMOTE_PROD) $(BRANCH)
	$(MAKE) install-modules-remote MODULES=$(MODULES) HOST=$(HOST)

install-modules-remote: check-vars
	@container="$$( $(SSH) "docker ps --filter 'name=$(ODOO_SERVICE_PREFIX)' --format '{{.Names}}' | head -1" )"; \
	if [ -z "$$container" ]; then \
		echo "No Odoo container found for prefix: $(ODOO_SERVICE_PREFIX)"; \
		exit 1; \
	fi; \
	addons_dir="$$( $(SSH) "docker inspect $$container --format '{{range .Mounts}}{{if eq .Destination \"/mnt/extra-addons\"}}{{.Source}}{{end}}{{end}}'" )"; \
	if [ -z "$$addons_dir" ]; then \
		echo "No /mnt/extra-addons mount found for $$container"; \
		exit 1; \
	fi; \
	if [ -z "$(ODOO_DB_HOST)" ] || [ -z "$(ODOO_DB_PASSWORD)" ]; then \
		remote_env="$$( $(SSH) "docker inspect $$container --format '{{range .Config.Env}}{{println .}}{{end}}'" )"; \
		if [ -z "$(ODOO_DB_HOST)" ]; then \
			odoo_db_host="$$( echo "$$remote_env" | awk -F= '/^DB_HOST=/{print $$2}' | head -1 )"; \
		else \
			odoo_db_host="$(ODOO_DB_HOST)"; \
		fi; \
		if [ -z "$(ODOO_DB_PASSWORD)" ]; then \
			odoo_db_password="$$( echo "$$remote_env" | awk -F= '/^DB_PASSWORD=/{print $$2}' | head -1 )"; \
		else \
			odoo_db_password="$(ODOO_DB_PASSWORD)"; \
		fi; \
		if [ -z "$$odoo_db_host" ] || [ -z "$$odoo_db_password" ]; then \
			remote_args="$$( $(SSH) "docker exec $$container ps aux" | grep -m1 /usr/bin/odoo )"; \
			[ -z "$$odoo_db_host" ] && odoo_db_host="$$( echo "$$remote_args" | sed -n 's/.*--db_host=\\([^ ]*\\).*/\\1/p' )"; \
			[ -z "$$odoo_db_password" ] && odoo_db_password="$$( echo "$$remote_args" | sed -n 's/.*--db_password=\\([^ ]*\\).*/\\1/p' )"; \
		fi; \
	else \
		odoo_db_host="$(ODOO_DB_HOST)"; \
		odoo_db_password="$(ODOO_DB_PASSWORD)"; \
	fi; \
	if [ -z "$$odoo_db_host" ] || [ -z "$$odoo_db_password" ]; then \
		echo "ODOO_DB_HOST/ODOO_DB_PASSWORD not found in remote container env. Set them explicitly."; \
		exit 1; \
	fi; \
	$(SSH) "git --git-dir=$(REMOTE_BARE_REPO) --work-tree=$$addons_dir checkout -f $(BRANCH)"; \
	$(SSH) "docker exec $$container odoo -d '$(ODOO_DB_NAME)' -r '$(ODOO_DB_USER)' -w '$$odoo_db_password' --db_host='$$odoo_db_host' --db_port='$(ODOO_DB_PORT)' -i '$(MODULES)' --http-port=0 --stop-after-init"; \
	service="$$( $(SSH) "docker service ls --format '{{.Name}}' | grep -m1 '^$(ODOO_SERVICE_PREFIX)'" )"; \
	if [ -z "$$service" ]; then \
		echo "No Odoo service found for prefix: $(ODOO_SERVICE_PREFIX)"; \
		exit 1; \
	fi; \
	$(SSH) "docker service update --force --update-parallelism 1 --update-order stop-first $$service"
