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

include /usr/share/octave/debian/defs.make
include /usr/share/quilt/quilt.make

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

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

PYVERS = $(shell pyversions -vr)

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

# Disable java and ada build on arm - problems with java-gcj-compat-dev
# at the moment. Also gnat not available on arm, mips, and mipsel.

ifneq (,$(findstring :$(DEB_BUILD_ARCH):,:alpha:arm:armeb:armel:mips:mipsel:kfreebsd-amd64:))
BUILD_ADA = no
else
BUILD_ADA = yes
endif

ifneq (,$(findstring :$(DEB_BUILD_ARCH):,:alpha:arm:hppa:hurd-i386:))
BUILD_JAVA = no
else
BUILD_JAVA = yes
endif

# Configure options

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

ifeq ($(BUILD_JAVA),yes)
JAVA_OPTIONS = \
	-DCMAKE_Java_RUNTIME=$(JAVA_HOME)/bin/java \
	-DCMAKE_Java_COMPILER=$(JAVA_HOME)/bin/javac \
	-DCMAKE_Java_ARCHIVE=$(JAVA_HOME)/bin/jar
else
JAVA_OPTIONS = -DENABLE_java=OFF
endif

CONFIGURE_OPTIONS = -DBUILD_TEST=ON -DHAVE_PTHREAD=ON \
	-DBUILD_DOC=OFF -DCMAKE_INSTALL_PREFIX=/usr \
	-DOCTAVE_OCT_DIR=$(OCTDIR) \
	-DUSE_RPATH=OFF -DPLD_psttf=OFF -DENABLE_tk=ON  -DENABLE_ocaml=OFF\
	$(ADA_OPTIONS) $(JAVA_OPTIONS) 

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


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

config: config-stamp
config-stamp: control
	mkdir $(BUILD_DIR)
	(cd $(BUILD_DIR) ; \
	PKG_CONFIG_ENV=" " F77=gfortran FC=gfortran \
	JAVA_HOME=$(JAVA_HOME) \
	cmake $(SRC_DIR) $(CONFIGURE_OPTIONS) -DENABLE_python=OFF )
	touch config-stamp

build-arch: build-arch-stamp
build-arch-stamp: patch config-stamp
	dh_testdir
	# The hack below is to go around a bug in matwrap that was
	# preventing the Octave binding to compile with g++ 4.0
	#( cd lib ; $(MAKE) )
	#( cd src ; $(MAKE) )
	#( cd bindings/octave ; \
	#  $(MAKE) CC="gcc -D__builtin_va_list=void" plplot_octave.oct )
	( cd $(BUILD_DIR) ; $(MAKE)  )
	-( cd $(BUILD_DIR) ; ITCL_LIBRARY=/usr/lib/itcl3.2 ctest -VV)
	touch build-arch-stamp

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

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp install-arch-stamp install-indep-stamp \
              config-stamp patch-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=plplot-bin --no-package=python-plplot   \
		     --no-package=libplplot9-java --no-package=libplplot-ada
	dh_movefiles --package=python-plplot
	dh_movefiles --package=plplot-bin
ifeq ($(BUILD_JAVA),yes)
	dh_movefiles --package=libplplot9-java
endif
ifeq ($(BUILD_ADA),yes)
	dh_movefiles --package=libplplot-ada
	dh_movefiles --package=libplplot-dev usr/share/ada
else
	dh_movefiles --package=libplplot-dev
endif
	dh_movefiles --package=$(libpkg)

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

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

	( cd $(debdoc)/plplot ; \
	  rm -f COPYING.LIB *.dvi ; \
	  mv ChangeLog 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)
	rm -f $(debtmp)/usr/bin/plplot-config

	for i in libplplot-dev plplot-tcl plplot-tcl-dev 		\
		plplot9-driver-gd plplot9-driver-xwin octave-plplot	\
		python-plplot plplot-bin libplplot-c++9c2 		\
		plplot9-driver-gnome2 plplot9-driver-wxwidgets		\
		plplot9-driver-cairo libplplot-fortran9	; do		\
	  ln -s $(libpkg) $(debdoc)/$$i ;				\
	done

ifeq ($(BUILD_JAVA),yes)
	ln -s $(libpkg) $(debdoc)/libplplot9-java
	mv $(debshr)/java/plplot.jar $(debshr)/java/plplot-$(version).jar
	( cd $(debshr)/java ; ln -s plplot-$(version).jar plplot.jar )
endif

ifeq ($(BUILD_ADA),yes)
	ln -s $(libpkg) $(debdoc)/libplplot-ada
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 ; mv html *.pdf *.ps.gz $(debdoc)/$(docpkg))
	touch install-indep-stamp

install-python: $(PYVERS:%=install-python%)
	( cd $(BUILD_DIR)/examples/python ; $(MAKE) DESTDIR=$(debtmp) install )
	mv $(debex)/python $(debdoc)/$(libpkg)/examples
	touch $@

install-python%: build-arch-stamp
	( cd $(BUILD_DIR) ; \
	  cmake $(SRC_DIR) $(CONFIGURE_OPTIONS) -DENABLE_python=ON -DENABLE_pygcw=ON -DPYTHON_EXECUTABLE=/usr/bin/python$* -DPYTHON_LIBRARIES=/usr/lib/python$* -DPYTHON_LIBRARY=/usr/lib/python$*/config/libpython$*.so -DPYTHON_INCLUDE_PATH="/usr/include/python$*;/usr/include/numpy"  -DNUMERIC_INCLUDE_PATH=/usr/include/numpy ; \
	  for d in gnome2/python python ; 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_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
	dh_installchangelogs --package=$(libpkg) --package=libcsiro0

	dh_pycentral --package=python-plplot
	dh_strip -a
	dh_compress -a -Xexamples/
	dh_fixperms -a
	dh_makeshlibs -V -a
	dh_shlibdeps -a -L $(libpkg) \
          -l $(debbase)/$(libpkg)/usr/lib:$(debbase)/plplot-tcl/usr/lib
	octave-depends --package=octave-plplot
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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