#!/usr/bin/make -f
export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk

ifeq ($(DEB_HOST_GNU_CPU),mipsel)
  # Drop the debug symbols all together on mipsel to avoid OOM causing FTBFS
  export DEB_CFLAGS_MAINT_STRIP=-g
  export DEB_CXXFLAGS_MAINT_STRIP=-g
endif

# Define CFLAGS and friends to harden the build -- must come any addtion to these variables
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

ifeq ($(DEB_HOST_GNU_CPU),mipsel)
  # Further reduce the memory consumption on mipsel
  LDFLAGS += -Wl,--reduce-memory-overheads -Wl,--no-keep-memory
endif

TOPDIR=$(CURDIR)
NS3_DIR=$(shell ls -d ns-*)
PYBINDGENV_DIR=$(shell ls -d pybindgen-*)

# Build in parallel, unless there is less than 3GiB
MEMORY=$(shell awk '/MemTotal:/{print $$2}' /proc/meminfo)
CPU_CORES=$(shell nproc)
BUILD_OPTION=$(shell if [ $(MEMORY) -lt 3090252 ]; then echo "-j1"; else echo "-j$(CPU_CORES)"; fi)

include /usr/share/dpkg/architecture.mk

# Reproducible Build wants us to use always the same date when building the documentation
include /usr/share/dpkg/pkg-info.mk
SPHINXOPTS := -D today=\"$(SOURCE_DATE_EPOCH)\"
SPHINXOPTS += -D html_last_updated_fmt=\"$(SOURCE_DATE_EPOCH)\"

# Include version information
VERSION=$(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's/\+dfsg.*//' | sed -e 's/\+ds.*//')
MAJOR_VERSION=$(word 1,$(subst ., ,$(VERSION)))
MINOR_VERSION=$(word 2,$(subst ., ,$(VERSION)))
PATCH_VERSION=$(word 3,$(subst ., ,$(VERSION).0)) # Add an extra .0 just in case there is no patch level

BUILD_PROFILE=release

%:
	dh $@ --with python3

# Set up version cache to allow generating version module. The double 0 is due to a CMake peculiarity.

execute_before_dh_auto_configure:
	cd $(NS3_DIR) ; \
	sed -e "s/@NS3_VERSION_CLOSEST_TAG@/NONEXISTENT_TAG__PACKAGE_BUILT_FROM_DEBIAN_SOURCE/" \
	    -e "s/@NS3_VERSION_COMMIT_HASH@//" \
	    -e "s/@NS3_VERSION_DIRTY_FLAG@/1/" \
	    -e "s/@NS3_VERSION_MAJOR@/$(MAJOR_VERSION)/" \
	    -e "s/@NS3_VERSION_MINOR@/$(MINOR_VERSION)/" \
	    -e "s/@NS3_VERSION_PATCH@/$(PATCH_VERSION)/" \
	    -e "s/@NS3_VERSION_RELEASE_CANDIDATE@//" \
	    -e "s/@NS3_VERSION_TAG@/NONEXISTENT_TAG__PACKAGE_BUILT_FROM_DEBIAN_SOURCE/" \
	    -e "s/@NS3_VERSION_TAG_DISTANCE@/00/" \
	    -e "s/@NS3_VERSION_BUILD_PROFILE@/$(BUILD_PROFILE)/" \
	    build-support/version.cache.in > src/core/model/version.cache

# Use the ns3 wrapper over cmake because I fail to get it work otherwise,
# but pass the parameters that cmake needs to build a proper Debian package (copied from the simgrid build logs)

override_dh_auto_configure:
	cd $(NS3_DIR) ; ./ns3 configure --verbose \
		--build-profile=$(BUILD_PROFILE) --enable-python-bindings --enable-logs --enable-examples --enable-tests --disable-mpi --enable-build-version \
                --prefix=/usr -- \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc -DCMAKE_INSTALL_LOCALSTATEDIR=/var -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY=ON -DCMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY=ON \
		-DCMAKE_INSTALL_RUNSTATEDIR=/run -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_INSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) -DCMAKE_INSTALL_PREFIX=/usr

