#!/usr/bin/make -f
NJOBS := $(shell echo ${DEB_BUILD_OPTIONS} | sed -rn 's/.*parallel=([0-9]*).*/\1/p')
CPPFLAGS := $(shell cat /etc/issue | grep "Ubuntu 1[2345789]\|Ubuntu 20.10\|Ubuntu 21\|Ubuntu 22.10\|Ubuntu 23.04\|Debian.* [789]" >/dev/null && echo "$$CPPFLAGS -DMEGACMD_DEPRECATED_OS" || echo "")
FCRYPTOPP := $(shell cat /etc/issue | grep "Ubuntu 1[4-8]\|Ubuntu 19.04\|Debian.* [8]" >/dev/null && echo "" || echo "-q")
WCRYPTOPP := $(shell cat /etc/issue | grep "Ubuntu 1[4-8]\|Ubuntu 19.04\|Debian.* [8]" >/dev/null && echo "" || echo "--with-cryptopp=$(CURDIR)/deps")
DPKGXZ := $(shell cat /etc/issue | grep "Ubuntu 24\|Noble Numbat\|Ubuntu 23\|Mantic Minotaur\|Lunar Lobster\|Ubuntu 22\|Jammy Jellyfish\|Ubuntu 21.10" >/dev/null && echo "-- -Zxz" || echo "")
SQLITE3 := $(shell cat /etc/issue | grep "Ubuntu 18.04\|Ubuntu 20.04\|Debian.* \b\([0-9]\|10\)\b" >/dev/null && echo "" || (grep -- -L sdk/contrib/build_sdk.sh >/dev/null && echo "-L") )
FMEDIAINFO := $(shell dpkg -l | grep mediainfo >/dev/null && echo "-i" || echo " ")
WMEDIAINFO := $(shell dpkg -l | grep mediainfo >/dev/null && echo " " || echo "--with-libmediainfo=$(CURDIR)/deps --with-libzen=$(CURDIR)/deps")
RFFMPEG := $(shell cat /etc/issue | grep "Raspbian" >/dev/null && echo "" || echo '-DREQUIRE_HAVE_FFMPEG')
FPDFIUM := $(shell dpkg --print-architecture | grep -i "i386\|arm" >/dev/null && echo "" || echo '--with-pdfium')
RPDFIUM := $(shell dpkg --print-architecture | grep -i "i386\|arm" >/dev/null && echo "" || echo '-DREQUIRE_HAVE_PDFIUM')
INSTALL3RD := $(shell dpkg --print-architecture | grep -i "i386\|arm" >/dev/null && echo "" || echo 'install3rd')
LDFLAGS := $(shell dpkg --print-architecture | grep -i "i386\|arm" >/dev/null && echo "" || echo "$$LDFLAGS -Wl,-rpath,/opt/megacmd/lib")
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")
EXPORTCCMFPU := $(shell cat /etc/issue | grep "Raspbian GNU/Linux 12" >/dev/null && echo "export CC='cc -mfpu=vfp'" || echo ":")

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
	sed -i -E "0,/megacmd \(([0-9.]*)[^\)]*\)/s//megacmd \(\1-$(MEGA_BUILD_ID).1)/" debian.changelog || :

	./autogen.sh

	#build dependencies into folder deps
	mkdir deps || :
	( $(EXPORTCCMFPU) ; bash -x ./sdk/contrib/build_sdk.sh -o archives \
	  -g -b $(FMEDIAINFO) $(FCRYPTOPP) $(SQLITE3) -l -c -s -u -v -a -z -I -p deps/ )

	# Link dynamically with freeimage if pdfium is required.
	if [ -n "$(INSTALL3RD)" ]; then  \
	  ln -sfr $(CURDIR)/deps/lib/libfreeimage*.so $(CURDIR)/deps/lib/libfreeimage.so.3; \
	  ln -sfn libfreeimage.so.3 $(CURDIR)/deps/lib/libfreeimage.so; \
	else \
	  rm $(CURDIR)/deps/lib/libfreeimage.so $(CURDIR)/deps/lib/libfreeimage.so.3 || : ; \
	fi

	CPPFLAGS="$(CPPFLAGS) $(RPDFIUM) $(RFFMPEG) -DREQUIRE_HAVE_LIBUV -DREQUIRE_USE_MEDIAINFO -DREQUIRE_USE_PCRE" LDFLAGS="$(LDFLAGS)" \
	   ./configure --without-libraw --disable-shared --enable-static --disable-silent-rules \
	  --disable-curl-checks --with-sodium=$(CURDIR)/deps --with-libuv=$(CURDIR)/deps --with-pcre $(FPDFIUM) \
	  --with-curl=$(CURDIR)/deps --with-freeimage=$(CURDIR)/deps --with-readline=$(CURDIR)/deps \
	  --with-termcap=$(CURDIR)/deps $(WCRYPTOPP) --prefix=$(CURDIR)/deps --disable-examples $(WMEDIAINFO)

	make -j$(NJOBS)
	echo "fs.inotify.max_user_watches = 524288" > 99-megacmd-inotify-limit.conf
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

install: build
	###dh_auto_install --destdir=megacmd/megacmd
	dh_install src/client/mega-* /usr/bin/
	dh_install mega-cmd /usr/bin/
	dh_install mega-cmd-server /usr/bin/
	dh_install mega-exec /usr/bin/
	dh_install src/client/megacmd_completion.sh /etc/bash_completion.d/
	lsb_release -ds > distro
	if [ -n "$(DEB_TESTING)" ]; then echo $(DEB_TESTING) > version; else lsb_release -rs > version;fi
	dh_install distro /usr/share/doc/megacmd/
	dh_install version /usr/share/doc/megacmd/
	dh_install 99-megacmd-inotify-limit.conf /etc/sysctl.d/

install3rd:
	mkdir -p $(CURDIR)/debian/megacmd/opt/megacmd/lib
	install $(CURDIR)/deps/lib/libfreeimage.so.* $(CURDIR)/debian/megacmd/opt/megacmd/lib

# Build architecture-independent files here.
binary-indep: build install $(INSTALL3RD)
	# 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

