#!/usr/bin/make -f

DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

# cmake doesn't honor CPPFLAGS
CFLAGS += $(CPPFLAGS) -DDISABLE_LOG
CXXFLAGS += $(CPPFLAGS)

# Work around #1082125
ifeq (s390x,$(DEB_HOST_ARCH))
  CFLAGS += -O1
endif

export CFLAGS CXXFLAGS LDFLAGS

BITOP_DEPENDS = lua-bitop
PREFER_LUA = ON
UNITTEST =
COMPILE_LUA = ON

ifneq (,$(findstring ok installed,$(shell dpkg-query -W -f '$${Status}' luajit 2>&1)))
  BITOP_DEPENDS =
  PREFER_LUA = OFF
  UNITTEST = unittest
  COMPILE_LUA = OFF
endif

TS_C_VER=0.24.1
TS_LUA_VER=0.4.0
TS_QUERY_VER=0.6.2
TS_VIM_VER=0.7.0
TS_VIMDOC_VER=4.0.0
TS_MARKDOWN_VER=0.5.0
TS_MARKDOWN_INLINE_VER=0.5.0

# Default test timeout upstream is 20 minutes.  Extend this to 45 minutes
TIMEOUT=TEST_TIMEOUT=2700

# Arguments to --filter-out are a Lua pattern, so any of the following
# characters need to be escaped with a % to be treated literally:
#   ^$()%.[]*+-?)

# This test causes hangs in Salsa CI and on the buildds, but I haven't been
# able to reproduce it locally to easily debug.
FUNCTEST_FILTER_OUT_ARGS = --filter-out 'vim%-patch:8%.2%.2586'
# Timing related test, which is too susceptible to flakiness on the buildds
FUNCTEST_FILTER_OUT_ARGS += --filter-out 'parser API supports caching queries'
FUNCTEST_FILTER_OUT_ARGS += --filter-out 'memory usage releases memory when closing windows when folds exist'
# Test fails with "process does not own the terminal"
FUNCTEST_FILTER_OUT_ARGS += --filter-out 'TUI paste: terminal mode'
FUNCTEST_FILTER_OUT_ARGS += --filter-out 'TUI forwards :term palette colors with termguicolors'
# Fail on some architectures - https://github.com/neovim/neovim/issues/32230
FUNCTEST_FILTER_OUT_ARGS += --filter-out commenting
FUNCTEST_FILTER_OUT_ARGS += --filter-out 'treesitter language API'
FUNCTEST_FILTER_OUT_ARGS += --filter-out 'treesitter node API'
FUNCTEST_FILTER_OUT_ARGS += --filter-out 'treesitter foldexpr'
# Fails in Salsa CI but haven't been able to reproduce elsewhere
FUNCTEST_FILTER_OUT_ARGS += --filter-out 'quitting swapfile dialog on startup stops TUI'

UNITTEST_FILTER_OUT_ARGS =

USER = $(shell id -un)
ID = $(shell id -u)
GID = $(shell id -g)

ifeq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
  BUILD = ninja -v -C build
else
  BUILD = ninja -C build
endif

BUILD_TYPE=Release
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
  BUILD_TYPE=Debug
endif

%:
	dh $@ --buildsystem=cmake+ninja --builddirectory=build

override_dh_auto_configure:
	dh_auto_configure -- -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DPREFER_LUA=$(PREFER_LUA) -DLIBLUV_LIBRARY:FILEPATH=/usr/lib/$(DEB_HOST_MULTIARCH)/lua/5.1/luv.so -DLIBLUV_INCLUDE_DIR:PATH=/usr/include/lua5.1 -DCOMPILE_LUA=$(COMPILE_LUA) -DUSE_BUNDLED_BUSTED=OFF -DENABLE_TRANSLATIONS=ON

