#!/usr/bin/make -f
# -*- gmake -*-
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independant
# package.
# Modified by Rafael Laboissiere <rafael@debian.org> on 1998/06/12
# for the plplot package

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

# Paths for Octave
MDIR = $(shell octave-config --print LOCALAPIFCNFILEDIR)
OCTDIR = $(shell octave-config --print LOCALAPIOCTFILEDIR)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH) 

version		:= $(shell perl debian/get-upstream-version.pl < cmake/modules/plplot_version.cmake)
libpkg          := libplplot12
docpkg          := plplot-doc
debbase		:= $(shell pwd)/debian
debtmp		:= $(debbase)/tmp
debusr		:= $(debtmp)/usr
deblib		:= $(debusr)/lib/$(DEB_HOST_MULTIARCH)
debshr		:= $(debusr)/share
debdoc		:= $(debshr)/doc
debinfo		:= $(debshr)/info
debex		:= $(debshr)/plplot$(version)/examples
octver		:= $(shell octave-config -v)

# Note cmake ignores CPPFLAGS so add them to CFLAGS and CXXFLAGS as a work around
CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS) \
	   $(shell mkoctfile -p INCFLAGS)
CFLAGS = $(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS)
CFLAGS += -fvisibility=hidden
#Don't add -fvisibility=hidden to CXXFLAGS for now as this breaks the octave bindings.
CXXFLAGS = $(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS)
#CXXFLAGS += -fvisibility=hidden
FFLAGS = $(shell dpkg-buildflags --get FFLAGS)
FFLAGS += -fvisibility=hidden
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
LDFLAGS += -Wl,--as-needed

CMAKE = xvfb-run cmake

JAVA_HOME = /usr/lib/jvm/default-java

PYVERS = $(shell pyversions -vr)

