#!/usr/bin/make -f

DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/buildflags.mk

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

NVIM_API_LEVEL = $(shell nvim --headless -u NONE -i NONE -c 'echo api_info().version.api_level -cq 2>&1)

ENABLE_TEST = ON
ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	ENABLE_TEST = OFF
endif

export CFLAGS CXXFLAGS LDFLAGS

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

override_dh_clean:
	env NVIM_API_LEVEL=$(NVIM_API_LEVEL) dh_clean

override_dh_auto_configure:
	python3 bindings/generate_bindings.py nvim src/auto
	dh_auto_configure -- -DUSE_SYSTEM_MSGPACK=ON -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=/usr/bin/python3 -DENABLE_TESTS=$(ENABLE_TEST)

execute_before_dh_auto_install:
# Workaround bug where $DISPLAY is needed for --help
	xvfb-run --auto-servernum help2man -s 1 --version-string=$(DEB_VERSION_UPSTREAM)  -n 'Neovim GUI' --no-info -o debian/nvim-qt.1 build/bin/nvim-qt

execute_after_dh_auto_install:
	rm -f $(CURDIR)/debian/neovim-qt/usr/share/nvim-qt/LICENSE

override_dh_auto_test:
# Need to use a server with 24bit color depth to avoid libegl-mesa
# crashes on some systems
	xvfb-run --server-args '-screen 0 1024x768x24' --auto-servernum dh_auto_test
