#!/usr/bin/make -f
export DH_VERBOSE=1
LDFLAGS+=-Wl,--as-needed

TOPDIR=$(shell pwd)
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)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# 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)\"

%:
	dh $@ --with python2

build-indep: build-doc-stamp

build-doc-stamp:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	make html man -C ./$(NS3_DIR)/doc/manual/ SPHINXOPTS="$(SPHINXOPTS)"
	make html man -C ./$(NS3_DIR)/doc/models/ SPHINXOPTS="$(SPHINXOPTS)"
	make html man -C ./$(NS3_DIR)/doc/tutorial/ SPHINXOPTS="$(SPHINXOPTS)"
else
	mkdir -p ./$(NS3_DIR)/doc/manual/build/html
	mkdir -p ./$(NS3_DIR)/doc/models/build/html
	mkdir -p ./$(NS3_DIR)/doc/tutorial/build/html
endif
	rm -f ns-3.*/doc/*/build/*/_static/jquery.js
	rm -f ns-3.*/doc/*/build/*/_static/underscore.js
	touch $@

override_dh_auto_configure:
	cd $(NS3_DIR) ; ./waf configure \
	           --prefix=/usr --enable-mpi -d release -o build-shared \
	           --libexecdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
	           --libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

override_dh_auto_build:
	####### build and install shared libraries, python bindings for default python.
	cd $(NS3_DIR) ; ./waf build $(BUILD_OPTION)

	cd ./$(NS3_DIR); ./waf install --destdir=../debian/tmp
	# delete test of modules but not libns3.${ver}-test.so which seems used
	# MQ: it seems that these test modules does not exist anymore in v3.27
	# MQ: so only remove them if they exist
	ls debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libns3*-*-test.so*|rm -f 
	# cp netanim-*/NetAnim debian/tmp/usr/bin
	# Install my own little script as a compiler
	cp debian/ns3++ debian/tmp/usr/bin
	chmod +x debian/tmp/usr/bin/ns3++
	
override_dh_auto_test:
	-cd ./$(NS3_DIR); ./test.py -v

override_dh_prep:
	#I install files here when build.
	#The upstream cannot work well for us.
	#I will remove this dir when dh_clean, surely.
	dh_prep -Xdebian/tmp
	
override_dh_python2:
	#### upstream now can only build for the default version of python.
	dh_python2 --no-guessing-versions

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

override_dh_auto_clean:
	#-sh ./debian/clean-tree.sh
	dh_auto_clean

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

override_dh_builddeb:
	dh_builddeb -- -Zxz

update-patches:
	cd debian/patches; \
	sed -i 's#ns-3.[0-9]*#$(NS3_DIR)#g' *.diff

get-orig-source:
	uscan --verbose --force-download 