override_dh_auto_build-arch:
	cd $(NS3_DIR) ; ./ns3 build --verbose

override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	echo "XXXX run: SPHINXOPTS='$(SPHINXOPTS)' ./ns3 docs -v sphinx"
	cd $(NS3_DIR) ; export SPHINXOPTS="$(SPHINXOPTS)"; ./ns3 docs -v sphinx
	echo "XXXX done: ./ns3 docs sphinx"
	echo "XXXX run: SPHINXOPTS='$(SPHINXOPTS)' ./ns3 docs -v all "
	cd $(NS3_DIR) ; export SPHINXOPTS="$(SPHINXOPTS)"; ./ns3 docs -v all
	echo "XXXX done: ./ns3 docs all"
	rm -f ns-3.*/doc/*/build/*/_static/jquery.js
	rm -f ns-3.*/doc/*/build/*/_static/underscore.js
else
	for d in manual models tutorial ; do mkdir -p $(NS3_DIR)/doc/$$d/build/html ; touch $(NS3_DIR)/doc/$$d/build/html/index.html ; done
endif
	touch $@

override_dh_auto_install:
	here=$(CURDIR); cd $(NS3_DIR) ; \
		DESTDIR=$${here}/debian/tmp AM_UPDATE_INFO_DIR=no INSTALL="install --strip-program=true" \
		./ns3 install
	
	mkdir -p debian/tmp/usr/bin
	# cp netanim-*/NetAnim debian/tmp/usr/bin
	# Install my own little script as a compiler
	set -e ;\
	all_libs=$$(find debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/ -name 'libns3*.so' | sed -e "s|debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/||" -e 's/.so$$//' | sort) ;\
	sed -e "s/@ALL_LIBS@/$$(echo $$all_libs)/g" debian/ns3++.in > debian/tmp/usr/bin/ns3++
	chmod +x debian/tmp/usr/bin/ns3++
	# Install the upstream changelog as NEWS, as requested by the policy
	(cd ns-3.*; cp RELEASE_NOTES.md NEWS.md)
	
override_dh_auto_test:
	-cd ./$(NS3_DIR); ./test.py -v

override_dh_installchangelogs:
	dh_installchangelogs ./$(NS3_DIR)/CHANGES.md

override_dh_link:
	dh_link
	if test -e debian/ns3-doc/usr/share/doc; then \
		jdupes -rl debian/ns3-doc/usr/share/doc; \
	fi

override_dh_install:
	dh_install
	
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	# The doc is not built when building only for a given arch (it's built in 'arch:all')
	if [ -e debian/ns3-doc/usr/share/doc/ns3/manual/html/_static ] ; then \
	  for where in manual models tutorial ; do \
	    for what in doctools.js language_data.js searchtools.js ; do \
	      rm debian/ns3-doc/usr/share/doc/ns3/$${where}/html/_static/$${what}; \
	      ln -s /usr/share/javascript/sphinxdoc/1.0/$${what} debian/ns3-doc/usr/share/doc/ns3/$${where}/html/_static/$${what}; \
	    done ; \
	  done ; \
	fi
endif

override_dh_auto_clean:
	#-sh ./debian/clean-tree.sh
	rm -f src/core/model/version.cache
	dh_auto_clean

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
override_dh_compress:
	dh_compress -Xusr/share/doc/ns3/examples/  -Xusr/share/doc/ns3/manual/  -Xusr/share/doc/ns3/models/  -Xusr/share/doc/ns3/tutorial/
endif

update-patches:
	cd debian/patches; \
	sed -i 's#ns-3.[0-9]*#$(NS3_DIR)#g' *.diff *.patch
	@echo "Don't forget to update debian/control to rename the lib package,"
	@echo "and the SONAME in the library-soversion.diff patch."
