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

LMSHAREDIR=/usr/share/lemonldap-ng
LMVARDIR=/var/lib/lemonldap-ng

configure: configure-stamp
configure-stamp:
	dh_testdir

	# Add here commands to configure the package.
	$(MAKE) configure STORAGECONFFILE=/etc/lemonldap-ng/lemonldap-ng.ini \
			PERLOPTIONS="INSTALLDIRS=vendor"

	touch configure-stamp


build: build-stamp

build-indep: build-stamp

build-arch:

build-stamp: configure-stamp 
	dh_testdir

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

	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_prep
	dh_installdirs

	# Add here commands to install the package into debian/lemonldap-ng.
	$(MAKE) test
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp \
			PREFIX=/usr/ \
			LMPREFIX=/usr/share/lemonldap-ng/ \
			BINDIR=$(LMSHAREDIR)/bin/ \
			DOCUMENTROOT=$(LMVARDIR) \
			EXAMPLESDIR=/examples/ \
			HANDLERDIR=$(LMVARDIR)/handler/ \
			PORTALSKINSDIR=$(LMSHAREDIR)/portal-skins/ \
			MANAGERSKINSDIR=$(LMSHAREDIR)/manager-skins/ \
			SESSIONSEXPLORERDATADIR=$(LMSHAREDIR)/sessions-explorer-imgs/ \
			STORAGECONFFILE=/etc/lemonldap-ng/lemonldap-ng.ini \
			TOOLSDIR=$(LMSHAREDIR)/ressources/ \
			CONFDIR=/etc/lemonldap-ng/ \
			CRONDIR=/etc/cron.d/ \
			DATADIR=$(LMVARDIR) \
			APACHEUSER=www-data \
			APACHEGROUP=www-data \
			DOCDIR=/usr/share/doc/lemonldap-ng-doc

	# Since Lenny, jquery.js is provided by libjs-jquery
	find $(CURDIR)/debian/tmp -type f -regex '.*/jquery-[0-9].*\.js' -delete
	find $(CURDIR)/debian/tmp -type f -name jquery.js -delete
	rm -f $(CURDIR)/debian/tmp$(LMSHAREDIR)/*-skins/*/jquery.js

	# TODO: uncomment this when Debian jquery-ui version will be upgraded to 1.8.6
	#find $(CURDIR)/debian/tmp -type f -regex '.*/jquery-ui-[0-9].*\.js' -delete
	#find $(CURDIR)/debian/tmp -type f -name jquery-ui.js -delete
	#rm -f $(CURDIR)/debian/tmp$(LMSHAREDIR)/*-skins/*/jquery-ui.js
	# TODO: restore this in Perl (with final \ )
	#	s#src=(["'"'"']).*?jquery-ui(-\d[\.\w\-]*?)?.js#src=$$1/javascript/jquery-ui/jquery-ui.min.js#i;'
	perl -i -pe 's#src=(["'"'"']).*?jquery(-\d[\.\w\-]*?)?.js#src=$$1/javascript/jquery/jquery.min.js#i;' \
		$$(find $(CURDIR)/debian/tmp/examples/manager/ -name *.pl -type f) \
		$$(find	$(CURDIR)/debian/tmp$(LMSHAREDIR)/manager-skins/*/ \
			$(CURDIR)/debian/tmp$(LMSHAREDIR)/portal-skins/*/ -type f -name *.tpl)

	# TODO: uncomment this for official releases
	#test -n "$$LOCALBUILD" || ./scripts/minifierjs $$(find debian/tmp/ -name '*.js')
	#test -n "$$LOCALBUILD" || ./scripts/minifiercss $$(find debian/tmp/ -name '*.css')

	# Move perl scripts in /usr/share, links are created by *.postinst scripts
	mkdir debian/tmp/usr/share/lemonldap-ng/manager debian/tmp/usr/share/lemonldap-ng/portal
	mv debian/tmp/var/lib/lemonldap-ng/portal/*.pl debian/tmp/usr/share/lemonldap-ng/portal/
	mv debian/tmp/var/lib/lemonldap-ng/manager/*pl debian/tmp/usr/share/lemonldap-ng/manager/

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs changelog
	dh_installdocs
	dh_installexamples
	dh_install
	mkdir -p debian/lemonldap-ng/usr/share/doc/lemonldap-ng/examples && \
		cd debian/lemonldap-ng/usr/share/doc/lemonldap-ng/examples && \
		ln -s ../../liblemonldap-ng-handler-perl/examples handler && \
		ln -s ../../liblemonldap-ng-manager-perl/examples manager && \
		ln -s ../../liblemonldap-ng-portal-perl/examples portal
	chmod +x debian/liblemonldap-ng-portal-perl$(LMSHAREDIR)/bin/purgeCentralCache
	chmod +x debian/liblemonldap-ng-handler-perl$(LMSHAREDIR)/bin/purgeLocalCache
#	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 -X.pm -X.pl
	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
