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

# 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
JAVA_UNSUPPORTED_SYSTEMS = zkfreebsd-gnuz

RUBYLIBDIR ?= $(shell ruby1.8 -rrbconfig -e "puts Config::CONFIG['rubylibdir']")
RUBYARCHDIR ?= $(shell ruby1.8 -rrbconfig -e "puts Config::CONFIG['archdir']")
RUBY19LIBDIR ?= $(shell ruby1.9.1 -rrbconfig -e "puts Config::CONFIG['rubylibdir']")
RUBY19ARCHDIR ?= $(shell ruby1.9.1 -rrbconfig -e "puts Config::CONFIG['archdir']")

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

CONFIGURE_VARS = CFLAGS="$(CFLAGS)"
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 \
		     --enable-java

DEFAULT_JAVA ?= $(shell readlink /etc/alternatives/java | sed s@/jre/bin/java@@)
ifneq (,$(findstring /usr/bin/gij,$(DEFAULT_JAVA)))
CONFIGURE_VARS += JAVAC="gcj-wrapper" JAR="fastjar"
#CONFIGURE_SWITCHES += --with-gcj
else
JAVA_HOME ?= $(DEFAULT_JAVA)
CONFIGURE_VARS += JAVA_HOME="$(JAVA_HOME)" JAVAC="$(JAVA_HOME)/bin/javac"
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) \
		--enable-regex
	cp -pR rubypure rubypure19
	cd rubypure && $(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES)
	cd rubypure19 && $(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES)
	cd javapure && $(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES) \
		--libdir=\$${prefix}/share/hyperestraier
	$(MAKE)
	cp -pR rubynative rubynative19
	cd rubynative && $(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES)
	cd rubynative19 && $(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES)
ifeq (,$(findstring z$(DEB_BUILD_GNU_CPU)z,$(JAVA_UNSUPPORTED_CPUS)))
ifeq (,$(findstring z$(DEB_BUILD_GNU_SYSTEM)z,$(JAVA_UNSUPPORTED_SYSTEMS)))
	cd javanative && $(CONFIGURE_VARS) LIBS="../libestraier.a" ./configure $(CONFIGURE_SWITCHES)
endif
endif
	cd perlnative && $(CONFIGURE_VARS) ./configure $(CONFIGURE_SWITCHES)

#Architecture 
build: build-arch 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
	export RUBY=/usr/bin/ruby1.8 && cd rubynative && $(MAKE) && $(MAKE) check
	export RUBY=/usr/bin/ruby1.9.1 && cd rubynative19 && $(MAKE) && $(MAKE) check
	perl -p -i -e 's@^#! /usr/bin/ruby1\.8 -w@#! /usr/bin/ruby1.9.1 -w@' rubynative19/estcmd.rb
ifeq (,$(findstring z$(DEB_BUILD_GNU_CPU)z,$(JAVA_UNSUPPORTED_CPUS)))
ifeq (,$(findstring z$(DEB_BUILD_GNU_SYSTEM)z,$(JAVA_UNSUPPORTED_SYSTEMS)))
	cd javanative && $(MAKE) && $(MAKE) check
endif
endif
	cd perlnative/src && perl Makefile.PL INC="-I/usr/include/qdbm -I. -I../.. -I/usr/include/estraier" \
		OPTIMIZE="-O2 -g -Wall" LIBS="-L../.. -lestraier -lqdbm -lz -lm -lc" INSTALLDIRS=vendor && $(MAKE)
	cd perlnative && $(MAKE) check
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp:  config.status

	# Add here commands to compile the indep part of the package.
	#$(MAKE) doc
	export RUBY=/usr/bin/ruby1.8 && cd rubypure && $(MAKE)
	export RUBY=/usr/bin/ruby1.9.1 && cd rubypure19 && $(MAKE)
	perl -p -i -e 's@^#! /usr/bin/ruby1\.8 -w@#! /usr/bin/ruby1.9.1 -w@' rubypure19/estcall.rb
	cd javapure && $(MAKE)
	touch build-indep-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp #CONFIGURE-STAMP#
	rm -rf config.sub config.guess rubypure19 rubynative19
	rm -f rubypure/pure/est*.rb rubynative/native/est*.rb
	[ ! -d rubypure/pure ] || mv rubypure/pure rubypure/example
	[ ! -d rubynative/native ] || mv rubynative/native rubynative/example
	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	-cd rubypure && $(MAKE) distclean
	-cd rubynative && $(MAKE) distclean
	-cd javapure && $(MAKE) distclean
	-cd javanative && $(MAKE) distclean
	-cd perlnative && $(MAKE) distclean
	dh_clean 

