#!/usr/bin/make -f

PACKAGE=geneweb

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Hack to workaround #335578 and geneweb not buildable on alpha
# as long as ocaml is not fixed
# We temporarily build using bytecode on alpha
ifeq ($(DEB_HOST_ARCH),alpha)
        WAY=out
else
        WAY=$(shell if [ -f "/usr/bin/ocamlc.opt" ]; then echo "opt";           \
        else echo "out";                                                \
        fi)
endif

configure:
	dh_testdir


build: configure
	dh_testdir
	bash -n debian/geneweb.postinst
	bash -n debian/geneweb.postrm
	bash -n debian/geneweb.preinst
	bash -n debian/geneweb.prerm
	bash -n debian/geneweb.init
	dh_installdirs -A

	# Move the proper makefile into place	
	cp -f `pwd`/tools/Makefile.inc.$(WAY) `pwd`/tools/Makefile.inc

	$(MAKE) $(WAY)

	$(MAKE) distrib

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp configure-stamp
	rm -rf debian/iso-codes
	rm -rf debian/pobuild
	rm -f debian/geneweb.templates.tmp
	rm -f debian/geneweb.templates

	debconf-updatepo

	# Add here commands to clean up after the build process.
	[ ! -f tools/Makefile.inc ] || [ ! -f Makefile ] || $(MAKE) clean

	# Clean out cruft left by former builds
	rm -f tools/Makefile.inc
	rm -f compilation.ml

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/$(PACKAGE).
	dh_install


	# Doc files go to the appropriate directory
	install -m 644 `pwd`/CHANGES \
	        `pwd`/debian/geneweb/usr/share/doc/geneweb/changelog
	# The CGI script
	chmod 755 `pwd`/debian/gwtp/usr/lib/cgi-bin/gwtp/gwtp.cgi
	# The wrappers
	chmod 755 `pwd`/debian/gwsetup/usr/lib/geneweb/gwsetup.wrapper

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: DH_OPTIONS=-i
binary-indep: build install

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.
binary-arch: DH_OPTIONS=-a
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_link
	dh_installdocs
	# License files should not be duplicated
	rm `pwd`/debian/geneweb/usr/share/doc/geneweb/LICENSE.htm || true

	dh_installmenu
	# At least until dh_installinit has a safe option
	# to avoid installing links for runlevels 0 and 6
	dh_installinit -- start 20 2 3 4 5 . stop 20 1 .
	dh_installman
	dh_installcron
	dh_installexamples
	dh_installlogrotate
	# gwtp does not have any manpage
	dh_installman -pgeneweb

	# Grab ISO 639 language code translations
	chmod u+x debian/get-iso-codes
	debian/get-iso-codes
	# Build templates
	chmod u+x debian/mktemplates
	debian/mktemplates

	dh_installdebconf
	dh_installchangelogs
	dh_strip
	dh_compress
	dh_fixperms
	# Use of the dh-buildinfo package: see http://people.debian.org/~dirson/buildinfo/
	dh_buildinfo
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol

	dh_md5sums
	dh_builddeb

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