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

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


# 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_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(shell dpkg-vendor --query vendor), Ubuntu)
DEB_DH_INSTALLINIT_ARGS := --upstart-only
else
DEB_DH_INSTALLINIT_ARGS := -u'start 50 2 3 4 5 . stop 80 0 1 6 .'
endif

CFLAGS = -Wall -g

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

# Include dpatch stuff.
#include /usr/share/dpatch/dpatch.make

config.status: configure
#	dh_testdir
	# Add here commands to configure the package.
	[ -f libstemmer_c.tgz ] || wget -c http://snowball.tartarus.org/dist/libstemmer_c.tgz
	tar -zcf libstemmer_old.tar.gz libstemmer_c/
	tar -zxf libstemmer_c.tgz
	tar -zxf re2.tgz
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --localstatedir=/var/lib/sphinxsearch --sysconfdir=/etc/sphinxsearch --with-mysql --with-pgsql --enable-id64 --with-libstemmer --with-re2 --with-unixodbc --with-syslog CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs  -pthread"

build: build-stamp

build-stamp:  config.status
	dh_testdir
	$(MAKE)
	touch build-stamp

clean: clean1 unpatch
clean1:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f Makefile ] || $(MAKE) distclean
	[ ! -f libstemmer_old.tar.gz ] || ( rm -rf libstemmer_c/; tar -zxf libstemmer_old.tar.gz;  rm libstemmer_old.tar.gz )
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/sphinxsearch.
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
 
	# Copy a default configuration file
	cp $(CURDIR)/debian/tmp/etc/sphinxsearch/sphinx.conf.dist $(CURDIR)/debian/sphinxsearch/etc/sphinxsearch/sphinx.conf

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_install
	dh_installcron
	dh_installdebconf
	dh_installinit
	dh_installman
	dh_link
	dh_strip --dbg-package=sphinxsearch-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs 
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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