#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.

package=salome
SALOME_VERSION=5.1.3
LIB_VERSION=0.0.0

builddir = build-$(package)

# Support multiple makes at once
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
NJOBS := 1
endif

# These are the modules which require build_configure to build.  They are
# ordered by priority and then dependency: KERNEL through NETGENPLUGIN are
# core; MULTIPR through SIERPINSKY are optional
# modules; LIGHT and PYLIGHT are alternate non-CORBA interface shells;
# HELLO through PYCALCULATOR are module examples; HXX2SALOME 
# and XDATA are module development tools.  In terms of dependency, KERNEL and
# HXX2SALOME are at the bottom, GUI depends on KERNEL, GEOM and MED also depend
# on GUI, VISU on MED and GUI, SMESH on GEOM MED and GUI, etc.
# Notes:
# - See ChangeLog regarding VISU, NETGENPLUGIN and SIERPINSKY modules.
# - XDATA is not a normal module, it needs special treatment.
# - BLSURFPLUGIN, GHS3D[PRL]PLUGIN and HexoticPLUGIN require non-free
#   libraries, and will not be part of the Debian package.
SALOME_MODULES = KERNEL_SRC_$(SALOME_VERSION) \
  GUI_SRC_$(SALOME_VERSION) \
  GEOM_SRC_$(SALOME_VERSION) \
  MED_SRC_$(SALOME_VERSION) \
  VISU_SRC_$(SALOME_VERSION) \
  SMESH_SRC_$(SALOME_VERSION) \
  YACS_SRC_$(SALOME_VERSION) \
  MULTIPR_SRC_$(SALOME_VERSION) \
  COMPONENT_SRC_$(SALOME_VERSION) \
  RANDOMIZER_SRC_$(SALOME_VERSION) \
  LIGHT_SRC_$(SALOME_VERSION) \
  PYLIGHT_SRC_$(SALOME_VERSION) \
  HELLO_SRC_$(SALOME_VERSION) \
  PYHELLO_SRC_$(SALOME_VERSION) \
  CALCULATOR_SRC_$(SALOME_VERSION) \
  PYCALCULATOR_SRC_$(SALOME_VERSION) \
  HXX2SALOME_SRC_$(SALOME_VERSION)
# XDATA_SRC_$(SALOME_VERSION) \
  BLSURFPLUGIN_SRC_$(SALOME_VERSION) \
  GHS3DPLUGIN_SRC_$(SALOME_VERSION) \
  GHS3DPRLPLUGIN_SRC_$(SALOME_VERSION) \
  HexoticPLUGIN_SRC_$(SALOME_VERSION)

DOC_MODULES = KERNEL_SRC_$(SALOME_VERSION) \
  GUI_SRC_$(SALOME_VERSION) \
  GEOM_SRC_$(SALOME_VERSION) \
  MED_SRC_$(SALOME_VERSION) \
  VISU_SRC_$(SALOME_VERSION) \
  SMESH_SRC_$(SALOME_VERSION) \
  YACS_SRC_$(SALOME_VERSION) \
  HELLO_SRC_$(SALOME_VERSION) \
  PYHELLO_SRC_$(SALOME_VERSION) \
  PYCALCULATOR_SRC_$(SALOME_VERSION) \

clean:
	dh_testdir
	-rm -rf $(builddir)

#	Remove files copied from debian/
	-rm -f KERNEL_SRC_$(SALOME_VERSION)/bin/runSalome.in \
	  KERNEL_SRC_$(SALOME_VERSION)/bin/killSalome.in \
	  KERNEL_SRC_$(SALOME_VERSION)/bin/appliskel/env.d/envProducts.sh.in

#	Remove files generated by build_configure
	-find * -name build_configure.log | xargs rm
	-find * -name configure.in | grep -v XDATA | xargs rm
	-rm -f HXX2SALOME_SRC_5.1.3/Makefile.am.list YACS_SRC_5.1.3/yacs_config.h.in
