#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

INSTALL_PREFIX=/usr/share/mccode
MCCODE_VERSION=3
MCSTAS_VERSION=3.2
MCXTRACE_VERSION=3.1

CMAKE_ARGS=-DCMAKE_INSTALL_PREFIX=$(INSTALL_PREFIX) -DMCCODE_VERSION=$(MCCODE_VERSION) -DMCCODE_USE_LEGACY_DESTINATIONS=OFF -DENABLE_CIF2HKL=OFF
CMAKE_ARGS_N=-Denable_mcstas=1 -DBUILD_MCSTAS=1 $(CMAKE_ARGS)
CMAKE_ARGS_X=-Denable_mcxtrace=1 -DBUILD_MCXTRACE=1 $(CMAKE_ARGS)

PARALLEL=$(shell echo $DEB_BUILD_OPTIONS|xargs -n 1|grep ^parallel=|cut -d= -f2)
ifneq ($(strip $(PARALLEL)),)
	MAKE_ARGS=-j$(PARALLEL)
endif

%:
	dh $@ --with cmake

execute_after_dh_auto_clean:
	rm -f cmake/Modules/debian.cmake

override_dh_auto_configure:

override_dh_auto_build:
	mkdir -p BUILD
	cp cmake/toolchains/deb64.cmake cmake/Modules/debian.cmake
	sed -i -e /amd64/d -e/CMAKE_SYSTEM_NAME/d cmake/Modules/debian.cmake

	mkdir -p BUILD/mcstas
	cd BUILD/mcstas && cmake $(CMAKE_ARGS_N) $(CURDIR)/mcstas && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-comps
	cd BUILD/mcstas-comps && cmake $(CMAKE_ARGS_N) $(CURDIR)/mcstas-comps && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mccodelib
	cd BUILD/mcstas-mccodelib && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mccodelib/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcplot-pyqtgraph
	cd BUILD/mcstas-mcplot-pyqtgraph && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcplot/pyqtgraph/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcplot-matplotlib
	cd BUILD/mcstas-mcplot-matplotlib && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcplot/matplotlib/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcplot-svg
	cd BUILD/mcstas-mcplot-svg && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcplot/svg/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcresplot
	cd BUILD/mcstas-mcresplot && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcresplot/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcrun
	cd BUILD/mcstas-mcrun && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcrun/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcgui
	cd BUILD/mcstas-mcgui && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcgui/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcdisplay-webgl
	cd BUILD/mcstas-mcdisplay-webgl && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcdisplay/webgl/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcdisplay-pyqtgraph
	cd BUILD/mcstas-mcdisplay-pyqtgraph && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcdisplay/pyqtgraph/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcdisplay-mantid
	cd BUILD/mcstas-mcdisplay-mantid && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcdisplay/mantid_xml/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-clusterscripts
	cd BUILD/mcstas-clusterscripts && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/cluster-scripts/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-manuals
	cd BUILD/mcstas-manuals && cmake $(CMAKE_ARGS_N) $(CURDIR)/docpkg/manuals/mcstas/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcstas-mcdoc
	cd BUILD/mcstas-mcdoc && cmake $(CMAKE_ARGS_N) $(CURDIR)/tools/Python/mcdoc/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace
	cd BUILD/mcxtrace && cmake $(CMAKE_ARGS_X) $(CURDIR)/mcxtrace && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-comps
	cd BUILD/mcxtrace-comps && cmake $(CMAKE_ARGS_X) $(CURDIR)/mcxtrace-comps && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-mccodelib
	cd BUILD/mcxtrace-mccodelib && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/Python/mccodelib/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-mxplot-matplotlib
	cd BUILD/mcxtrace-mxplot-matplotlib && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/Python/mcplot/matplotlib/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-mxplot-pyqtgraph
	cd BUILD/mcxtrace-mxplot-pyqtgraph && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/Python/mcplot/pyqtgraph/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-mxplot-svg
	cd BUILD/mcxtrace-mxplot-svg && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/Python/mcplot/svg/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-mxrun
	cd BUILD/mcxtrace-mxrun && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/Python/mcrun/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-mxgui
	cd BUILD/mcxtrace-mxgui && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/Python/mcgui/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-mxdisplay-webgl
	cd BUILD/mcxtrace-mxdisplay-webgl && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/Python/mcdisplay/webgl/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-mxdisplay-pyqtgraph
	cd BUILD/mcxtrace-mxdisplay-pyqtgraph && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/Python/mcdisplay/pyqtgraph/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-clusterscripts
	cd BUILD/mcxtrace-clusterscripts && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/cluster-scripts/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-manuals
	cd BUILD/mcxtrace-manuals && cmake $(CMAKE_ARGS_X) $(CURDIR)/docpkg/manuals/mcxtrace/ && make $(MAKE_ARGS)

	mkdir -p BUILD/mcxtrace-mxdoc
	cd BUILD/mcxtrace-mxdoc && cmake $(CMAKE_ARGS_X) $(CURDIR)/tools/Python/mcdoc/ && make $(MAKE_ARGS)

