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

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

export DEB_HOST_GNU_TYPE
export DEB_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

%:
	dh $@

override_dh_pysupport:
	dh_pysupport /usr/share/cherokee/

override_dh_install:
	dh_install
	# We avoid manually stating each of the files to be included
	# in libcherokee-server0 by copying over to them all the
	# corresponding (*.so) files we have in
	# $(BUILD_DIR)/usr/lib/cherokee that have not been copied over
	# to any of the plugin packages (libcherokee-mod-*) or to
	# cherokee-admin (which is after all a plugin by a different name).
	INSTALLED=`mktemp`; \
		DEST=$(CURDIR)/debian/libcherokee-server0/usr/lib/cherokee; \
		ls $(CURDIR)/debian/libcherokee-mod*/usr/lib/cherokee/* \
		   $(CURDIR)/debian/cherokee-admin/usr/lib/cherokee/*  | \
			sed 's/.*\///' > $$INSTALLED; \
		install $$(ls $(BUILD_DIR)/usr/lib/cherokee/*.so | \
			   egrep -vf $$INSTALLED ) $$DEST;\
		rm -f $$INSTALLED
	# Include our nice "Powered by Debian" logo alongside
	# Cherokee's own
	install -m 0644 $(CURDIR)/debian/powered_by_debian.png $(CURDIR)/debian/cherokee/usr/share/cherokee/default-site/cherokee-images/powered_by_debian.png
	# Source files for JQuery/JQuery-ui are not needed (minimized
	# versions are shipped)
	rm -f $(CURDIR)/debian/cherokee-admin/usr/share/cherokee/admin/CTK/static/js/jquery-1.3.2.js \
		$(CURDIR)/debian/cherokee-admin/usr/share/cherokee/admin/CTK/static/js/jquery-ui-1.7.2.js

override_dh_auto_configure:
	dh_auto_configure -- --host=$(DEB_HOST_GNU_TYPE)         \
			   --build=$(DEB_BUILD_GNU_TYPE)         \
			   --enable-os-string="Debian GNU/Linux" \
			   --prefix=/usr                         \
			   --localstatedir=/var                  \
			   --mandir=/usr/share/man               \
			   --infodir=/usr/share/info             \
			   --sysconfdir=/etc                     \
	                   --docdir=/usr/share/doc/cherokee-doc  \
			   --with-wwwroot=/var/www               \
	 		   --enable-pthread

clean:
	dh clean --before auto_clean
	# the distclean target in the doc directory seems broken, replace the 
	# makefile with a dummy so it's broken distclean target doesn't get run
	echo distclean: > doc/Makefile
	[ ! -f Makefile ] || $(MAKE) distclean
	# Deal with several files that should be cleaned but are left
	# around
	rm -fr cherokee/mime_grammar.output \
		cherokee/icons_grammar.output \
		cherokee/config_protocol_grammar.output \
		www/cherokee-images \
		cherokee/loader.autoconf.h \
		cherokee/loader.autoconf.inc \
		cherokee/Makefile.MingW \
		cherokee.spec \
		windows/cherokee.nsi \
		config.log \
		$(BUILD_DIR)
	dh_clean
