#!/usr/bin/make -f

# Get version from changelog as well as lsb_release
# to compute unique build version (useful for repo mirroring systems doing file pooling)
PKG_VERSION  := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)
PKG_VERSION_TAG := $(PKG_VERSION)+$(DIST_NAME).$(DIST_RELEASE).$(DIST_VERSION)
SUBSTVARS_BINARY_VERSION := -Vbinary:Version="$(PKG_VERSION_TAG)"
SUBSTVARS_SOURCE_VERSION := -Vsource:Version="$(PKG_VERSION_TAG)"

DESTDIR := $(CURDIR)/debian/mesos
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_BITS := $(shell dpkg-architecture -qDEB_BUILD_ARCH_BITS)

GCC_VERSION := $(shell gcc -dumpversion)
# Bundled libboost won't build with GCC 6 (Debian Stretch)
CPPFLAGS += -Wno-deprecated-declarations
# GCC 8 on Debian Buster needs these ones too
ifeq ($(shell dpkg --compare-versions $(GCC_VERSION) ge 8 && echo yes),yes)
   CPPFLAGS += -Wno-error=parentheses -Wno-error=class-memaccess
endif

# We need a release "version" when building on testing/unstable
ifeq ($(DIST_VERSION),$(filter $(DIST_VERSION),testing unstable))
  PROPER_DIST_VERSION := 10
else
  PROPER_DIST_VERSION := $(DIST_VERSION)
endif

# For Debian Stretch+ workaround XFS isolator build on 32 bits architectures
ifeq ($(DIST_NAME),Debian)
  ifeq ($(shell dpkg --compare-versions $(PROPER_DIST_VERSION) ge 9 && echo yes),yes)
    ifeq ($(DEB_BUILD_BITS),32)
      CPPFLAGS += -D_FILE_OFFSET_BITS=64
    endif
  endif
endif
# Same workaround for Ubuntu 18.04+
ifeq ($(DIST_NAME),Ubuntu)
  ifeq ($(shell dpkg --compare-versions $(PROPER_DIST_VERSION) ge 18.04 && echo yes),yes)
    ifeq ($(DEB_BUILD_BITS),32)
      CPPFLAGS += -D_FILE_OFFSET_BITS=64
    endif
  endif
endif

# On armhf and arm64 we'll use the real Oracle JDK because OpenJDK is too slow :-(
JAVAC_PATH := $(shell realpath /usr/bin/javac)
JAVA_HOME  := $(abspath $(dir $(shell dirname $(JAVAC_PATH))))
export JAVA_HOME

# Supposed to speedup Maven
export MAVEN_OPTS=-client -XX:+TieredCompilation -XX:TieredStopAtLevel=1 -Xverify:none

# Pip crap tries to create things in HOME
ifeq ($(HOME),/nonexistent)
export HOME=$(CURDIR)
endif

%:
	dh $@  --with autotools_dev,autoreconf,python2,javahelper,systemd --parallel

override_dh_auto_clean:
	dh_auto_clean
	rm -f config.log config.lt libtool
	rm -rf "$(CURDIR)/.m2"
	rm -rf "$(CURDIR)/CHANGELOG"

CHANGELOG:
	"$(CURDIR)/debian/mesos-changelog.py"

override_dh_auto_configure: CHANGELOG
	# Can't be built without maven being able to access network
	[ -n "$${PBUILDER_OPERATION}" -a -n "$${USENETWORK}" -a "$${USENETWORK}" = "yes" -o -z "$${PBUILDER_OPERATION}" ] \
	  || (echo && echo "###########################################" \
	  && echo "It seems you're building with cowbuilder/pbuilder without having explicitly enabled networking" \
	  && echo "Please add USENETWORK=yes in your pbuilderrc" \
	  && exit 1)

	# Try to get saner dependencies behavior
	# Cannot use --disable-bundled atm because we're missing too many libs :/
	# Not available:  --with-elfio --with-gmock --with-libprocess --with-nvml --with-picojson --with-stout
	# Disabled on purpose: --with-libev: configure: WARNING: libev found must have been built with -DEV_CHILD_ENABLE=0
	# Disabled on purpose: --with-http-parser: FTFBS, seems to old...
	# Disabled on purpose: --with-boost=/usr build on Jessie, FTBFS on Stretch (1.62 vs 1.55 vs 1.53 bundled)
	# Disabled on purpose: --with-protobuf=/usr: Python module fails with https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=721791
	# Disabled because it's not used: --with-libev, replace by libevent required to enable SSL
	dh_auto_configure -- \
	  --enable-python \
	  --enable-java \
          --disable-bundled-pip \
          --disable-bundled-setuptools \
  	  --disable-bundled-wheel \
	  --disable-python-dependency-install \
	  --with-apr=/usr \
	  --with-curl=/usr \
	  --with-glog=/usr \
 	  --with-leveldb=/usr \
	  --with-sasl=/usr \
	  --with-svn=/usr \
	  --with-zlib=/usr \
	  --with-zookeeper=/usr \
	  --enable-libevent --with-libevent=/usr --with-ssl=/usr \
	  --enable-xfs-disk-isolator \
	  --enable-port-mapping-isolator --with-nl=/usr