execute_before_dh_auto_install:
	cd BUILD/mcstas && make install DESTDIR=$(CURDIR)/debian/mcstas
	cd BUILD/mcstas-comps && make install DESTDIR=$(CURDIR)/debian/mcstas-comps
	cd BUILD/mcstas-mccodelib && make install DESTDIR=$(CURDIR)/debian/mcstas-mccodelib
	cd BUILD/mcstas-mcplot-pyqtgraph && make install DESTDIR=$(CURDIR)/debian/mcstas-mcplot-pyqtgraph
	cd BUILD/mcstas-mcplot-matplotlib && make install DESTDIR=$(CURDIR)/debian/mcstas-mcplot-matplotlib
	cd BUILD/mcstas-mcplot-svg && make install DESTDIR=$(CURDIR)/debian/mcstas-mcplot-svg
	cd BUILD/mcstas-mcresplot && make install DESTDIR=$(CURDIR)/debian/mcstas-mcresplot
	cd BUILD/mcstas-mcrun && make install DESTDIR=$(CURDIR)/debian/mcstas-mcrun
	cd BUILD/mcstas-mcgui && make install DESTDIR=$(CURDIR)/debian/mcstas-mcgui
	cd BUILD/mcstas-mcdisplay-webgl && make install DESTDIR=$(CURDIR)/debian/mcstas-mcdisplay-webgl
	cd BUILD/mcstas-mcdisplay-pyqtgraph && make install DESTDIR=$(CURDIR)/debian/mcstas-mcdisplay-pyqtgraph
	cd BUILD/mcstas-mcdisplay-mantid && make install DESTDIR=$(CURDIR)/debian/mcstas-mcdisplay-mantid
	cd BUILD/mcstas-clusterscripts && make install DESTDIR=$(CURDIR)/debian/mcstas-clusterscripts
	cd BUILD/mcstas-manuals && make install DESTDIR=$(CURDIR)/debian/mcstas-manuals
	cd BUILD/mcstas-mcdoc && make install DESTDIR=$(CURDIR)/debian/mcstas-mcdoc
	# cd BUILD/mcstas-ncrystal && make install DESTDIR=$(CURDIR)/debian/mcstas-ncrystal

	cd BUILD/mcxtrace && make install DESTDIR=$(CURDIR)/debian/mcxtrace
	cd BUILD/mcxtrace-comps && make install DESTDIR=$(CURDIR)/debian/mcxtrace-comps
	cd BUILD/mcxtrace-mccodelib && make install DESTDIR=$(CURDIR)/debian/mcxtrace-mccodelib
	cd BUILD/mcxtrace-mxplot-matplotlib && make install DESTDIR=$(CURDIR)/debian/mcxtrace-mxplot-matplotlib
	cd BUILD/mcxtrace-mxplot-pyqtgraph && make install DESTDIR=$(CURDIR)/debian/mcxtrace-mxplot-pyqtgraph
	cd BUILD/mcxtrace-mxplot-svg && make install DESTDIR=$(CURDIR)/debian/mcxtrace-mxplot-svg
	cd BUILD/mcxtrace-mxrun && make install DESTDIR=$(CURDIR)/debian/mcxtrace-mxrun
	cd BUILD/mcxtrace-mxgui && make install DESTDIR=$(CURDIR)/debian/mcxtrace-mxgui
	cd BUILD/mcxtrace-mxdisplay-webgl && make install DESTDIR=$(CURDIR)/debian/mcxtrace-mxdisplay-webgl
	cd BUILD/mcxtrace-mxdisplay-pyqtgraph && make install DESTDIR=$(CURDIR)/debian/mcxtrace-mxdisplay-pyqtgraph
	cd BUILD/mcxtrace-clusterscripts && make install DESTDIR=$(CURDIR)/debian/mcxtrace-clusterscripts
	cd BUILD/mcxtrace-manuals && make install DESTDIR=$(CURDIR)/debian/mcxtrace-manuals
	cd BUILD/mcxtrace-mxdoc && make install DESTDIR=$(CURDIR)/debian/mcxtrace-mxdoc

	# Replace symlinks with the files they point to
	for i in $$(awk '/^Package:/ { print $$2 }' debian/control) ; do find debian/$$i -type l -print0 | xargs -r  -0 sed -i -e '' ; done

	debian/move-and-link-usr-share mcstas/usr/share/mccode/bin/mcstas

	# recode l1..u8 $(CURDIR)/debian/mcstas/usr/share/mccode/mcstas/$(MCCODE_VERSION)/share/general.c
	# recode l1..u8 $(CURDIR)/debian/mcstas/usr/share/mccode/mcstas/$(MCCODE_VERSION)/share/intersection.c

	debian/move-and-link-usr-share mcstas-comps/usr/share/mccode/lib/neutronics/libneutronics.a

	# recode l1..u8 $(CURDIR)/debian/mcstas-comps/usr/share/mccode/mcstas/$(MCCODE_VERSION)/share/chopper_fermi.c

	debian/move-and-link-usr-share mcxtrace/usr/share/mccode/bin/mcxtrace

