#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

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

WEB_ADMIN_DEST = $(CURDIR)/debian/mlmmj-php-web-admin/usr/share/mlmmj-php-web-admin
WEB_ADMIN_SRC = $(CURDIR)/contrib/web/php-admin

USER_ADMIN_SRC = $(CURDIR)/contrib/web/php-user

CONFFLAGS := --prefix=/usr --mandir=/usr/share/man
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	CONFFLAGS += --build=$(DEB_BUILD_GNU_TYPE)
else
	CONFFLAGS += --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	./configure $(CONFFLAGS) $(shell dpkg-buildflags --export=configure)
	touch configure-stamp

build: build-arch build-indep

build-arch: build-arch-stamp

build-indep: build-indep-stamp

build-arch-stamp: configure-stamp
	dh_testdir
	$(MAKE)
	touch $@

build-indep-stamp:

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f build-stamp configure-stamp config.log
	dh_clean

install: install-indep install-arch

install-arch:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -a
	# Add here commands to install the package into debian/mlmmj.
	$(MAKE) DESTDIR=$(CURDIR)/debian/mlmmj install
	rm $(CURDIR)/debian/mlmmj/usr/bin/mlmmj-make-ml.sh
	install -m 755 -D $(CURDIR)/debian/mlmmj-make-ml.Debian \
		$(CURDIR)/debian/mlmmj/usr/share/doc/mlmmj/mlmmj-make-ml.Debian
	dh_install -a

install-indep:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -i
	dh_install -i

	# mlmmj-php-web
	install -m 644 -D $(USER_ADMIN_SRC)/mlmmj.php	$(CURDIR)/debian/mlmmj-php-web/usr/share/mlmmj-php-web/mlmmj.php
	install -m 644 -D $(USER_ADMIN_SRC)/example.html $(CURDIR)/debian/mlmmj-php-web/usr/share/mlmmj-php-web/index.html
	install -m 644 -D $(USER_ADMIN_SRC)/README	$(CURDIR)/debian/mlmmj-php-web/usr/share/doc/mlmmj-php-web/README
	install -m 644 -D $(USER_ADMIN_SRC)/example.html $(CURDIR)/debian/mlmmj-php-web/usr/share/doc/mlmmj-php-web/examples/examples.html

	### mlmmj-php-web-admin ###
	# Create dirs
	install -d $(WEB_ADMIN_DEST)/htdocs
	install -d $(CURDIR)/debian/mlmmj-php-web-admin/etc/mlmmj-php-web-admin
	install -d $(CURDIR)/debian/mlmmj-php-web-admin/etc/mlmmj-php-web-admin/templates
	# Copy the content
	install -m 644 $(WEB_ADMIN_SRC)/htdocs/*.php		$(WEB_ADMIN_DEST)/htdocs
	install -m 644 $(WEB_ADMIN_SRC)/htdocs/dot.htaccess	$(WEB_ADMIN_DEST)/htdocs/.htaccess
	install -m 644 -D $(WEB_ADMIN_SRC)/README		$(CURDIR)/debian/mlmmj-php-web-admin/usr/share/doc/mlmmj-php-web-admin/README
	install -m 644 $(WEB_ADMIN_SRC)/conf/*			$(CURDIR)/debian/mlmmj-php-web-admin/etc/mlmmj-php-web-admin
	install -m 644 $(WEB_ADMIN_SRC)/templates/*		$(CURDIR)/debian/mlmmj-php-web-admin/etc/mlmmj-php-web-admin/templates
	# Make sure we're using /etc for conf files
	ln -s /etc/mlmmj-php-web-admin				$(CURDIR)/debian/mlmmj-php-web-admin/usr/share/mlmmj-php-web-admin/conf
	ln -s /etc/mlmmj-php-web-admin/templates		$(CURDIR)/debian/mlmmj-php-web-admin/usr/share/mlmmj-php-web-admin/templates

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_installdebconf
	dh_installcron
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb


DEBVERS         ?= $(shell dpkg-parsechangelog -SVersion)
VERSION         ?= $(shell echo '$(DEBVERS)' | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//')
DEBPKGNAME      ?= $(shell dpkg-parsechangelog -SSource)
GIT_TAG         ?= $(shell echo '$(VERSION)' | sed -e 's/~/_/')

gen-orig-xz:
	if [ ! -f ../$(DEBPKGNAME)_$(VERSION).orig.tar.xz ] ; then \
		git archive --prefix=$(DEBPKGNAME)-$(VERSION)/ $(GIT_TAG) | xz >../$(DEBPKGNAME)_$(VERSION).orig.tar.xz ; \
	fi
	[ ! -e ../build-area ] && mkdir ../build-area || true
	[ ! -e ../build-area/$(DEBPKGNAME)_$(VERSION).orig.tar.xz ] && cp ../$(DEBPKGNAME)_$(VERSION).orig.tar.xz ../build-area

binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

binary: binary-indep binary-arch

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