OCAMLABI := $(shell ocamlc -version)
OFILES := $(filter-out debian/control,$(patsubst %.in,%,$(wildcard debian/*.in)))

DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

# Disable Ada build on sh4 - gnat not available at the moment.

ifneq (,$(findstring :$(DEB_BUILD_ARCH):,:sh4:))
BUILD_ADA = no
else
BUILD_ADA = yes
endif

# Configure options

ifeq ($(BUILD_ADA),yes)
ADA_OPTIONS = -DENABLE_ada=ON
else
ADA_OPTIONS = -DENABLE_ada=OFF
endif

JAVA_OPTIONS = \
	-DCMAKE_Java_RUNTIME=$(JAVA_HOME)/bin/java \
	-DCMAKE_Java_COMPILER=$(JAVA_HOME)/bin/javac \
	-DCMAKE_Java_ARCHIVE=$(JAVA_HOME)/bin/jar \
	-DJAVACFLAGS="-source 1.5 -target 1.5"

CONFIGURE_OPTIONS = -DBUILD_TEST=ON \
	-DBUILD_DOC=OFF -DCMAKE_INSTALL_PREFIX=/usr \
        -DCMAKE_INSTALL_LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH) \
	-DOCTAVE_OCT_DIR=$(OCTDIR) \
	-DUSE_RPATH=OFF -DPLD_psttf=OFF -DENABLE_tk=ON \
        -DTCL_TCLSH=/usr/bin/tclsh8.5 \
	$(ADA_OPTIONS) $(JAVA_OPTIONS) 

BUILD_DIR = $(debbase)/build_tmp
SRC_DIR = $(shell pwd)

control:
ifeq ($(BUILD_ADA),no)
	slice -o UNDEF:debian/control debian/control.in
else
	slice -o UNDEF+ADA:debian/control debian/control.in
endif

config: config-stamp
config-stamp: control
	mkdir -p $(BUILD_DIR)
	( cd $(BUILD_DIR) ; \
	PKG_CONFIG_ENV=" " FC=gfortran \
	JAVA_HOME=$(JAVA_HOME) DC=$(DC) \
        CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" FFLAGS="$(FFLAGS)" \
        LDFLAGS="$(LDFLAGS)" CPPFLAGS="$(CPPFLAGS)" \
	$(CMAKE) $(SRC_DIR) $(CONFIGURE_OPTIONS) )
	touch config-stamp

build-arch: build-arch-stamp
build-arch-stamp: config-stamp
	dh_testdir
	( cd $(BUILD_DIR) ; $(MAKE)  )
	-( cd $(BUILD_DIR) ; unset DISPLAY ; $(MAKE) test_noninteractive )
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: 
	dh_testdir
	( cd $(BUILD_DIR) ; $(CMAKE) $(SRC_DIR) $(CONFIGURE_OPTIONS) -DBUILD_DOC=ON ; \
	cd doc ; $(MAKE)  )
	touch build-indep-stamp

clean: 
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp install-arch-stamp install-indep-stamp \
              config-stamp debian/files install-python*
	rm -rf $(BUILD_DIR)
	rm -rf $(debbase)/$(libpkg)
	dh_clean

install-arch: install-arch-stamp install-python
	dh_movefiles -a --no-package=$(libpkg) --no-package=libplplot-dev \
		     --no-package=python-plplot --no-package=python-plplot-qt   \
		     --no-package=libplplot-java --no-package=libplplot-ada1 \
	             --no-package=libplplot-ocaml --no-package=libplplot-lua \
	             --no-package=libplplot-d --no-package=libplplot-ada1-dev
	dh_movefiles --package=python-plplot-qt
	dh_movefiles --package=python-plplot
	dh_movefiles --package=libplplot-java
	dh_movefiles --package=libplplot-ocaml
	dh_movefiles --package=libplplot-lua
	dh_movefiles --package=libplplot-d
ifeq ($(BUILD_ADA),yes)
	dh_movefiles --package=libplplot-ada1
	dh_movefiles --package=libplplot-ada1-dev 
endif
	dh_movefiles --package=libplplot-dev
	dh_movefiles --package=$(libpkg)

install-arch-stamp: build-arch-stamp
	dh_testdir
	dh_testroot

	# Strip bindings/octave/plplot_octave.oct as this is missed by 
        # dh_strip.
	strip --strip-unneeded --remove-section=.comment $(BUILD_DIR)/bindings/octave/plplot_octave.oct

	( cd $(BUILD_DIR) ; \
	  $(MAKE) DESTDIR=$(debtmp) install )

	( cd $(debdoc)/plplot ; \
	  rm -f COPYING.LIB ; \
	  mv ChangeLog.release changelog )
	mv $(debdoc)/plplot $(debdoc)/$(libpkg)

	install -d $(debex)/perl
	install --mode=644 examples/perl/x* examples/perl/README* \
		$(debex)/perl
	mv $(debex) $(debdoc)/$(libpkg)

	for i in libplplot-dev plplot-tcl plplot-tcl-bin plplot-tcl-dev	\
		plplot12-driver-xwin octave-plplot			\
		python-plplot-qt                                        \
		python-plplot libplplot-c++11 plplot12-driver-wxwidgets \
		plplot12-driver-qt libplplot-java			\
		plplot12-driver-cairo libplplot-fortran10		\
		libplplot-ocaml libplplot-lua libplplot-d ; do		\
	  ln -s $(libpkg) $(debdoc)/$$i ;				\
	done
	ln -s libqsastime0 $(debdoc)/libqsastime-dev

	mv $(debshr)/java/plplot.jar $(debshr)/java/plplot-$(version).jar
	( cd $(debshr)/java ; ln -s plplot-$(version).jar plplot.jar )

ifeq ($(BUILD_ADA),yes)
	ln -s $(libpkg) $(debdoc)/libplplot-ada1
	ln -s $(libpkg) $(debdoc)/libplplot-ada1-dev
endif

	touch install-arch-stamp

install-indep: install-indep-stamp
	dh_movefiles -i

install-indep-stamp: build-indep-stamp
	( cd $(BUILD_DIR)/doc ; \
	  $(MAKE) DESTDIR=$(debtmp) install )

	mkdir -p $(debdoc)/$(docpkg)
	(cd $(debdoc)/plplot ; rm -f html/plplotdoc-html.proc ; mv html *.pdf $(debdoc)/$(docpkg))
	touch install-indep-stamp

install-python: $(PYVERS:%=install-python%)
	touch $@

install-python%: build-arch-stamp
	( cd $(BUILD_DIR) ; \
	  $(CMAKE) $(SRC_DIR) $(CONFIGURE_OPTIONS) -DPYTHON_EXECUTABLE=/usr/bin/python$* -DPYTHON_LIBRARIES=/usr/lib/python$* -DPYTHON_LIBRARY=`python-config --configdir`/libpython$*.so -DPYTHON_INCLUDE_PATH="/usr/include/python$*;/usr/include/numpy;/usr/lib/pyshared/python$*/numpy/core/include/numpy"  -DNUMERIC_INCLUDE_PATH=/usr/include/numpy ; \
	  for d in python qt_gui/pyqt4 ; do		\
	    ( cd bindings/$$d;				\
	      $(MAKE) clean ;				\
	      $(MAKE) ;					\
	      $(MAKE) DESTDIR=$(debtmp) install ) ;	\
	  done )
	touch $@

# Build architecture-independent files here.
binary-indep: build-indep install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i
	dh_installdocs --package=$(docpkg)
	dh_installchangelogs --package=$(docpkg)
	dh_compress -i --exclude=.pdf
	dh_fixperms -i
	dh_installdeb -i
	dh_lintian -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build-arch install-arch
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a

	dh_installdocs --package=$(libpkg) --package=libcsiro0 --package=libqsastime0
	dh_installchangelogs --package=$(libpkg) --package=libcsiro0 --package=libqsastime0

	dh_python2 --package=python-plplot-qt --package=python-plplot
	dh_numpy
	dh_strip -a
	dh_compress -a -Xexamples/
	dh_fixperms -a
	dh_makeshlibs -V -a
	dh_shlibdeps -a --no-package=octave-plplot -L $(libpkg) \
          -l $(debbase)/$(libpkg)/usr/lib/$(DEB_HOST_MULTIARCH):$(debbase)/plplot-tcl/usr/lib/$(DEB_HOST_MULTIARCH)
	dpkg-shlibdeps -Tdebian/octave-plplot.substvars -S$(debbase)/$(libpkg)/usr/lib/*.so* -S/usr/lib/$(octver)/*.so* $(debbase)/octave-plplot/$(OCTDIR)/plplot_octave.oct
	dh_installdeb -a

        # Replace tk copied files with symlinks
	(cd $(debbase)/plplot-tcl-dev/usr/share/doc/${libpkg}/examples/tk ; for file in x??.tcl plgrid.tcl ; do rm -f $${file} ; ln -s ../tcl/$${file} . ; done)

	dh_lintian -a
	dh_gencontrol -a -- -VF:OCamlABI="$(OCAMLABI)"
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep

get-orig-source:
	uscan --force-download

.PHONY: build clean binary-indep binary-arch binary config \
        control 

.NOTPARALLEL:

