#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CXXFLAGS_MAINT_APPEND = -Wno-format-overflow
export MEGA_EXEC_PATH=$(CURDIR)/debian/tmp/usr/bin/mega-exec
export BUILDDIR = obj-$(DEB_HOST_GNU_TYPE)

# Limit parallel jobs to avoid OOM during C++ compilation.
# The SDK is memory-intensive and kills cc1plus with full parallelism.
%:
	dh $@ --buildsystem=cmake --max-parallel=2

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/ \
		-DCMAKE_INSTALL_BINDIR=usr/bin \
		-DCMAKE_INSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DCMAKE_SKIP_RPATH=ON \
		-DCMAKE_MODULE_PATH=$(CURDIR)/sdk/cmake/modules/packages \
		-DFreeImage_INCLUDE_DIR=/usr/include \
		-DFreeImage_LIBRARY=/usr/lib/$(DEB_HOST_MULTIARCH)/libfreeimage.so \
		-DUSE_PDFIUM=OFF \
		-DUSE_LIBUV=ON \
		-DENABLE_FTP=ON \
		-DWITH_FUSE=ON \
		-DFUSE_INCLUDE_DIR=/usr/include/fuse3 \
		-DFUSE_LIBRARY=/usr/lib/$(DEB_HOST_MULTIARCH)/libfuse3.so \
		-DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON \
		-DCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=ON \
		-DFETCHCONTENT_FULLY_DISCONNECTED=ON \
		-DCMAKE_SKIP_INSTALL_ALL_DEPENDENCY=ON \
		-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=OFF
	cp -a contrib/docs/commands/ contrib/commands.orig/
	cp -a UserGuide.md UserGuide.md.orig

# Tests require connection to external Mega.nz servers
override_dh_auto_test:

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp
	find debian/tmp -type f -exec chmod 644 {} \;
	find debian/tmp -type d -exec chmod 755 {} \;
	chmod 755 debian/tmp/usr/bin/mega-*
	rm -rf debian/tmp/lib
	rm -rf debian/tmp/opt/megacmd/lib

	# SDK Package Files Installation Manual
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/mega_sdklib
	mkdir -p debian/tmp/usr/include/megasdk
	# Files were renamed during installation to avoid overly generic names
	# in order to prevent conflicts with other packages on the system.
	install -D  $(BUILDDIR)/sdk/libSDKlib.a debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libMegaSDKlib.a
	install -D -m 644  $(BUILDDIR)/sdk/sdklib.pc debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/megasdklib.pc
	install -D -m 644  $(BUILDDIR)/sdk/sdklibConfig.cmake debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/mega_sdklib/megasdklibConfig.cmake
	cp -r $(BUILDDIR)/sdk/mega/* debian/tmp/usr/include/megasdk/

execute_after_dh_auto_install-indep:
ifeq ($(filter nodoc,$(DEB_BUILD_OPTIONS)),)
	python3 $(CURDIR)/contrib/docs/generate_command_docs.py
	-pkill -e mega-cmd-server
	cp $(CURDIR)/UserGuide.md $(CURDIR)/contrib/docs/UserGuide.md
	mkdocs build -d $(CURDIR)/debian/html -f $(CURDIR)/mkdocs.yml
	rm -f $(CURDIR)/contrib/docs/UserGuide.md
	rm -f debian/html/sitemap.xml.gz
endif


override_dh_mkdocs:
	dh_mkdocs --theme-package mkdocs-material

execute_after_dh_installdocs:
	rm -rf contrib/docs/commands/
	mv contrib/commands.orig/ contrib/docs/commands/
	mv UserGuide.md.orig UserGuide.md