#	Remove files generated by libtool, except in DEPRECATED directories
	-find * -name libtool.m4 -o -name ltmain.sh -o -name lt\*.m4 -o -name config.guess -o -name config.sub | grep -v /DEPRECATED/ | xargs rm
#	Remove files generated by automake, except in DEPRECATED directories
	-find * -name depcomp -o -name install-sh -o -name missing | \
	  grep -v /DEPRECATED/ | grep -v /HXX2SALOME_GENERIC_CLASS_NAME_SRC/ | grep -v XDATA | \
	  xargs rm
	-find * -name py-compile | grep config_files | xargs rm
#	some Makefile.in are not generated by automake
	-find * -name Makefile.in | grep -v XDATA | while read f; do \
	  test ! -e $${f%.in}.am || rm -f $$f; \
	done
#	Remove files generated by autoconf
	-find * -name configure | grep -v COMPO_CXX_SRC | grep -v XDATA | xargs rm
#	Remove files generated by aclocal
	-find * -name aclocal.m4 | grep -v XDATA | xargs rm

	QUILT_PATCHES=debian/patches quilt pop -a || test $$? = 2
	-rm -rf .pc

	rm -f *-stamp
#	salome-dev-doc isn't a real package yet
	rm -rf debian/salome-dev-doc
	dh_clean

patch-stamp:
	QUILT_PATCHES=debian/patches quilt push -a || test $$? = 2
	touch $@

build: build-arch build-indep
build-arch: build-arch-stamp
build-arch-stamp: patch-stamp
	dh_testdir

