# **************************************************************************
# * (C)opyright 2026         by Ruben Carlo Benante                        *
# *                                                                        *
# * This program is free software; you can redistribute it and/or modify   *
# *  it under the terms of the GNU General Public License as published by  *
# *  the Free Software Foundation, either version 3 of the License, or     *
# *  (at your option) any later version.                                   *
# *                                                                        *
# * This program is distributed in the hope that it will be useful,        *
# *  but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *  GNU General Public License for more details.                          *
# *                                                                        *
# * You should have received a copy of the GNU General Public License      *
# *  along with this program.  If not, see http://www.gnu.org/licenses/.   *
# *                                                                        *
# * Contact author at:                                                     *
# *  Ruben Carlo Benante                                                   *
# *  rcb@beco.cc                                                           *
# **************************************************************************

# Makefile for logsdu - build the unpacked extension into build/.
#
# Usage:
#   make            # typecheck and bundle into build/
#   make test       # run the unit tests
#   make dist       # build, then zip build/ into logsdu-<version>.zip
#   make clean      # remove build/ and the zip
#
# Dependencies are installed with pnpm, never npm:
#   corepack pnpm install
#
# Load build/ in Firefox: about:debugging -> This Firefox ->
# Load Temporary Add-on -> pick build/manifest.json

EXT_ID := logsdu
VERSION := $(shell node -p "require('./package.json').version")
DIST := $(EXT_ID)-$(VERSION).zip

.PHONY: all build test dist clean check-deps

all: build

# Unit tests for the pure logic: the input formatters and the decision that
# says whether a page load may press "Entrar". Run straight through Node's
# built-in runner and type stripping, so there is no test framework to install.
test:
	node --test "src/**/*.test.ts"

# Call the local toolchain directly, so this works regardless of how pnpm is
# provided (corepack vs standalone). Run "corepack pnpm install" first.
build: check-deps
	node_modules/.bin/tsc -noEmit -skipLibCheck
	node esbuild.config.mjs production

# A plain zip of build/ is exactly what AMO wants for signing.
dist: build
	rm -f $(DIST)
	cd build && zip -qr ../$(DIST) .
	@echo "Package: $(DIST)"
	@echo "Upload it to addons.mozilla.org as an unlisted add-on to get a signed .xpi."

clean:
	rm -rf build
	rm -f $(EXT_ID)-*.zip

# Fail with a useful message rather than a confusing "tsc: not found".
check-deps:
	@test -x node_modules/.bin/tsc || { \
		echo "ERROR: dependencies are not installed."; \
		echo "Run: corepack pnpm install   (do NOT use npm install in this tree)"; \
		exit 1; \
	}