install:
	dh_testdir
	dh_testroot
	dh_clean -k -s 
	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
	cd rubypure && $(MAKE) install \
		DESTDIR=$(CURDIR)/debian/libestraier-ruby1.8 \
		MYRBLIBDIR=$(RUBYLIBDIR)
	cd rubypure19 && $(MAKE) install \
		DESTDIR=$(CURDIR)/debian/libestraier-ruby1.9.1 \
		MYRBLIBDIR=$(RUBY19LIBDIR)
	cd rubynative && $(MAKE) install \
		DESTDIR=$(CURDIR)/debian/libestraier-ruby1.8 \
		MYRBLIBDIR=$(RUBYLIBDIR)
	cd rubynative19 && $(MAKE) install \
		DESTDIR=$(CURDIR)/debian/libestraier-ruby1.9.1 \
		MYRBLIBDIR=$(RUBY19LIBDIR)

	# create example directory tree
	mv $(CURDIR)/debian/libestraier-ruby1.8/usr/bin/estcmd.rb rubynative/example/
	mv $(CURDIR)/debian/libestraier-ruby1.9.1/usr/bin/estcmd.rb rubynative19/example/
	mv $(CURDIR)/debian/libestraier-ruby1.8/usr/bin/estcall.rb rubypure/example/
	mv $(CURDIR)/debian/libestraier-ruby1.9.1/usr/bin/estcall.rb rubypure19/example/
	mv rubynative/example rubynative/native
	mv rubypure/example rubypure/pure
	mv rubynative19/example rubynative19/native
	mv rubypure19/example rubypure19/pure

	install -m 644 rubynative/estraier-doc.rb \
		$(CURDIR)/debian/libestraier-ruby1.8/$(RUBYLIBDIR)/
	install -m 644 rubynative19/estraier-doc.rb \
		$(CURDIR)/debian/libestraier-ruby1.9.1/$(RUBY19LIBDIR)/
	mkdir -p $(CURDIR)/debian/libestraier-ruby1.8/$(RUBYARCHDIR)
	mkdir -p $(CURDIR)/debian/libestraier-ruby1.9.1/$(RUBY19ARCHDIR)
	install -m 644 rubynative/src/estraier.so \
		$(CURDIR)/debian/libestraier-ruby1.8/$(RUBYARCHDIR)/
	install -m 644 rubynative19/src/estraier.so \
		$(CURDIR)/debian/libestraier-ruby1.9.1/$(RUBY19ARCHDIR)/
	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/
ifeq (,$(findstring z$(DEB_BUILD_GNU_CPU)z,$(JAVA_UNSUPPORTED_CPUS)))
ifeq (,$(findstring z$(DEB_BUILD_GNU_SYSTEM)z,$(JAVA_UNSUPPORTED_SYSTEMS)))
	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
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

# 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
ifneq (,$(findstring z$(DEB_BUILD_GNU_CPU)z,$(JAVA_UNSUPPORTED_CPUS)))
ifneq (,$(findstring z$(DEB_BUILD_GNU_SYSTEM)z,$(JAVA_UNSUPPORTED_SYSTEMS)))
	-rm -f $(CURDIR)/debian/libestraier-java/usr/share/java/estraier.jar
endif
endif
	dh_fixperms
	dh_strip
	dh_compress 
	dh_perl
	dh_makeshlibs -a --no-package=libestraier-java -V
	dh_installdeb
	dh_shlibdeps -l debian/libestraier8/usr/lib
	dh_gencontrol -a
	dh_lintian -a
	dh_md5sums -a
	dh_builddeb -a

# Build architecture independant packages using the common target.
binary-indep: build-indep install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install
	$(MAKE) -f debian/rules binary-common
#	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

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