#	Install new .in files
	cp -fp debian/runSalome.in debian/killSalome.in KERNEL_SRC_$(SALOME_VERSION)/bin/
	cp -fp debian/envProducts.sh.in KERNEL_SRC_$(SALOME_VERSION)/bin/appliskel/env.d/

	set -e; for salomodule in $(SALOME_MODULES); do \
	  echo; echo RECONFIGURING SALOME MODULE $$salomodule; echo; \
	  cd $$salomodule; \
	   KERNEL_ROOT_DIR=$(CURDIR)/KERNEL_SRC_$(SALOME_VERSION) \
	     GUI_ROOT_DIR=$(CURDIR)/GUI_SRC_$(SALOME_VERSION) \
	     MED_ROOT_DIR=$(CURDIR)/MED_SRC_$(SALOME_VERSION) \
	     GEOM_ROOT_DIR=$(CURDIR)/GEOM_SRC_$(SALOME_VERSION) \
	     RANDOMIZER_ROOT_DIR=$(CURDIR)/RANDOMIZER_SRC_$(SALOME_VERSION) \
	     COMPONENT_ROOT_DIR=$(CURDIR)/COMPONENT_SRC_$(SALOME_VERSION) \
	     CALCULATOR_ROOT_DIR=$(CURDIR)/CALCULATOR_SRC_$(SALOME_VERSION) \
	     PYCALCULATOR_ROOT_DIR=$(CURDIR)/PYCALCULATOR_SRC_$(SALOME_VERSION) \
	     SMESH_ROOT_DIR=$(CURDIR)/SMESH_SRC_$(SALOME_VERSION) \
	     SIERPINSKY_ROOT_DIR=$(CURDIR)/SMESH_SRC_$(SALOME_VERSION) \
	     ./build_configure; \
	  cd ..; \
	done

	-mkdir -p $(builddir)
	set -e; cd $(builddir); for salomodule in $(SALOME_MODULES); do \
	  mkdir $$salomodule || true; \
	  cd $$salomodule; \
	   echo; echo CONFIGURING SALOME MODULE $$salomodule; echo; \
	   ../../$$salomodule/configure --prefix=/usr \
	     CASROOT=/usr VTKSUFFIX=-5.4 NETGENHOME=/usr \
	     --disable-dependency-tracking \
	     --disable-maintainer-mode \
	     --with-mpi=/usr --with-mpi_include=/usr/include/mpi \
	     KERNEL_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     GUI_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     MED_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     GEOM_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     RANDOMIZER_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     COMPONENT_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     CALCULATOR_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     PYCALCULATOR_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     SMESH_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     SIERPINSKY_ROOT_DIR=$(CURDIR)/debian/tmp/usr; \
	   \
	   echo; echo COMPILING SALOME MODULE $$salomodule; echo; \
	   $(MAKE) -j $(NJOBS) LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/salome/lib bindir=/usr/lib/salome/bin libdir=/usr/lib/salome/lib docdir=/usr/share/doc/salome-doc; \
	   \
	   echo; echo INSTALLING SALOME MODULE $$salomodule; echo; \
	   $(MAKE) install LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/salome/lib DESTDIR=$(CURDIR)/debian/tmp bindir=/usr/lib/salome/bin libdir=/usr/lib/salome/lib docdir=/usr/share/doc/salome-doc; \
	  cd ..; \
	  rm -f ../debian/tmp/usr/lib/salome/lib/*.la; \
	  rm -rf $$salomodule; \
	done
	rmdir $(builddir)

#	Restore these when the XDATA clean target actually works
#	echo; echo CONFIGURING SALOME MODULE XDATA; echo
#	(cd XDATA_SRC_$(SALOME_VERSION) && \
	 aclocal -I adm/unix/config_files && autoconf && automake && \
	 ./configure --prefix=/usr VTKSUFFIX=-5.4 \
	   KERNEL_ROOT_DIR=$(CURDIR)/KERNEL_SRC_$(SALOME_VERSION))
	touch $@

# Unfortunately, when building in a separate $(builddir), build-indep requires
# build-arch because for example one needs to build the KERNEL module before
# configuring the GUI module, etc.
build-indep: build-indep-stamp
build-indep-stamp: build-arch-stamp
	-mkdir -p $(builddir)
	set -e; cd $(builddir); for salomodule in $(DOC_MODULES); do \
	  mkdir $$salomodule || true; \
	  cd $$salomodule; \
	   ../../$$salomodule/configure --prefix=/usr \
	     CASROOT=/usr VTKSUFFIX=-5.4 NETGENHOME=/usr \
	     --with-mpi=/usr --with-mpi_include=/usr/include/mpi \
	     KERNEL_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     GUI_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     MED_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     GEOM_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     RANDOMIZER_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     COMPONENT_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     CALCULATOR_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     PYCALCULATOR_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     SMESH_ROOT_DIR=$(CURDIR)/debian/tmp/usr \
	     SIERPINSKY_ROOT_DIR=$(CURDIR)/debian/tmp/usr; \
	   \
	   echo; echo GENERATING USER DOCUMENTATION IN MODULE $$salomodule; echo; \
	   $(MAKE) -C doc usr_docs install -j $(NJOBS) DESTDIR=$(CURDIR)/debian/salome-doc bindir=/usr/lib/salome/bin libdir=/usr/lib/salome/lib docdir=/usr/share/doc/salome-doc; \
	   echo; echo GENERATING DEVELOPER DOCUMENTATION IN MODULE $$salomodule; echo; \
	   $(MAKE) -C doc clean dev_docs install -j $(NJOBS) DESTDIR=$(CURDIR)/debian/salome-dev-doc bindir=/usr/lib/salome/bin libdir=/usr/lib/salome/lib docdir=/usr/share/doc/salome-doc; \
	  cd ..; \
	  rm -rf $$salomodule; \
	done
	rmdir $(builddir)

	touch $@ install-indep-stamp

install: build install-stamp

install-arch: build-arch install-stamp

install-indep: install-indep-stamp

# Later maybe make this run make install only in the doc directories, so it
# doesn't try to build everything else
install-indep-stamp: build-indep

# This installs everything, so it's not really install-arch-stamp
install-stamp:
	dh_testdir
	mv debian/tmp/usr/idl debian/tmp/usr/share/
	install -d debian/tmp/usr/share/aclocal
	cp -af debian/tmp/usr/salome_adm/unix/config_files/check_Kernel.m4 debian/tmp/usr/share/aclocal/salome.m4
	for m4file in check_GUI.m4 check_GEOM.m4 check_Med.m4 check_RANDOMIZER.m4 check_SMESH.m4; do \
	  cat debian/tmp/usr/adm_local/unix/config_files/$$m4file >> debian/tmp/usr/share/aclocal/salome.m4; \
	done
#	Temporary kludge until there's a more permanent way to install this
	install -d debian/salome/usr/bin
	mv debian/tmp/usr/lib/salome/bin/runSalome debian/tmp/usr/lib/salome/bin/killSalome debian/salome/usr/bin/
	install -d debian/tmp/usr/share/applications
	cp -a debian/salome.desktop debian/tmp/usr/share/applications/
	rm -f debian/tmp/usr/bin/appliskel/env.d/*.in
	mv debian/tmp/usr/lib/salome/lib/SalomePyQt.so debian/tmp/usr/lib/salome/lib/SalomePyQt.so.0
	ln -s SalomePyQt.so.0 debian/tmp/usr/lib/salome/lib/SalomePyQt.so
	mv debian/tmp/usr/Tests debian/tmp/usr/share/salome/
	rm -rf debian/tmp/usr/lib64
	rm -rf debian/tmp/usr/doc
	rm -rf debian/tmp/usr/lib/salome/bin/appliskel
#	rm -rf debian/tmp/usr/lib/salome/bin/styles
	rm -rf debian/tmp/usr/lib/salome/bin/HXX2SALOME_GENERIC_CLASS*
	rm -f debian/tmp/usr/lib/salome/bin/*.pyo debian/tmp/usr/lib/salome/bin/*.pyc
	rm -f debian/tmp/usr/lib/salome/bin/*.csh debian/tmp/usr/lib/salome/bin/*.ksh debian/tmp/usr/lib/salome/bin/*.bat
	for shscript in `ls debian/tmp/usr/lib/salome/bin/*.sh`; do \
	  shbase=`basename $$shscript .sh`; \
	  mv debian/tmp/usr/lib/salome/bin/$$shbase.sh debian/tmp/usr/lib/salome/bin/$$shbase; \
	done
	mv debian/tmp/usr/lib/salome/bin/*.xml debian/tmp/usr/lib/salome/bin/VERSION \
	  debian/tmp/usr/lib/salome/bin/*.tgz debian/tmp/usr/lib/salome/bin/*.awk \
	  debian/tmp/usr/share/salome/
	install -d debian/salome/usr/lib/salome/bin
	mv debian/tmp/usr/lib/salome/bin/SALOME_ContainerPy.py debian/salome/usr/lib/salome/bin/SALOME_ContainerPy
	mv debian/tmp/usr/lib/salome/bin/*.py debian/tmp/usr/lib/python*/*-packages/salome/
	(cd debian/tmp/usr/lib/python*/*-packages/salome && \
	  chmod -x *.py *.so.$(LIB_VERSION) *.la *.a && rm -f *.pyc *.pyo)
	for pyscript in avs2med med2sauv salomeloader sauv2med; do \
	  mv debian/tmp/usr/lib/salome/bin/$$pyscript debian/salome/usr/bin/; \
	done
	(cd debian/tmp/usr/share/salome && \
	  chmod -x resources/*/*.xml *.awk *.tgz)
	install -d debian/salome-examples/usr/share/salome/examples
	cp -a SAMPLES_SRC_$(SALOME_VERSION)/* debian/salome-examples/usr/share/salome/examples/
	chmod -x `find debian/salome-examples/usr/share/salome/examples -type f`

# This installs everything that install-indep-stamp is supposed to install
	touch $@

binary-indep: install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i
	dh_movefiles -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install-arch
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_movefiles -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_installmenu -a
	dh_strip -a
	dh_makeshlibs -a
	dh_compress -a
	dh_pycentral -psalome
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a -l $(CURDIR)/debian/salome/usr/lib/salome/lib
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep

.PHONY: binary binary-arch binary-indep clean install install-arch install-indep build build-arch build-indep
