#!/usr/bin/make -f
NJOBS := $(shell echo ${DEB_BUILD_OPTIONS} | sed -rn 's/.*parallel=([0-9]*).*/\1/p')
VCPKG_ROOT := $(shell [ -f /opt/vcpkg.tar.gz ] && echo "-DVCPKG_ROOT=vcpkg" || echo "")
DPKGXZ := $(shell cat /etc/issue | grep "Ubuntu 25\|Questing Quokka\|Plucky Puffin\|Ubuntu 24\|Ubuntu 23\|Ubuntu 22" >/dev/null && echo "-- -Zxz" || echo "")
EXPORT_VCPKG_FORCE_SYSTEM_BINARIES := $(shell uname -m | grep "armv7l" >/dev/null && echo "VCPKG_FORCE_SYSTEM_BINARIES=1" || echo "")
export VCPKG_DEFAULT_BINARY_CACHE := $(shell [ -f /opt/vcpkg.tar.gz ] && echo "/opt/persistent/vcpkg_cache" || echo "")
export PATH := $(shell [ -f /opt/cmake.tar.gz ] && echo "`pwd`/cmake_inst/bin:${PATH}" || echo "${PATH}")

MEGA_BUILD_ID := $(shell cat MEGA_BUILD_ID || echo "1")

QTINSTALL := $(shell cat /etc/issue | grep "Ubuntu 20.04" >/dev/null && echo "-DCMAKE_PREFIX_PATH=/opt/mega" || echo "")
QTDEFINES := $(shell [ -f /opt/mega/bin/qmake ] && echo "${QTINSTALL}" || echo "" )

build: build-stamp
build-stamp:
	sed -i -E "0,/megasdk \(([0-9.]*)[^\)]*\)/s//megasdk \(\1-$(MEGA_BUILD_ID).1)/" debian.changelog || :

	if [ -f /opt/vcpkg.tar.gz ]; then           \
		tar xzf /opt/vcpkg.tar.gz;              \
		mkdir -p $(VCPKG_DEFAULT_BINARY_CACHE); \
	fi
	
	if [ -f /opt/cmake.tar.gz ]; then																						\
		HASH=$$(md5sum /opt/cmake.tar.gz | awk '{print $$1}');																\
		if [ "$$HASH" = "ebc26503469f12bf1e956c564fcfa82a" ] || [ "$$HASH" = "2e278820bb4ec6fb71dbef1704fb5359" ]; then		\
			echo "Valid cmake.tar.gz (hash: $$HASH)";																		\
		else																												\
			echo "Invalid cmake.tar.gz hash: $$HASH" >&2;																	\
			exit 1;																											\
		fi;																													\
	fi

	if [ -f /opt/cmake.tar.gz ]; then     \
		tar xzf /opt/cmake.tar.gz;        \
		ln -s cmake-*-linux* cmake_inst;  \
	fi

	cmake --version
	$(EXPORT_VCPKG_FORCE_SYSTEM_BINARIES) cmake $(VCPKG_ROOT) -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DENABLE_QT_BINDINGS=ON -DENABLE_LOG_PERFORMANCE=ON -DUSE_LIBUV=ON $(QTDEFINES) -S $(CURDIR) -B $(CURDIR)/build_dir
	cmake --build $(CURDIR)/build_dir -j$(NJOBS)
	cmake --install $(CURDIR)/build_dir --prefix $(CURDIR)/install_dir
	
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

install: build
	dh_auto_install --destdir=debian/megasdk -- INSTALL_ROOT=$(CURDIR)/debian/megasdk
#	dh_install install_dir/* /
	
# Build architecture-independent files here.
binary-indep: build install
	# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
	dh_installman
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs
	dh_link
	dh_strip --dbg-package=megasdk-dbg
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info
	dh_gencontrol
	dh_md5sums
	dh_builddeb $(DPKGXZ)

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install