execute_after_dh_auto_install:
	#chmod +x debian/*/usr/share/mccode/mcstas/$(MCCODE_VERSION)/bin/*
	#chmod +x debian/*/usr/share/mccode/mcxtrace/$(MCCODE_VERSION)/bin/*

	#chmod +x debian/mcxtrace-comps/usr/share/mccode/mcxtrace/$(MCCODE_VERSION)/data/get_xray_db_data

	rm -f debian/mcstas-mcdisplay-webgl/usr/share/mccode/mcstas/$(MCCODE_VERSION)/tools/Python/mcdisplay/webgl/jquery.min.js
	rm -f debian/mcxtrace-mcdisplay-webgl/usr/share/mccode/mcxtrace/$(MCCODE_VERSION)/tools/Python/mxdisplay/webgl/jquery.min.js

	mkdir -p debian/mcstas/usr/bin/ debian/mcxtrace/usr/bin/
	mv debian/mcstas/usr/share/mccode/bin/mcstas-pygen debian/mcstas/usr/bin/
	mv debian/mcxtrace/usr/share/mccode/bin/mcxtrace-pygen debian/mcxtrace/usr/bin/
	mv debian/mcstas-mcrun/usr/share/mccode/bin/mcsplit.py debian/mccode-common/usr/share/mccode/bin
	rm debian/mcxtrace-mxrun/usr/share/mccode/bin/mcsplit.py

execute_after_dh_gencontrol:
	for i in BUILD/*mcstas* ; do dh_gencontrol -p$$(basename $$i) -- -v$(MCSTAS_VERSION)+$(DEB_VERSION) ; done
	for i in BUILD/*mcxtrace* ; do dh_gencontrol -p$$(basename $$i) -- -v$(MCXTRACE_VERSION)+$(DEB_VERSION) ; done
