#!/usr/bin/make -f
CPPFLAGS := $(shell cat /etc/issue | grep "Ubuntu 1[2357]\|Debian.* 7\|Ubuntu 1[46].10" >/dev/null && echo "$$CPPFLAGS -DMEGACMD_DEPRECATED_OS" || echo "")
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")
FULLREQS := $(shell cat /etc/issue | grep "Ubuntu 12.04" >/dev/null && echo "" || echo '-DREQUIRE_HAVE_FFMPEG -DREQUIRE_HAVE_LIBUV -DREQUIRE_USE_MEDIAINFO -DREQUIRE_USE_PCRE')


build: build-stamp
build-stamp:
	./autogen.sh

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

	CPPFLAGS="$(CPPFLAGS) $(FULLREQS)" ./configure --without-libraw --disable-shared --enable-static --disable-silent-rules \
	  --disable-curl-checks --with-sodium=$(CURDIR)/deps --with-libuv=$(CURDIR)/deps --with-pcre \
	  --with-curl=$(CURDIR)/deps --with-freeimage=$(CURDIR)/deps --with-readline=$(CURDIR)/deps \
	  --with-termcap=$(CURDIR)/deps --prefix=$(CURDIR)/deps --disable-examples $(WMEDIAINFO)

	make

	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
	lsb_release -rs > version	
	dh_install distro /usr/share/doc/megacmd/
	dh_install version /usr/share/doc/megacmd/


# 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
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

