#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# 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

INSTALL_PROGRAM = /usr/bin/install
BUILD_DIR = $(CURDIR)/debian/tmp
#BIN_DIR = $(CURDIR)/debian/cherokee
LIB_DIR = $(CURDIR)/debian/libcherokee0
DEV_DIR = $(CURDIR)/debian/libcherokee0-dev
BENCH_DIR = $(CURDIR)/debian/cherokee-bench

# 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)

# Compiler flags to use
CFLAGS = -Wall -g

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

config.status: configure
	dh_testdir
	# Add here commands to configure the package
	./configure  --host=$(DEB_HOST_GNU_TYPE)      \
			   --build=$(DEB_BUILD_GNU_TYPE)    \
			   --prefix=/usr                    \
			   --localstatedir=/var             \
			   --mandir=\$${prefix}/share/man   \
			   --infodir=\$${prefix}/share/info \
			   --sysconfdir=/etc                \
			   --disable-epoll                  \
	 		   --enable-pthreads

#Architecture
build: build-arch build-indep

build-arch: build-arch-stamp

build-arch-stamp: config.status
	$(MAKE)
	$(MAKE) test
	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

clean: 
	dh_testdir
	dh_testroot
	-$(MAKE) -i distclean
	rm -rf build-arch-stamp build-indep-stamp 
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
	dh_clean

install: install-indep install-arch

install-indep:
######	dh_testdir
######	dh_testroot
######	dh_clean -k -i
######	dh_installdirs -i

	# Add here commands to install the indep part of the package into
	# debian/<package>-doc
	#INSTALLDOC#

######	dh_install -i

install-arch: 
	dh_testdir
	dh_testroot
	dh_clean -k -s
	dh_installdirs -s

	install -d $(BUILD_DIR) 	

	$(MAKE) install INSTALL_PROGRAM=$(INSTALL_PROGRAM) CFLAGS="$(CFLAGS)" DESTDIR=$(BUILD_DIR)

# cherokee - Main binary package
	# The main Cherokee package is built in $(BUILD_DIR) - We need only to
	# remove from it everything that does not belong in there. Here we just
	# delete the files that are built but not needed, and on the following
	# lines we will move everything belonging to the different generated
	# binary packages to its place
	rm $(BUILD_DIR)/etc/cherokee/*sample

# libcherokee0-dev - Development files
	install -d $(DEV_DIR)/DEBIAN
	install -d $(DEV_DIR)/usr/{bin,lib/pkgconfig,include/cherokee}
	install -d $(DEV_DIR)/usr/share/{doc/libcherokee0-dev,aclocal,man/man1}

	mv $(BUILD_DIR)/usr/lib/libcherokee.{a,so}         $(DEV_DIR)/usr/lib/
	mv $(BUILD_DIR)/usr/include/cherokee/*             $(DEV_DIR)/usr/include/cherokee/
	mv $(BUILD_DIR)/usr/bin/cherokee-config            $(DEV_DIR)/usr/bin
	mv $(BUILD_DIR)/usr/share/man/man1/cherokee-config.1 $(DEV_DIR)/usr/share/man/man1/
	mv $(BUILD_DIR)/usr/lib/pkgconfig/*                $(DEV_DIR)/usr/lib/pkgconfig/
	mv $(BUILD_DIR)/usr/share/aclocal/*                $(DEV_DIR)/usr/share/aclocal/

# libcherokee0 - Runtime library
	install -d $(LIB_DIR)/DEBIAN
	install -d $(LIB_DIR)/usr/lib/{pkgconfig,cherokee}
	install -d $(LIB_DIR)/usr/share/{cherokee,aclocal,doc/libcherokee0}

	mv $(BUILD_DIR)/usr/lib/libcherokee*.so*           $(LIB_DIR)/usr/lib/
	mv $(BUILD_DIR)/usr/lib/cherokee/libcherokee_*.so* $(LIB_DIR)/usr/lib/cherokee/

# cherokee-bench - Benchmark utility
	install -d $(BENCH_DIR)/DEBIAN
	install -d $(BENCH_DIR)/usr/{bin,share/doc/cherokee-bench}

	mv $(BUILD_DIR)/usr/bin/cherokee-bench             $(BENCH_DIR)/usr/bin/

# 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 AUTHORS
#	dh_installexamples
#	dh_installmenu
#	dh_installdebconf
	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
	dh_installdeb
	dh_makeshlibs
	dh_shlibdeps -L libcherokee0 -L libcherokee0-dev -L cherokee-bench -L cherokee -l debian/libcherokee0/usr/lib:debian/libcherokee0/usr/lib/cherokee
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(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