override_dh_auto_build:
	@echo 'blhc: ignore-line-regexp: .* -E .* -DDO_NOT_DEFINE_EMPTY_ATTRIBUTES .*'
	$(BUILD)
	mkdir -p runtime/parser/
	make -f /usr/src/tree-sitter/c/$(TS_C_VER)/Makefile
	cp debian/.ts-parser-c/c.so runtime/parser/
	make -f /usr/src/tree-sitter/lua/$(TS_LUA_VER)/Makefile
	cp debian/.ts-parser-lua/lua.so runtime/parser/
	make -f /usr/src/tree-sitter/query/$(TS_QUERY_VER)/Makefile
	cp debian/.ts-parser-query/query.so runtime/parser/
	make -f /usr/src/tree-sitter/vim/$(TS_VIM_VER)/Makefile
	cp debian/.ts-parser-vim/vim.so runtime/parser/
	make -f /usr/src/tree-sitter/vimdoc/$(TS_VIMDOC_VER)/Makefile
	cp debian/.ts-parser-vimdoc/vimdoc.so runtime/parser/
	make -f /usr/src/tree-sitter/markdown/$(TS_MARKDOWN_VER)/Makefile
	cp debian/.ts-parser-markdown/markdown.so runtime/parser/
	make -f /usr/src/tree-sitter/markdown_inline/$(TS_MARKDOWN_INLINE_VER)/Makefile
	cp debian/.ts-parser-markdown_inline/markdown_inline.so runtime/parser/

override_dh_auto_install:
	env DESTDIR=$(CURDIR)/debian/tmp $(BUILD) install

override_dh_install-indep:
	dh_install -i -X.so

override_dh_auto_clean:
	$(MAKE) clean
	dh_auto_clean

override_dh_gencontrol:
	dh_gencontrol -- -Vneovim:Depends=$(BITOP_DEPENDS)

debian/passwd:
	printf -- "$(USER):x:$(ID):$(GID):nvim test:$(CURDIR)/debian/fakehome:/bin/false\n" > debian/passwd
	printf -- "users:x:$(GID):\n" > debian/group

debian/fakehome:
	mkdir -p $@

debian/fakeruntime:
	mkdir -p $@
	chmod 0700 $@

debian/fakehome/locale-stamp:
	mkdir -p debian/fakehome/locale
	localedef -f UTF-8 -i en_US debian/fakehome/locale/en_US.UTF-8
	touch $@

override_dh_auto_test-indep:

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
# Force SHELL=/bin/bash so we have a printf that understands \x07
override_dh_auto_test-arch: export SHELL=/bin/bash
override_dh_auto_test-arch: export TEST_COLORS=0
override_dh_auto_test-arch: debian/passwd debian/fakehome debian/fakehome/locale-stamp debian/fakeruntime
	env "LOCPATH=$(CURDIR)/debian/fakehome/locale" "HOME=$(CURDIR)/debian/fakehome" "XDG_RUNTIME_DIR=$(CURDIR)/debian/fakeruntime" $(MAKE) -C test/old/testdir clean
	env "LOCPATH=$(CURDIR)/debian/fakehome/locale" "HOME=$(CURDIR)/debian/fakehome" "XDG_RUNTIME_DIR=$(CURDIR)/debian/fakeruntime" TERM=xterm $(MAKE) -C test/old/testdir "NVIM_PRG=$(CURDIR)/build/bin/nvim"
	env "LOCPATH=$(CURDIR)/debian/fakehome/locale" $(TIMEOUT) BUSTED_ARGS="$(FUNCTEST_FILTER_OUT_ARGS)" TEST_SKIP_FRAGILE=1 "HOME=$(CURDIR)/debian/fakehome" "XDG_RUNTIME_DIR=$(CURDIR)/debian/fakeruntime" LD_PRELOAD=libnss_wrapper.so "NSS_WRAPPER_PASSWD=$(CURDIR)/debian/passwd" "NSS_WRAPPER_GROUP=$(CURDIR)/debian/group" $(BUILD) functionaltest
ifneq (,$(UNITTEST))
	env "LOCPATH=$(CURDIR)/debian/fakehome/locale" BUSTED_ARGS="$(UNITTEST_FILTER_OUT_ARGS)" TEST_SKIP_FRAGILE=1 "HOME=$(CURDIR)/debian/fakehome" "XDG_RUNTIME_DIR=$(CURDIR)/debian/fakeruntime" LD_PRELOAD=libnss_wrapper.so "NSS_WRAPPER_PASSWD=$(CURDIR)/debian/passwd" "NSS_WRAPPER_GROUP=$(CURDIR)/debian/group" $(BUILD) $(UNITTEST)
endif
endif
