#!/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 "")
#TODO: pass deprecation to cmake (probably better not via ccpflags)
EXTRADEFINES := $(shell cat /etc/issue | grep "Ubuntu 1[2345789]\|Ubuntu 20.10\|Ubuntu 21\|Ubuntu 22.10\|Ubuntu 23\|Debian.* [789]" >/dev/null && echo "$$CPPFLAGS -DMEGACMD_DEPRECATED_OS" || echo "")
DPKGXZ := $(shell cat /etc/issue | grep "Ubuntu 24\|Oracular Oriole\|Noble Numbat\|Ubuntu 23\|Mantic Minotaur\|Lunar Lobster\|Ubuntu 22\|Jammy Jellyfish\|Ubuntu 21.10" >/dev/null && echo "-- -Zxz" || echo "")
#LDFLAGS := $(shell dpkg --print-architecture | grep -i "i386\|arm" >/dev/null && echo "" || echo "$$LDFLAGS -Wl,-rpath,/opt/megacmd/lib")
EXPORT_VCPKG_FORCE_SYSTEM_BINARIES := $(shell uname -m | grep "armv7l" >/dev/null && echo "VCPKG_FORCE_SYSTEM_BINARIES=1" || echo "")
DEB_TESTING := $(shell cat /etc/issue | grep "Debian" > /dev/null && cat /etc/apt/sources.list.d/debian.sources | grep -v "^\#" | grep testing > /dev/null && echo "testing")
#TODO: Ensure raspbian builds with that export
EXPORTCCMFPU := $(shell cat /etc/issue | grep "Raspbian GNU/Linux 12" >/dev/null && echo "export CC='cc -mfpu=vfp'" || echo ":")
DISTRO := $(shell lsb_release -ds)
VER := $(shell lsb_release -rs)
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}")
export CXXFLAGS := $(shell [ -n "${EXTRADEFINES}" ] && echo "${EXTRADEFINES} ${CXXFLAGS}" || echo "${CXXFLAGS}")


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

build: build-stamp
build-stamp:
	sed -i -E "s/(^#define MEGACMD_BUILD_ID )[0-9]*/\1$(MEGA_BUILD_ID)/g" src/megacmdversion.h.in
	sed -i -E "0,/megacmd \(([0-9.]*)[^\)]*\)/s//megacmd \(\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 echo "8dc99be7ba94ad6e14256b049e396b40  /opt/cmake.tar.gz" | md5sum -c -; 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 -S $(CURDIR) -B $(CURDIR)/build_dir
	cmake --build $(CURDIR)/build_dir -j$(NJOBS)
	cmake --install $(CURDIR)/build_dir --prefix $(CURDIR)/install_dir

	#generate file that would increase inotify limit:
	echo "fs.inotify.max_user_watches = 524288" > 99-megacmd-inotify-limit.conf

	if [ -n "$(DEB_TESTING)" ]; then                            \
		sed -i "s#@VERSION@#$(DEB_TESTING)#g" debian/postinst;  \
	else                                                        \
		sed -i "s#@VERSION@#$(VER)#g" debian/postinst;          \
	fi
	sed -i "s#@DISTRO@#$(DISTRO)#g" debian/postinst

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

install: build
	###dh_auto_install --destdir=megacmd/megacmd
	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=megacmd-dbg
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info -l$(CURDIR)/deps/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb $(DPKGXZ)

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

