#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
export DIR_WHERE_FIRST_CMAKELISTS_DOT_TEXT_IS=buildstartshere

# Set debhelper options
DEB_BUILD_OPTIONS=nocheck

# Get some information about the machine we are building on:
export numberOfProcessorsOnMachine=$(shell nproc)
#export wantedJobs=$(shell echo "1.25*$(numberOfProcessorsOnMachine)" | bc -l)
export wantedJobs=$(numberOfProcessorsOnMachine)
export jobsIget=$(shell echo $(wantedJobs) | awk '{print int($$1+0.5)}')
export TOTAL_DISK = $(shell df -h --total | awk 'END{print $$2}')
export FREEMEM = $(shell free --mega)
export TOTAL_MEM = $(shell free --total --mega | xargs | awk '{print $$19"MB"}')
export TOTAL_RAM = $(shell free --total --mega | xargs | awk '{print $$8"MB"}')
export TOTAL_SWAP = $(shell free --total --mega | xargs | awk '{print $$15"MB"}')



%:
	echo
	echo "----- Entering section % -----"
	echo
	#dh $@ --buildsystem=cmake --builddirectory=$(DIR_WHERE_FIRST_CMAKELISTS_DOT_TEXT_IS) --parallel --max-parallel=$(jobsIget)
	#dh $@ --buildsystem=cmake --parallel --max-parallel=$(jobsIget)
	#dh $@ --buildsystem=cmake
	dh $@
	echo
	echo "----- Exiting section % -----"
	echo



#clean:
#	dh clean --buildsystem=cmake --parallel --max-parallel=4
#	dh_auto_clean -O--buildsystem=cmake -O--parallel -O--max-parallel=4
#	dh_autoreconf_clean -O--buildsystem=cmake -O--parallel -O--max-parallel=4
#	dh_clean -O--buildsystem=cmake -O--parallel -O--max-parallel=4
#	rm -f debian/debhelper-build-stamp
#	rm -rf debian/.debhelper/
#	rm -f -- debian/bcunit.substvars debian/bcunit-dev.substvars debian/libbcunit1.substvars debian/bcunit-doc.substvars debian/files
#	rm -fr -- debian/bcunit/ debian/tmp/ debian/bcunit-dev/ debian/libbcunit1/ debian/bcunit-doc/
#	find . \
#	    \( -path '*/.git' -o -path '*/.svn' -o -path '*/.bzr' -o -path '*/.hg' -o -path '*/CVS' -o -path '*/.pc' -o -path '*/_darcs' \) -prune -o \
#	    \( -type f \( -name '#*#' -o -name '.*~' -o -name '*~' -o -name 'DEADJOE' -o -name '*.orig' -o -name '*.rej' -o -name '*.bak' -o -name '.*.orig' -o -name '.*.rej' -o -name '.SUMS' -o -name 'TAGS' -o \( -path '*/.deps/*' -a -name '*.P' \) \) -print -exec rm -f {} + \) -o \
#	    \( -type d \( -name 'autom4te.cache' -o -name '__pycache__' \) -print -prune -exec rm -rf {} + \)



#override_dh_auto_clean:
#	dh_auto_clean
#	dh_clean



override_dh_auto_configure:
	echo
	echo "--- Start override_dh_auto_configure: ---"
	echo
	#  find /usr/src/packages
	#  -DENABLE_BCUNIT_CURSES=ON \
	#  -DENABLE_STATIC=OFF \
	#  -DENABLE_BCUNIT_DOC=ON
	cmake . \
	  -DCMAKE_INSTALL_PREFIX=/usr
	echo
	echo "--- End override_dh_auto_configure: ---"
	echo
    


override_dh_update_autotools_config:
	echo
	echo "--- Start override_dh_update_autotools_config: ---"
	echo
	echo "Skipping running override_dh_update_autotools_config"
	echo
	echo "--- End override_dh_update_autotools_config: ---"
	echo
    


override_dh_autoreconf:
	echo
	echo "--- Start override_dh_autoreconf: ---"
	echo
	echo "Skipping running dh_autoreconf"
	echo
	echo "--- End override_dh_autoreconf: ---"
	echo



override_dh_auto_build:
	echo
	echo "--- Start override_dh_auto_build: ---"
	echo
	echo "--- Machine data we are building on ---"
	echo "Total disk: $$TOTAL_DISK"
	echo "Output of free -h:"
	echo "$$FREEMEM" 
	echo "Total memory: $$TOTAL_MEM"
	echo "Total RAM: $$TOTAL_RAM"
	echo "Total SWAP: $$TOTAL_SWAP"
	echo "numberOfProcessorsOnMachine=$$numberOfProcessorsOnMachine"
	echo "wantedJobs=$$wantedJobs"
	echo "jobsIget=$$jobsIget"
	echo "--- End machine data we are building on ---"
	echo
	echo "--- Show files ---"
	find /usr/src/packages
	echo "--- End show files ---"
	echo
	cmake --build .
	echo
	echo "--- End override_dh_auto_build: ---"
	echo
    


override_dh_auto_test:
	echo
	echo "--- Start override_dh_auto_test: ---"
	echo
	echo "Skipping running dh_auto_test"
	echo
	echo "--- End override_dh_auto_test: ---"
	echo
    


override_dh_auto_install:
	echo
	echo "--- Start override_dh_auto_install: ---"
	echo
	make -j$(jobsIget) install DESTDIR=/usr/src/packages/BUILD/debian/tmp AM_UPDATE_INFO_DIR=no
	echo
	echo "--- End override_dh_auto_install: ---"
	echo



execute_after_dh_install:
	echo
	echo "--- Start execute_before_dh_auto_install: ---"
	echo
	echo "--- Show files ---"
	find /usr/src/packages/BUILD/debian/tmp
	echo "--- End show files ---"
	echo
	echo "--- End execute_before_dh_auto_install: ---"
	echo