# Copyright (C) 2024 Free Software Foundation, Inc.
#
# 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/.
#
# Notes:
# ------
#   - To run all t-utils.el based tests:
#      make t-utils-run
#
#   - To run a single t-utils.el based test:
#        make RUN_T_UTILS_TEST=test-NAME
#     For example:
#        make RUN_T_UTILS_TEST=test-matlab-ts-mode-indent
#
#   - To run a single t-utils.el based test with extra flags, add T_UTILS_FLAGS.
#     For example:
#        make T_UTILS_FLAGS="--eval '(setq treesit--indent-verbose t)'" \
#            RUN_T_UTILS_TEST=test-matlab-ts-mode-indent
#

EMACS ?= emacs

ifeq ($(OS),Windows_NT)
    MATLAB_ARCH = win64
    SLIB_EXT = dll
else
    UNAME = /usr/bin/uname
    UNAME_OUT=$(shell $(UNAME))
    UNAME_M_OUT = $(shell $(UNAME) -m)
    ifeq ($(UNAME_OUT),Linux)
        ifeq ($(UNAME_M_OUT),x86_64)
            MATLAB_ARCH = glnxa64
            SLIB_EXT    = so
        endif
    else ifeq ($(UNAME_OUT),Darwin)
        UNAME_M_OUT = $(shell $(UNAME) -m)
        ifeq ($(UNAME_M_OUT),x86_64)
	    MATLAB_ARCH = maci64
            SLIB_EXT    = dylib
        else ifeq ($(UNAME_M_OUT),arm64)
	    MATLAB_ARCH = maca64
            SLIB_EXT    = dylib
        endif
    endif
    ifeq ($(MATLAB_ARCH),)
        $(error "Unhandled system, $(UNAME_OUT) $(UNAME_M_OUT)")
    endif
endif


MATLAB_TS_SLIB = $(lastword $(sort $(wildcard ../matlab-ts-bin/*/$(MATLAB_ARCH)/*.$(SLIB_EXT))))
MATLAB_TS_DIR = $(patsubst %/,%,$(dir $(abspath $(MATLAB_TS_SLIB))))

MATLAB_TS_SETUP = --eval "(setq treesit-extra-load-path '(\"$(MATLAB_TS_DIR)\"))"

EMACSFLAGS = --batch -Q --eval '(setq debug-on-error t)' $(MATLAB_TS_SETUP)

MATLAB_FILES = $(wildcard *.m)

ifneq ($(RUN_T_UTILS_TEST),)
    TEST_TARGETS = $(RUN_T_UTILS_TEST)
else
    TEST_TARGETS = modetests

    # OS is defined on Windows (e.g. OS=Windows_NT) and there's no MATLAB shell tests on windows.
    ifndef OS
        # ../Makefile will set MATLAB_EXE to none if there is no MATLAB
        ifeq ($(MATLAB_EXE),none)
            $(warning warning: matlab not available, skipping M-x matlab-shell tests)
        else
            TEST_TARGETS += shelltests
        endif
    endif
endif

CLEAN_MATLABPATH = env MATLABPATH=

.PHONY: all
all: $(TEST_TARGETS)

.PHONY: modetests
modetests: metest.el
	"$(EMACS)" $(EMACSFLAGS) -l metest.el -e "metest-all-syntax-tests"

.PHONY: shelltests
shelltests: mstest.el
	$(CLEAN_MATLABPATH) "$(EMACS)" $(EMACSFLAGS) -l mstest.el $(MATLAB_PROG_SETUP) -e "mstest-run-all-tests"

T_UTILS_SETUP=-L .. -l ../matlab-autoload.el -L . -l t-utils.el

ifneq ($(RUN_T_UTILS_TEST),)
.PHONY: $(RUN_T_UTILS_TEST)
$(RUN_T_UTILS_TEST):
	"$(EMACS)" $(EMACSFLAGS) $(T_UTILS_SETUP) $(T_UTILS_FLAGS) \
	  -eval '(t-utils-run "$(RUN_T_UTILS_TEST).el")'
endif

.PHONY: t-utils-run
t-utils-run:
	"$(EMACS)" $(EMACSFLAGS) $(T_UTILS_SETUP) $(T_UTILS_FLAGS) -f t-utils-run


.PHONY: disp-matlab-ts-dir
disp-matlab-ts-dir:
	@echo $(MATLAB_TS_DIR)

.PHONY: prof-test
prof-test:
	"$(EMACS)" $(EMACSFLAGS) $(T_UTILS_SETUP) -l t-utils.el -L prof -l $(PROF_TEST_NAME) -f $(PROF_TEST_NAME)

# [EOF] tests/Makefile

# LocalWords:  EMACSFLAGS setq ifneq modetests shelltests metest mstest eof utils treesit ifeq env
# LocalWords:  SLIB UNAME usr uname maci dylib maca lastword patsubst abspath
