#!/usr/bin/make -f

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

ifneq (,$(or $(filter noopt,$(DEB_BUILD_OPTIONS)),\
             $(filter nostrip,$(DEB_BUILD_OPTIONS))))
	FLAGS += debug=1
endif

BACKEND := debian/open-xchange-appsuite-manifest/opt/open-xchange/appsuite
FRONTEND := debian/open-xchange-appsuite/var/www/appsuite
SETTINGS := debian/open-xchange-guidedtours/opt/open-xchange/etc

override_dh_auto_install:
	sh bin/build-appsuite skipLess=1 builddir=$(FRONTEND) manifestDir=$(BACKEND) \
	    l10nDir=debian/open-xchange-appsuite-l10n-@lang@/opt/open-xchange/appsuite \
	    helpDir=debian/open-xchange-appsuite-help-@lang@/var/www/appsuite \
	    helpDriveDir=debian/open-xchange-appsuite-help-drive-@lang@/var/www/appsuite \
	    $(FLAGS) --trace
	for i in i18n/*.properties; do \
	    j=$${i#i18n/}; pkgname=$${j%.properties}; \
	    mkdir -p debian/$$pkgname/opt/open-xchange/etc/languages/appsuite/; \
	    cp $$i debian/$$pkgname/opt/open-xchange/etc/languages/appsuite/; \
	done
	cp -r $(FRONTEND)/apps $(BACKEND)/
	mv $(FRONTEND)/share $(BACKEND)/
	mkdir -p debian/open-xchange-appsuite/opt/open-xchange/sbin
	sed -e "s:## cd ##:cd /var/www/appsuite:" bin/touch-appsuite > \
	    debian/open-xchange-appsuite/opt/open-xchange/sbin/touch-appsuite
	chmod +x debian/open-xchange-appsuite/opt/open-xchange/sbin/touch-appsuite
	mkdir -p debian/open-xchange-guidedtours/opt/open-xchange/appsuite/apps/io.ox
	mv $(BACKEND)/apps/io.ox/tours debian/open-xchange-guidedtours/opt/open-xchange/appsuite/apps/io.ox/
	mkdir -p "$(SETTINGS)"
	cp -r conf/* "$(SETTINGS)"
	mkdir -p "debian/open-xchange-guidedtours/opt/open-xchange/appsuite/manifests"
	mv $(BACKEND)/manifests/open-xchange-guidedtours.json debian/open-xchange-guidedtours/opt/open-xchange/appsuite/manifests/
	mkdir -p debian/open-xchange-appsuite-help-common/var/www/appsuite/
	mv $(FRONTEND)/help debian/open-xchange-appsuite-help-common/var/www/appsuite/
	mkdir -p debian/open-xchange-appsuite-help-drive-common/var/www/appsuite/
	mv $(FRONTEND)/help-drive debian/open-xchange-appsuite-help-drive-common/var/www/appsuite/

override_dh_auto_clean:
	sh bin/build-appsuite clean skipLess=1 builddir=$(FRONTEND) manifestDir=$(BACKEND) \
	    l10nDir=debian/open-xchange-appsuite-l10n-@lang@/opt/open-xchange/appsuite \
        helpDir=debian/open-xchange-appsuite-help-@lang@/var/www/appsuite \
	    $(FLAGS) --trace

%:
	dh $@
