#!/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)

# Reproducible Build wants us to use always the same date when building the documentation
LAST_CHANGE = $(shell dpkg-parsechangelog -S Date)
BUILD_DATE  = $(shell LC_ALL=C date -u "+%B %d, %Y" -d "$(LAST_CHANGE)")
SPHINXOPTS := -D today=\"$(BUILD_DATE)\"
SPHINXOPTS += -D html_last_updated_fmt=\"$(BUILD_DATE)\"

%:
	dh $@ --with python2

build-indep: build-doc-stamp

build-doc-stamp:
	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)"
	rm ns-3.*/doc/*/build/*/_static/jquery.js
	rm ns-3.*/doc/*/build/*/_static/underscore.js
	touch $@

override_dh_auto_build:
	####### build and install shared libraries, python bindings for default python.
	./build.py --disable-netanim --enable-tests --build-options="$(BUILD_OPTION)" -- --prefix=/usr --enable-mpi -d release -o build-shared
	cd ./$(NS3_DIR);./waf install --destdir=../debian/tmp
	rm debian/tmp/usr/lib/libns3*-*-test.so* # delete test of modules but not libns3.${ver}-test.so which seems used
	# cp netanim-*/NetAnim debian/tmp/usr/bin
	# Fixup the installation of PyViz
	mv debian/tmp/usr/lib/python2.7/dist-packages/visualizer/visualizer/* debian/tmp/usr/lib/python2.7/dist-packages/visualizer/
	mv debian/tmp/usr/lib/python2.7/dist-packages/visualizer/src/visualizer/visualizer/* debian/tmp/usr/lib/python2.7/dist-packages/visualizer/
	rmdir debian/tmp/usr/lib/python2.7/dist-packages/visualizer/visualizer
	rm -rf debian/tmp/usr/lib/python2.7/dist-packages/visualizer/src
	mv debian/tmp/usr/lib/python2.7/dist-packages/visualizer/plugins/visualizer/plugins/* debian/tmp/usr/lib/python2.7/dist-packages/visualizer/plugins/
	mv debian/tmp/usr/lib/python2.7/dist-packages/visualizer/plugins/src/visualizer/visualizer/plugins/* debian/tmp/usr/lib/python2.7/dist-packages/visualizer/plugins/
	rm -rf debian/tmp/usr/lib/python2.7/dist-packages/visualizer/plugins/visualizer
	rm -rf debian/tmp/usr/lib/python2.7/dist-packages/visualizer/plugins/src
	# 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:
	dh_compress -Xusr/share/doc/ns3/examples/  -Xusr/share/doc/ns3/manual/  -Xusr/share/doc/ns3/models/  -Xusr/share/doc/ns3/tutorial/

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 
