#!/usr/bin/make -f

# /opt/rstudio-tools is hardcoded
export RSTUDIO_TOOLS_ROOT := $(CURDIR)/debian/_build/opt/rstudio-tools

export RSTUDIO_NODE_VERSION := $(shell awk '/RSTUDIO_NODE_VERSION/ { print $$2 }' cmake/globals.cmake)
export RSTUDIO_INSTALLED_NODE_VERSION := $(shell awk '/RSTUDIO_INSTALLED_NODE_VERSION/ { print $$2 }' cmake/globals.cmake)

%:
	dh ${@} --builddirectory=debian/_build --buildsystem=cmake

execute_after_dh_auto_clean:
	rm -rf debian/_build

execute_before_dh_auto_configure:
	mkdir -p $(CURDIR)/debian/_build
	mkdir -p $(RSTUDIO_TOOLS_ROOT)

	cd dependencies/linux && \
		./install-boost

	cd dependencies/common && \
		./install-soci && \
		./install-dictionaries && \
		./install-mathjax && \
		./install-quarto && \
		npm_config_cache=$(CURDIR)/debian/_build/npm ./install-npm-dependencies && \
		./install-panmirror && \
		./install-gwt

override_dh_auto_configure:
	dh_auto_configure -- \
		-DRSTUDIO_TARGET=Server \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_PREFIX_PATH=$(RSTUDIO_TOOLS_ROOT) \
		-DCMAKE_INSTALL_PREFIX=/usr/lib/rstudio \
		-DFETCHCONTENT_FULLY_DISCONNECTED=OFF \
		-DRSTUDIO_DEPENDENCIES_DIR=$(CURDIR)/dependencies \
		-DRSTUDIO_ENABLE_COPILOT=OFF \
		-DRSTUDIO_TOOLS_ROOT="$(RSTUDIO_TOOLS_ROOT)" \
		-DRSTUDIO_USE_SYSTEM_BOOST:BOOL=OFF \
		-DBOOST_ROOT="$(RSTUDIO_TOOLS_ROOT)" \
		-DSOCI_CORE_LIB=$(wildcard $(RSTUDIO_TOOLS_ROOT)/soci-*/build/lib/libsoci_core.a) \
		-DSOCI_SQLITE_LIB=$(wildcard $(RSTUDIO_TOOLS_ROOT)/soci-*/build/lib/libsoci_sqlite3.a)

override_dh_strip:
	# upstream binary
	dh_strip -Xquarto
