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

export HOME := $(CURDIR)/debian

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

DESTDIR=$(CURDIR)/debian/yaws
DESTDIR1=$(CURDIR)/debian/erlang-yaws
TMPDIR=$(CURDIR)/debian/tmp

CFLAGS = -Wall -g

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

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	TYPE = debug
endif

%:
	dh $@ --with autotools_dev

override_dh_auto_clean:
	dh_testdir
	dh_testroot
	#
	# Clean up applications included into yaws distribution
	[ ! -f include.mk ] || \
	for appdir in applications/* ; do \
	    (cd $$appdir ; \
	     $(MAKE) clean || exit 1 ; \
	     $(MAKE) cleandocs || exit 1) \
	done
	rm -rf applications/chat/ebin
	#
	# Clean up after the build process.
	[ ! -f include.mk ] || $(MAKE) clean
	[ ! -f include.mk ] || $(MAKE) conf_clean
	rm -f priv/charset.def
	#
	rm -f erl_crash.dump
	rm -f debian/yaws-*.postinst debian/yaws-*.postrm

override_dh_auto_configure:
	dh_testdir
	#
	CFLAGS="$(CFLAGS)" \
	./configure --host=$(DEB_HOST_GNU_TYPE) \
		    --build=$(DEB_BUILD_GNU_TYPE) \
		    --prefix=/usr \
		    --mandir=/usr/share/man \
		    --infodir=/usr/share/info \
		    --sysconfdir=/etc \
		    --localstatedir=/var

override_dh_auto_build-arch:
	dh_testdir
	#
	# Compile the package.
	mkdir -p examples/ebin
	TYPE="$(TYPE)" $(MAKE) DLL_LIBS=-lc

override_dh_auto_build-indep:
	dh_testdir
	#
	# Build applications included into yaws distribution
	for appdir in applications/* ; do \
	    (cd $$appdir ; \
	     mkdir -p ebin ; \
	     TYPE="$(TYPE)" $(MAKE) || exit 1) \
	done
	#
	# Build documentation
	$(MAKE) docs

override_dh_auto_test:
	# Do nothing because the tests require downloading some tools from
	# the Internet.

override_dh_auto_install-arch:
	dh_testdir
	#
	# Install the package into debian/yaws.
	$(MAKE) install DESTDIR=$(TMPDIR)
	#
	# Fix location of epam binary
	mv $(TMPDIR)/usr/lib/yaws/priv/lib/epam $(TMPDIR)/usr/lib/yaws/priv/
	#
	# Fix permissions on examples
	chmod a-x $(TMPDIR)/var/yaws/www/testdir/index.html
	#
	# Remove duplicated headers
	rm -f $(TMPDIR)/usr/lib/yaws/*.hrl
	#
	# Remove useless .empty files and .xvpics directory
	find $(TMPDIR) -type f -name '.*' -print -exec rm -rf \{\} \;
	#
	# Remove empty directories
	rm -rf $(TMPDIR)/usr/lib/yaws/examples/
	#
	dh_install -a

override_dh_auto_install-indep:
	dh_testdir
	#
	# Install applications (and READMEs) included into yaws distribution
	# Application beams go to /usr/lib/yaws/$application/ebin
	# Other application files go to /usr/share/yaws-$application/docroot
	for appdir in applications/* ; do \
	    (cd $$appdir ; \
	     app=`basename $$appdir` ; \
	     $(MAKE) install DESTDIR=$(DESTDIR)-$$app \
	     		     EBININSTALLDIR=$(DESTDIR)-$$app/usr/lib/yaws/$$app/ebin \
	     		     CHATINSTALLDIR=$(DESTDIR)-chat/usr/share/yaws-chat \
	     		     MAILINSTALLDIR=$(DESTDIR)-mail/usr/share/yaws-mail \
	     		     WIKIINSTALLDIR=$(DESTDIR)-wiki/var/lib/yaws-wiki || exit 1) \
	done
	#
	# Fix permissions of application files
	for appdir in applications/* ; do \
	    app=`basename $$appdir` ; \
	    find $(DESTDIR)-$$app -type f -exec chmod a-x \{\} \; ; \
	done
	#
	# Remove useless empty files
	for appdir in applications/* ; do \
	    app=`basename $$appdir` ; \
	    find $(DESTDIR)-$$app -type f -name '.*' -print -exec rm -f \{\} \; ; \
	done
	#
	# Generate postinst and postrm scripts for applications
	for appdir in applications/* ; do \
	    app=`basename $$appdir` ; \
	    sed -e "s/@APPLICATION@/$$app/g" \
	    	debian/yaws-application.postinst.in >debian/yaws-$$app.postinst || exit 1; \
	    sed -e "s/@APPLICATION@/$$app/g" \
	    	debian/yaws-application.postrm.in >debian/yaws-$$app.postrm || exit 1 ; \
	done
	#
	dh_install -i

override_dh_compress:
	dh_compress -X.erl -X.beam -X.yaws -X.pdf -X .ps -X .eps

override_dh_gencontrol:
	erlang-depends
	dh_gencontrol

v=1.98

get-orig-source:
	wget -O - http://yaws.hyber.org/download/yaws-$(v).tar.gz | tar zx
	tar zcf yaws_$(v).orig.tar.gz yaws-$(v)
	rm -r yaws-$(v)
	rm yaws

.PHONY: override_dh_auto_clean override_dh_auto_configure override_dh_auto_build-arch \
	override_dh_auto_build-indep override_dh_auto_test override_dh_auto_install-arch \
	override_dh_auto_install-indep override_dh_compress override_dh_gencontrol \
	get-orig-source
