#!/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-*)
REFTRACE_DIR=$(shell ls -d ns-*-ref-traces)
NSC_DIR=$(shell ls -d nsc-*)

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)
CPU_CORES=$(shell nproc)
BUILD_OPTION_OPTIPNG=$(shell echo "-j$(CPU_CORES)")

%:
	dh $@ --with python2

build-indep: build-doc-stamp build-doxygen-stamp optimize-doc-size-stamp

optimize-doc-size-stamp: build-doc-stamp build-doxygen-stamp
	sh debian/duplicate-files.sh ns-3.*/doc/html
	make $(BUILD_OPTION_OPTIPNG) -f debian/optipng.mk
	touch $@

build-doc-stamp:
	make html man -C ./$(NS3_DIR)/doc/manual/
	make html man -C ./$(NS3_DIR)/doc/models/
	make html man -C ./$(NS3_DIR)/doc/tutorial/
	rm ns-3.*/doc/*/build/*/_static/jquery.js
	rm ns-3.*/doc/*/build/*/_static/underscore.js
	touch $@

build-doxygen-stamp: build-shared
	cd $(NS3_DIR); ./waf --doxygen
	touch $@

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

override_dh_auto_build: build-static build-shared
	sed -i 's/-Wl,-Bdynamic//g' debian/tmp/usr/lib/pkgconfig/*.pc

build-static:
	######	build and install static libraries
	./build.py --disable-netanim --build-options="$(BUILD_OPTION)" -- --prefix=/usr --enable-static --enable-mpi -d release -o build-static
	cd ./$(NS3_DIR);./waf install --destdir=../debian/tmp

build-shared: build-static
	####### 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
	
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_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

override_dh_compress:
	dh_compress -Xusr/share/doc/ns3/doxygen/  -Xusr/share/doc/ns3/manual/  -Xusr/share/doc/ns3/models/  -Xusr/share/doc/ns3/tutorial/

override_dh_builddeb:
	dh_builddeb -- -Zxz

dfsg:
	for i in `find -name waf`;do \
		if [ "$$(stat --format='%s' $$i)" -gt 10000 ];then rm -f $$i; fi;\
	done
	UP_V=$$(ls -d ns-3.*| cut -d '-' -f2); \
	DIR_NAME=$$(pwd |xargs basename);\
	cd ..;\
	tar --exclude-vcs --exclude=debian --exclude=.pc -cJf ns3_$${UP_V}+dfsg.orig.tar.xz $${DIR_NAME} 
