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

NS3VER=3.12.1
NS3_DIR=ns-$(NS3VER)
PYBINDGENV_DIR=pybindgen-0.15.0.795
REFTRACE_DIR=ns-$(NS3VER)-ref-traces
NSC_DIR=nsc-0.5.2

RELEASE=debug

###check system memory, if it is less than 3GiB, use "-j1" when build
MEMORY=$(shell awk '/MemTotal:/{print $$2}' /proc/meminfo)
BUILD_OPTION=$(shell if [ $(MEMORY) -lt 3090252 ]; then echo "-j1"; fi)

%:
	dh $@ --with python2


override_dh_auto_configure:
	cp ./debian/upstream-back/build.config ./.config
	cd ./debian/waf-*; ./configure
	cp ./debian/waf-*/waf ./$(NS3_DIR)/waf

override_dh_auto_build:
	###############make document
	-make html man -C ./$(NS3_DIR)/doc/manual/
	-make html man -C ./$(NS3_DIR)/doc/models/
	-make html man -C ./$(NS3_DIR)/doc/tutorial/

	cp debian/upstream-back/build-options.py ./build.py
	
	######	build and install static libraries
	./build.py --build-options="$(BUILD_OPTION)" -- --prefix=/usr --enable-static --enable-mpi
	cd ./$(NS3_DIR);./waf install --destdir=../debian/tmp
	
	####### build and install shared libraries, python bindings for default python.
	./build.py --enable-tests --build-options="$(BUILD_OPTION)" -- --prefix=/usr --enable-mpi
	cd ./$(NS3_DIR);./waf install --destdir=../debian/tmp
	
	####### cp the default build.py back
	cp debian/upstream-back/build.py ./build.py
	
	######## build doxygen
	-cd $(NS3_DIR); ./waf --doxygen
	

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_auto_install:


override_dh_install:
	-rm ns-3.*/doc/*/build/*/_static/jquery.js
	-rm ns-3.*/doc/*/build/*/_static/underscore.js
	dh_install

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_strip:
	dh_strip -plibns3-3 --dbg-package=libns3-3-dbg 
	dh_strip -ppython-ns3 --dbg-package=python-ns3-dbg
	dh_strip

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