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

TOPDIR=$(shell pwd)
NS3VER=3.14.1
NS3_DIR=ns-$(NS3VER)
PYBINDGENV_DIR=pybindgen-0.15.0.809
REFTRACE_DIR=ns-$(NS3VER)-ref-traces
NSC_DIR=nsc-0.5.3

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)
# force build with -j1
BUILD_OPTION=-j1

%:
	dh $@ --with python2

binary: binary-indep binary-arch

binary-indep: build-doc build-doxygen
	dh binary-indep

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

build-doxygen: build
	cd $(NS3_DIR); ./waf --doxygen

override_dh_auto_configure:
	#cp ./debian/upstream-back/build.config ./.config
	##############################################
	# build and copy waf
	cd debian/waf-1.6; ./configure; cp waf $(TOPDIR)/$(NS3_DIR); make distclean
	cd debian/waf-1.5; ./configure; cp waf $(TOPDIR)/$(PYBINDGENV_DIR); make distclean

override_dh_auto_build:
	######	build and install static libraries
	./build.py --disable-netanim --build-options="$(BUILD_OPTION)" -- --prefix=/usr --enable-static --enable-mpi -d release
	cd ./$(NS3_DIR);./waf install --destdir=../debian/tmp
	
	####### 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
	cd ./$(NS3_DIR);./waf install --destdir=../debian/tmp
	
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:
	# they are installed in auto_build

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
	dh_auto_clean
