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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export PERL_MM_USE_DEFAULT=1

LM_ROOT=/var/lib/lemonldap-ng/
DEB_TMP_LM_ROOT=debian/tmp$(LM_ROOT)

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp 
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) INSTALLDIRS=vendor

	touch $@

clean:
	dh_testdir
	dh_testroot
	debconf-updatepo
	rm -f build-stamp configure-stamp

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

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/lemonldap-ng.
	$(MAKE) test
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp PREFIX=/usr
	$(MAKE) example \
		EXAMPLEDIRBUILD=$(DEB_TMP_LM_ROOT) \
		EXAMPLEDIR=$(LM_ROOT) \
		EXAMPLECONFDIR=$(LM_ROOT)conf/ \
		EXAMPLELASPDIRBUILD=$(DEB_TMP_LM_ROOT)liberty-alliance-sp-portal/ \
		EXAMPLELASPDIR=$(LM_ROOT)liberty-alliance-sp-portal/

	# Debian example files
	cp $(DEB_TMP_LM_ROOT)handler/MyHandler.pm $(DEB_TMP_LM_ROOT)handler/MyHandler-Debian.pm
	cp $(DEB_TMP_LM_ROOT)portal/index.pl $(DEB_TMP_LM_ROOT)portal/index-Debian.pl
	cp $(DEB_TMP_LM_ROOT)portal/index_skin.pl $(DEB_TMP_LM_ROOT)portal/index_skin-Debian.pl
	cp $(DEB_TMP_LM_ROOT)liberty-alliance-sp-portal/index.pl $(DEB_TMP_LM_ROOT)liberty-alliance-sp-portal/index-Debian.pl
	cp $(DEB_TMP_LM_ROOT)manager/index.pl $(DEB_TMP_LM_ROOT)manager/index-Debian.pl
	mv $(DEB_TMP_LM_ROOT)index.pl $(DEB_TMP_LM_ROOT)test.pl
	mv $(DEB_TMP_LM_ROOT)handler/lmH-apache.conf $(DEB_TMP_LM_ROOT)apache-protected-area.conf
	mv $(DEB_TMP_LM_ROOT)handler/lmH-apache2.conf $(DEB_TMP_LM_ROOT)apache2-protected-area.conf
	perl -i -pe 's#__DIR__/?#'/var/lib/lemonldap-ng'#g;s#__CONFDIR__/?#'$(LM_ROOT)conf'#g;' \
		$(DEB_TMP_LM_ROOT)apache*-protected-area.conf

	# System to use /etc/lemonldap-ng/storage.conf instead of setting
	# manually the configStorage parameter
	perl -000 -i -pe 's/(Lemonldap::NG::Manager::Conf.*new\s*\(\s*){[^}]+}/$$1\$$Lemonldap::NG::Conf::configStorage/; s#^use#\nrequire "/usr/share/lemonldap-ng/configStorage.pm";\nuse#m;' \
			$(DEB_TMP_LM_ROOT)manager/scripts/lmConfigEditor \
			$(DEB_TMP_LM_ROOT)portal/scripts/purgeCentralCache

	perl -000 -i -pe 's/(configStorage\s*=>\s*){[^}]+}/$$1\$$Lemonldap::NG::Conf::configStorage/; s#^use#\nrequire "/usr/share/lemonldap-ng/configStorage.pm";\nuse#m;' \
			$(DEB_TMP_LM_ROOT)handler/MyHandler-Debian.pm \
			$(DEB_TMP_LM_ROOT)manager/index-Debian.pl \
			$(DEB_TMP_LM_ROOT)portal/index-Debian.pl \
			$(DEB_TMP_LM_ROOT)portal/index_skin-Debian.pl \
			$(DEB_TMP_LM_ROOT)liberty-alliance-sp-portal/index-Debian.pl
# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs changelog
	dh_installdocs
	mkdir $(DEB_TMP_LM_ROOT)protected
	find debian/tmp/var/lib/lemonldap-ng -type f -perm /111 -exec chmod -x {} \;
	chmod +x $(DEB_TMP_LM_ROOT)manager/scripts/lmConfigEditor
	find debian/tmp/var/lib/lemonldap-ng -type f -name *.pl -exec chmod +x {} \;
	dh_installexamples
	dh_install
	chmod +x debian/liblemonldap-ng-portal-perl/usr/share/lemonldap-ng/bin/purgeCentralCache
#	dh_installmenu
	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_python
#	dh_installinit
	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: build install

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