#!/usr/bin/make -f

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

LMSHAREDIR=/usr/share/lemonldap-ng
LMVARDIR  =/var/lib/lemonldap-ng
TMP       = $(CURDIR)/debian/tmp

CONFDIR=/etc/lemonldap-ng
SESSIONSDIR=$(LMVARDIR)/sessions
PSESSIONSDIR=$(LMVARDIR)/psessions
NOTIFICATIONSDIR=$(LMVARDIR)/notifications
CONFSTORAGEDIR=$(LMVARDIR)/conf
FIRSTCONFFILE=$(CONFSTORAGEDIR)/lmConf-1.json
LMINIFILE=$(CONFDIR)/lemonldap-ng.ini
CAPTCHADIR=$(LMVARDIR)/captcha

%:
	dh $@ --with systemd

override_dh_auto_configure:
	$(MAKE) configure STORAGECONFFILE=/etc/lemonldap-ng/lemonldap-ng.ini \
		PERLOPTIONS="INSTALLDIRS=vendor"

override_dh_auto_build:
	$(MAKE) all

override_dh_auto_install:
	$(MAKE) install \
			DESTDIR=$(CURDIR)/debian/tmp \
			PREFIX=/usr \
			LMPREFIX=/usr/share/lemonldap-ng/ \
			BINDIR=$(LMSHAREDIR)/bin \
			SBINDIR=/usr/sbin \
			FASTCGISOCKDIR=/var/run/llng-fastcgi-server \
			DOCUMENTROOT=$(LMVARDIR) \
			EXAMPLESDIR=/examples \
			HANDLERDIR=$(LMVARDIR)/handler \
			MANAGERDIR=$(LMSHAREDIR)/manager \
			PORTALDIR=$(LMSHAREDIR)/portal \
			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 \
			DEFDOCDIR=/usr/share/doc/lemonldap-ng-doc \
			FRDOCDIR=/usr/share/doc/lemonldap-ng-fr-doc \
			PROD=yes
	$(MAKE) install_fr_doc_site \
			DESTDIR=$(CURDIR)/debian/tmp \
			DOCDIR=/usr/share/doc/lemonldap-ng-fr-doc \
			PROD=yes
	for i in handler portal manager test; do \
		mv $(TMP)/etc/lemonldap-ng/$$i-apache2.X.conf $(TMP)/etc/lemonldap-ng/$$i-apache2.conf; \
	done

override_dh_link:
	rm -rf debian/lemonldap-ng-fr-doc/usr/share/doc/lemonldap-ng-fr-doc/pages/documentation/current/documentation \
		debian/lemonldap-ng-fr-doc/usr/share/doc/lemonldap-ng-fr-doc/pages/documentation/current/icons \
		debian/lemonldap-ng-fr-doc/usr/share/doc/lemonldap-ng-fr-doc/pages/documentation/current/lib
	dh_link

override_dh_compress:
	dh_compress -X favicon.ico

# Fix lemonldap-ng dirs permissions and owner since dh_fixperms change them:
#  * global configuration dirs must be writable by www-data but not readable
#    by all (also sessions, captcha,... dirs)
#  * lemonldap-ng.ini must not be readable by all
override_dh_fixperms:
	dh_fixperms
	chown www-data:www-data \
		debian/*/$(SESSIONSDIR) \
		debian/*/$(SESSIONSDIR)/lock \
		debian/*/$(PSESSIONSDIR) \
		debian/*/$(PSESSIONSDIR)/lock \
		debian/*/$(NOTIFICATIONSDIR) \
		debian/liblemonldap-ng-common-perl/$(CONFSTORAGEDIR) \
		debian/liblemonldap-ng-portal-perl/$(CAPTCHADIR)
	chgrp www-data debian/liblemonldap-ng-common-perl/$(LMINIFILE) \
		debian/liblemonldap-ng-common-perl/$(FIRSTCONFFILE)
	chmod 770 debian/*/$(SESSIONSDIR) debian/*/$(SESSIONSDIR)/lock \
	        debian/*/$(PSESSIONSDIR) debian/*/$(PSESSIONSDIR)/lock \
		debian/*/$(NOTIFICATIONSDIR) \
		debian/liblemonldap-ng-portal-perl/$(CAPTCHADIR)
	chmod 750 debian/liblemonldap-ng-common-perl/$(CONFSTORAGEDIR)
	chmod 640 debian/liblemonldap-ng-common-perl/$(FIRSTCONFFILE) \
		debian/liblemonldap-ng-common-perl/$(LMINIFILE)
