#!/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=opencascade

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

QUILT := quilt --quiltrc debian/patches/quiltrc

BUILD_DIR := builddir

unpatch:
	dh_testdir
	if [ -e .pc ]; then \
	  $(QUILT) pop -a -R || test $$? = 2 || exit 1; \
	  rm -f patch-stamp; \
	fi

clean: unpatch
	dh_testdir
	rm -rf $(BUILD_DIR)
	rm -f *-stamp
# This is left by a patch-stamp/unpatch combination
	rm -rf .pc
# Files created by autoreconf in ros/ directory
	rm -rf ros/autom4te.cache ros/config.h.in~
	dh_clean

patch: patch-stamp
patch-stamp:
	dh_testdir
	$(QUILT) push -a || test $$? = 2
	touch $@

reconfigure-stamp: patch-stamp
	dh_testdir
	cd ros && autoreconf -fi
	touch $@

#  configure options which are not used currently, but may be enabled:
#  --with-freeimage=/usr
#    Not enabled because this pulls in too many dependencies
#  --with-tbb-include=/usr/include --with-tbb-library=/usr/lib
#    There are only 2 parallelized simple loops in the whole code,
#    so if we really want to enable multithread, openMP seems a much
#    better alternative, tbb is available only on very few architectures
#  --enable-openmp
#    This flag is Debian specific, it is added by debian/patches/add-enable-openmp.patch
#    When this option is set, loops described above are parallelized with openmp.
#    It is enabled now for testing, but may be disabled later.
configure-stamp: reconfigure-stamp
	dh_testdir
	rm -rf $(BUILD_DIR)
	mkdir $(BUILD_DIR)
# Set CPPFLAGS to find arch-6.5.0/Standard_Macro64.hxx.  Ideally upstream would
# add $(top_builddir)/inc to INCLUDES in their Makefile.am files.
	cd $(BUILD_DIR) && ../ros/configure \
	  --prefix=/usr --enable-shared --disable-debug \
	  --disable-dependency-tracking --disable-maintainer-mode \
	  --with-freetype=/usr --with-ftgl=/usr --with-gl2ps=/usr \
	  --enable-openmp \
	  --with-tcl=/usr/lib/tcl8.5 --with-tk=/usr/lib/tk8.5 \
	  CPPFLAGS="$(CPPFLAGS) -I'$(CURDIR)/$(BUILD_DIR)/inc'"
	touch $@

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	$(MAKE) -C $(BUILD_DIR) -j $(NJOBS)
	touch $@

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	-rm -rf debian/tmp
	$(MAKE) -C $(BUILD_DIR) install DESTDIR=$(CURDIR)/debian/tmp
	# Fix OpenCascade mess
	rm -rf debian/tmp/usr/lin debian/tmp/usr/Linux
	install -d debian/tmp/usr/include
	mv debian/tmp/usr/inc debian/tmp/usr/include/opencascade
	install -d debian/tmp/usr/share/opencascade/6.5.0
	mv debian/tmp/usr/src debian/tmp/usr/share/opencascade/6.5.0/src
	for script in demo idoc mdltest mkdoc tdoc test2xl vmdltest; do \
	  chmod +x debian/tmp/usr/share/opencascade/6.5.0/src/DrawResources/$$script; \
	done
	# Improve integration in Debian desktop
	install -d debian/tmp/usr/share/applications
	cp -p debian/opencascade-draw.desktop debian/tmp/usr/share/applications/
	install -d debian/tmp/usr/share/pixmaps
	cp -p debian/occ-icon-32.xpm debian/tmp/usr/share/pixmaps/
	install -d debian/tmp/usr/share/man/man1
	cp -p debian/DRAWEXE.1 debian/tmp/usr/share/man/man1/
	find debian/tmp/usr/share -name FILES -exec rm -f {} \;
	touch $@

binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i
	dh_install -i --sourcedir=debian/tmp --list-missing
ifneq (,$(filter $(DEB_HOST_ARCH), $(gcj_native_archs))) 
	cp -a debian/tmp/usr/lib/libTKjcas.so debian/tmp/usr/lib/libTKjcas.la \
                debian/libopencascade-foundation-dev/usr/lib
endif
	dh_installdocs -i
	dh_installchangelogs -i
	dh_lintian -i
	dh_compress -i -X.pdf -X.js -X.xml -X.csfdb -X.iges -X.BMP -X.xwd -X.brep -X.rle -X.step -X.stl
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_install -a --sourcedir=debian/tmp --list-missing
ifneq (,$(filter $(DEB_HOST_ARCH), $(gcj_native_archs))) 
	cp -a debian/tmp/usr/lib/libTKjcas-*.so debian/libopencascade-foundation-6.5.0/usr/lib
endif
	dh_installdocs -a
	dh_installchangelogs -a
	dh_strip -a
	dh_makeshlibs -a
	dh_installmenu -a
	dh_lintian -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep

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