#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# work-around for upstream build that thinks $(HOME) belongs in the
# include path for all builds!
export HOME=$(CURDIR)/junkhome

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_ARCH      ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_GNU_CPU   ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)
DEB_BUILD_GNU_SYSTEM ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)

JAVA_UNSUPPORTED_CPUS = zhppaz zmipsz zs390xz zmipselz
JAVA_UNSUPPORTED_SYSTEMS = zkfreebsd-gnuz zgnuz

export RUBYLIBDIR=/usr/lib/ruby/vendor_ruby

export CFLAGS += $(shell dpkg-buildflags --get CFLAGS) -D_FILE_OFFSET_BITS=64
export CPPFLAGS += $(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS += $(shell dpkg-buildflags --get CXXFLAGS)
export LDFLAGS += $(shell dpkg-buildflags --get LDFLAGS)
export GCJFLAGS += $(shell dpkg-buildflags --get GCJFLAGS)
export MYCFLAGS := $(CFLAGS)

export CONFIGURE_VARS += CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" GCJFLAGS="$(GCJFLAGS)"
CONFIGURE_SWITCHES = --host=$(DEB_HOST_GNU_TYPE) \
		     --build=$(DEB_BUILD_GNU_TYPE) \
		     --prefix=/usr \
		     --mandir=\$${prefix}/share/man \
		     --infodir=\$${prefix}/share/info \
		     --includedir=\$${prefix}/include/estraier \
		     --libexecdir=\$${prefix}/lib/estraier

ifeq (,$(findstring z$(DEB_BUILD_GNU_CPU)z,$(JAVA_UNSUPPORTED_CPUS)))
ifeq (,$(findstring z$(DEB_BUILD_GNU_SYSTEM)z,$(JAVA_UNSUPPORTED_SYSTEMS)))
DEFAULT_JAVA ?= $(shell readlink /etc/alternatives/java | sed s@/jre/bin/java@@)
JAVA_HOME ?= $(DEFAULT_JAVA)
CONFIGURE_VARS += JAVA_HOME="$(JAVA_HOME)"
BUILD_JAVA = true
endif
endif

config.status: configure
	dh_testdir
	# Add here commands to configure the package.
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	$(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES)
	cd rubypure && $(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES)
	$(MAKE)
ifeq "$(BUILD_JAVA)" "true"
	cd javapure && $(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES) \
		--libdir=\$${prefix}/share/hyperestraier
	cd javanative && $(CONFIGURE_VARS) LIBS="../libestraier.a" ./configure $(CONFIGURE_SWITCHES)
endif
	cd perlnative && $(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES)

#Architecture 
build: build-arch build-indep

build-indep:

build-arch: build-arch-stamp
build-arch-stamp:  config.status
	# Add here commands to compile the arch part of the package.
	$(MAKE) && $(MAKE) check
	$(MAKE) estseek.fcgi
	cd rubypure && $(MAKE)
	perl -p -i -e 's@^#! /usr/bin/ruby1\.8 -w@#! /usr/bin/ruby -w@' rubypure/estcall.rb
	perl -p -i -e 's@^#! /usr/bin/ruby1\.8 -w@#! /usr/bin/ruby -w@' rubynative/estcmd.rb
ifeq "$(BUILD_JAVA)" "true"
	cd javapure && $(MAKE)
	cd javanative && $(MAKE) && $(MAKE) check
endif
	cd perlnative/src && perl Makefile.PL INC="-I/usr/include/qdbm -I. -I../.. -I/usr/include/estraier" \
		OPTIMIZE="$(CFLAGS) $(CPPFLAGS) $(FFLAGS)" \
		LIBS="-L../.. -lestraier -lqdbm -lz -lm -lc" INSTALLDIRS=vendor && $(MAKE) \
		OTHERLDFLAGS="$(LDFLAGS)"
	cd perlnative && $(MAKE) check
	#$(MAKE) doc
	touch build-arch-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp binary-arch-stamp binary-indep-stamp
	rm -rf config.sub config.guess rubypure/pure rubynative/native
	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	-cd rubypure && $(MAKE) distclean
	-cd rubynative && $(MAKE) distclean
	-cd rubynative/src && $(MAKE) distclean
	-cd javapure && $(MAKE) distclean
	-cd javanative && $(MAKE) distclean
	-cd perlnative && $(MAKE) distclean
	dh_clean 

# Build architecture dependant packages using the common target.
binary-arch: binary-arch-stamp
binary-arch-stamp: build-arch
	dh_testdir
	dh_testroot
	dh_prep 
	dh_installdirs -s
	# Add here commands to install the arch part of the package into 
	# debian/tmp.
	$(MAKE) install prefix=$(CURDIR)/debian/hyperestraier/usr
	perl -p -i -e 's/^\.TH ESTWAVER 3/\.TH ESTWAVER 1/' $(CURDIR)/debian/hyperestraier/usr/share/man/man1/estwaver.1

	# ruby
	mkdir -p $(CURDIR)/debian/ruby-hyperestraier/$(RUBYLIBDIR)
	cp -Rf rubypure/estraierpure.rb $(CURDIR)/debian/ruby-hyperestraier/$(RUBYLIBDIR)
	install -m 644 rubynative/estraier-doc.rb $(CURDIR)/debian/ruby-hyperestraier/$(RUBYLIBDIR)

	# build Ruby with gem2deb
	dh_ruby --install

	# create example directory tree
	cp -pR rubypure/example rubypure/pure
	cp -Rf rubypure/estcall.rb rubypure/pure
	cp -pR rubynative/example rubynative/native
	cp -Rf rubynative/estcmd.rb rubynative/native

ifeq "$(BUILD_JAVA)" "true"
	cd javapure && $(MAKE) install DESTDIR=$(CURDIR)/debian/libestraier-java
	mv $(CURDIR)/debian/libestraier-java/usr/share/hyperestraier/estraierpure.jar $(CURDIR)/debian/libestraier-java/usr/share/hyperestraier/lib/
	cd javanative && $(MAKE) install \
		DESTDIR=$(CURDIR)/debian/libestraier-java
	cp -L $(CURDIR)/debian/libestraier-java/usr/lib/libjestraier.so $(CURDIR)/debian/libestraier-java/usr/lib/jni/libjestraier.so
	mv $(CURDIR)/debian/libestraier-java/usr/lib/estraier.jar $(CURDIR)/debian/libestraier-java/usr/share/hyperestraier/lib/
	-rm -f $(CURDIR)/debian/libestraier-java/usr/lib/libjestraier.so*
endif
	chrpath -d perlnative/src/blib/arch/auto/Estraier/Estraier.so
	perl -p -i -e 's/^Perl Binding/Estraier - Perl Binding/' perlnative/src/blib/man3/Estraier.3pm
	cd perlnative && $(MAKE) install \
		DESTDIR=$(CURDIR)/debian/libestraier-perl
	# estmttest is test only
	rm $(CURDIR)/debian/hyperestraier/usr/bin/estmttest
	install -m 755 estseek.fcgi $(CURDIR)/debian/hyperestraier/usr/lib/estraier/
	chrpath -d $(CURDIR)/debian/hyperestraier/usr/lib/estraier/*cgi
	chrpath -d `file $(CURDIR)/debian/hyperestraier/usr/bin/* | awk -F: '/ELF/{print $$1}'`
	dh_install -s
	dh_movefiles --source=./debian/hyperestraier
	cd $(CURDIR)/debian/hyperestraier/usr/share/hyperestraier && \
	rm -rf COPYING ChangeLog THANKS doc
	find $(CURDIR)/debian -type d -empty -delete

	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common
	touch binary-arch-stamp

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installpam
#	dh_installmime
	dh_installinit
#	dh_installcron
	dh_installman
	dh_link
ifeq "$(BUILD_JAVA)" "true"
	-rm -f $(CURDIR)/debian/libestraier-java/usr/share/java/estraier.jar
endif
	dh_fixperms
	dh_strip
	dh_compress 
	dh_perl
	dh_makeshlibs --no-package=libestraier-java -V
	dh_installdeb
	dh_shlibdeps -l debian/libestraier8/usr/lib
	dh_gencontrol
	dh_lintian
	dh_md5sums
	dh_builddeb

binary: binary-arch binary-indep
binary-indep:
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch 
