#!/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)
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

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

-include $(CURDIR)/vsn.mk

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

%:
	dh $@

override_dh_auto_clean:
	dh_testdir
	dh_testroot
	#
	# Clean up applications included into yaws distribution
	[ ! -f Makefile ] || \
	for appdir in applications/* ; do \
	    (cd $$appdir ; \
	     $(MAKE) distclean || exit 1) \
	done
	#
	# Clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf ac-aux
	rm -rf c_src/.deps
	rm -rf win32/.deps
	rm -rf ebin
	rm -f priv/charset.def
	#
	rm -f erl_crash.dump
	rm -f debian/yaws-*.postinst debian/yaws-*.postrm
	rm -f debian/erlang-yaws.maintscript
	rm -f *.stamp

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 \
		    --disable-silent-rules

build-arch.stamp:
	dh_testdir
	#
	# Compile the package.
	TYPE="$(TYPE)" $(MAKE) DLL_LIBS=-lc
	#
	# Compile the Yapp application
	cd applications/yapp ; TYPE="$(TYPE)" $(MAKE)
	touch build-arch.stamp

override_dh_auto_build-arch: build-arch.stamp

override_dh_auto_build-indep: build-arch.stamp
	dh_testdir
	#
	# Build applications included into yaws distribution
	for appdir in chat mail wiki ; do \
	    (cd applications/$$appdir ; TYPE="$(TYPE)" $(MAKE) || exit 1) \
	done
	#
	# Build documentation
	$(MAKE) doc

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

install-arch.stamp:
	dh_testdir
	#
	# Install the package into debian/tmp.
	$(MAKE) install DESTDIR=$(TMPDIR)
	#
	# Install the yapp application
	cd applications/yapp ; $(MAKE) install DESTDIR=$(TMPDIR)
	#
	# Relocate pkgconfig script
	install -m 755 -d $(TMPDIR)/usr/lib/$(DEB_HOST_MULTIARCH)
	mv $(TMPDIR)/usr/lib/pkgconfig $(TMPDIR)/usr/lib/$(DEB_HOST_MULTIARCH)
	#
	# Relocate examples
	mv $(TMPDIR)/usr/lib/yaws-*/examples $(TMPDIR)/var/yaws/
	mv $(TMPDIR)/usr/lib/yapp-*/examples $(TMPDIR)/var/yaws/examples/yapp
	mv $(TMPDIR)/usr/lib/yapp-*/priv $(TMPDIR)/var/yaws/yapp-priv
	mv $(TMPDIR)/usr/lib/yapp-*/doc $(TMPDIR)/var/yaws/yapp-doc
	#
	# Remove unneeded .la files
	rm -vf $(TMPDIR)/usr/lib/yaws-*/priv/lib/*.la
	#
	sed -e "s/@YAWS_VSN@/$(YAWS_VSN)/g" \
	    debian/erlang-yaws.maintscript.in >debian/erlang-yaws.maintscript || exit 1
	#
	dh_install -a
	touch install-arch.stamp

override_dh_auto_install-arch: install-arch.stamp

override_dh_auto_install-indep: install-arch.stamp
	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 app in chat mail wiki ; do \
	    (cd applications/$$app ; $(MAKE) install DESTDIR=$(TMPDIR) || exit 1) \
	done
	#
	# Fix permissions on examples
	chmod a-x $(TMPDIR)/var/yaws/www/testdir/index.html
	#
	# Fix run_erl and to_erl paths.
	sed -i -e 's:/lib/erlang/erts-[0-9.]*/:/:g' $(TMPDIR)/usr/bin/yaws
	#
	# Fix permissions of application files
	find $(TMPDIR)/var/yaws -type f -exec chmod a-x \{\} \;
	#
	# Generate postinst and postrm scripts for applications
	for app in chat mail wiki yapp ; do \
	    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_usrlocal:
	dh_usrlocal
	cat debian/yaws.postinst.custom >>debian/yaws.postinst.debhelper

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

override_dh_gencontrol:
	erlang-depends
	dh_gencontrol

.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_usrlocal override_dh_compress \
	override_dh_gencontrol