override_dh_auto_build:
	# Otherwise fails in cowbuilder with /nonexistent/.m2/repository
	# -T 1C enable multithreading
	# See also MAVEN_OPTS variable to speed up
	dh_auto_build -- MVN="/usr/bin/mvn -X -Duser.home=\"$(CURDIR)\" -T 1C"
	echo "@@@@ LIST OF PYTHON WHEEL FILES BELOW"
	find "$(CURDIR)" -name '*.whl'
	echo "@@@@ COMPUTED PYTHON WHEEL SUFFIX BELOW (CONFIG.LOG)"
	grep "PYTHON_WHL" "$(CURDIR)/config.log"
	# Despite I've patched configure.ac to look for proper WHL files
	# System PIP/WHEEL are still buggy and create incorrect X86_64 files
	# On i386 system. At least on Stretch/Xenial
	# FUCK IT FUCK FUCK FUCK
	# It's insane, there's no way to sort this out
	# And the worse thing, I don't give a shit about this wheel format
	# Let's make it arch free for good
	find "$(CURDIR)" -name '*-linux_*.whl' -exec file-rename -v -f 's/-[^-]+-[^-]+.whl/-none-any.whl/' {} \;

override_dh_auto_install:
	dh_auto_install
	# Mesophere wrapper
	install -m 0755 "$(CURDIR)/debian/mesos-init-wrapper" "$(DESTDIR)/usr/bin/mesos-init-wrapper"
	# Default files (master and slave ones will be installed by dh_installinit)
	mkdir -p "$(DESTDIR)/etc/default"
	install -m 0644 "$(CURDIR)/debian/mesos.mesos.default" "$(DESTDIR)/etc/default/mesos"
	# Default config
	mkdir -p "$(DESTDIR)/etc/mesos"
	mkdir -p "$(DESTDIR)/etc/mesos-master"
	mkdir -p "$(DESTDIR)/etc/mesos-slave"
	mkdir -p "$(DESTDIR)/var/log/mesos"
	mkdir -p "$(DESTDIR)/var/lib/mesos"
	echo "zk://localhost:2181/mesos" > "$(DESTDIR)/etc/mesos/zk"
	echo "/var/lib/mesos" > "$(DESTDIR)/etc/mesos-master/work_dir"
	echo "1" > "$(DESTDIR)/etc/mesos-master/quorum"
	echo "/var/lib/mesos" > "$(DESTDIR)/etc/mesos-slave/work_dir"
	# Shitty symlink to get mesosphere Marathon and Chronos working
	# https://github.com/mesosphere/mesos-deb-packaging/blob/master/build_mesos#L263
	# For example Chronos is started with: vm_opts=(-Djava.library.path=/usr/local/lib:/usr/lib64:/usr/lib
	# 20170614: Both Chronos and Marathon are acting like this...
	mkdir -p "$(DESTDIR)/usr/lib"
	ln -sf "$(DEB_HOST_MULTIARCH)/mesos" "$(DESTDIR)/usr/lib/mesos"
	for lib in $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/*.so; do ln -sf "$(DEB_HOST_MULTIARCH)/`basename $${lib}`" "$(DESTDIR)/usr/lib/`basename $${lib}`"; done
	# Install Java stuff
	mkdir -p "$(DESTDIR)/usr/share/java"
	cp -v "$(CURDIR)/src/java/target/mesos-"*.jar "$(DESTDIR)/usr/share/java"

override_dh_installinit:
	dh_systemd_enable -pmesos --name=mesos-master --no-enable
	dh_systemd_enable -pmesos --name=mesos-slave  --no-enable
	dh_installinit    -pmesos --name=mesos-master --no-start --no-restart-on-upgrade
	dh_installinit    -pmesos --name=mesos-slave  --no-start --no-restart-on-upgrade
	dh_systemd_start  -pmesos --name=mesos-master --no-start --no-restart-on-upgrade
	dh_systemd_start  -pmesos --name=mesos-slave  --no-start --no-restart-on-upgrade

override_dh_systemd_enable:
	echo "Already handled by override_dh_install_init"

override_dh_systemd_start:
	echo "Already handled by override_dh_install_init"

override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)" $(SUBSTVARS_R_DEPENDS) $(SUBSTVARS_BINARY_VERSION) $(SUBSTVARS_SOURCE_VERSION)

override_dh_auto_test:
