#!/usr/bin/make -f
# -*- makefile -*-
export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
VERSION=5

%:
	dh $@

override_dh_auto_clean:
	[ ! -f config.make ] || $(MAKE) distclean
	[ ! -f config.make ] ||	$(MAKE) -C ActiveSync distclean
	rm -f config.make

override_dh_auto_build:
	dh_auto_build -- OBJCFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" messages=yes
	$(MAKE) -C ActiveSync OBJCFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" message=yes

override_dh_auto_configure:
	./configure --with-ssl=ssl --enable-saml2 --enable-mfa

override_dh_auto_test:
ifeq ($(DEB_HOST_ARCH_ENDIAN),little)
	dh_auto_test
else
	-dh_auto_test
endif

override_dh_auto_install:
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp GNUSTEP_INSTALLATION_DOMAIN=SYSTEM OBJCFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" messages=yes install
	$(MAKE) -C ActiveSync DESTDIR=$(CURDIR)/debian/tmp GNUSTEP_INSTALLATION_DOMAIN=SYSTEM OBJCFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" messages=yes install
	mkdir -p debian/tmp/etc/default debian/tmp/etc/cron.d debian/tmp/etc/logrotate.d debian/tmp/usr/share/doc/sogo debian/tmp/usr/lib/tmpfiles.d
	cp Scripts/sogo-default debian/tmp/etc/default/sogo
	cp Scripts/sogo-default debian/tmp/etc/default/sogoeas
	sed -i 's/sogo/sogoeas/g' debian/tmp/etc/default/sogoeas
	sed -i 's/PREFORK=3/PREFORK=15/' debian/tmp/etc/default/sogoeas
	sed -i 's/Default: 3/Default: 15/' debian/tmp/etc/default/sogoeas
	sed -i 's/SOGo/SOGo ActiveSync/' debian/tmp/etc/default/sogoeas
	cp Scripts/sogo.cron debian/tmp/etc/cron.d/sogo
	cp Scripts/sogo-backup.sh debian/tmp/usr/sbin/sogo-backup
	cp debian/logrotate debian/tmp/etc/logrotate.d/sogo
	cp debian/logrotate debian/tmp/etc/logrotate.d/sogoeas
	sed -i 's/sogo/sogoeas/' debian/tmp/etc/logrotate.d/sogoeas
#	cp Apache/SOGo.conf debian/tmp/usr/share/doc/sogo/apache.conf
	cp debian/sogo.tmpfiles.d debian/tmp/usr/lib/tmpfiles.d/sogo.conf

#	Put arch independent files in /usr/share instead of /usr/lib
#	Resource files for SOGo
	mkdir -p debian/tmp/usr/share/GNUstep/SOGo
	for i in AdministrationUI.SOGo Appointments.SOGo API.SOGo CommonUI.SOGo Contacts.SOGo ContactsUI.SOGo MailPartViewers.SOGo \
		 Mailer.SOGo MailerUI.SOGo MainUI.SOGo PreferencesUI.SOGo SchedulerUI.SOGo; do \
	  mkdir debian/tmp/usr/share/GNUstep/SOGo/$$i \
	  && mv debian/tmp/usr/lib/GNUstep/SOGo/$$i/Resources debian/tmp/usr/share/GNUstep/SOGo/$$i/ \
	  && dh_link -psogo-common usr/share/GNUstep/SOGo/$$i/Resources usr/lib/GNUstep/SOGo/$$i/Resources ; \
	done

#	Resource files for ActiveSync
	mkdir -p debian/tmp/usr/share/GNUstep/SOGo/ActiveSync.SOGo
	mv debian/tmp/usr/lib/GNUstep/SOGo/ActiveSync.SOGo/Resources debian/tmp/usr/share/GNUstep/SOGo/ActiveSync.SOGo
	dh_link -psogo-activesync-common usr/share/GNUstep/SOGo/ActiveSync.SOGo/Resources usr/lib/GNUstep/SOGo/ActiveSync.SOGo/Resources
	-rm debian/tmp/usr/lib/GNUstep/SOGo/ActiveSync.SOGo/stamp.make

#	Resource files for Templates and WebServerResources
	mv debian/tmp/usr/lib/GNUstep/SOGo/Templates debian/tmp/usr/lib/GNUstep/SOGo/WebServerResources debian/tmp/usr/share/GNUstep/SOGo
	dh_link -psogo-common usr/share/GNUstep/SOGo/Templates usr/lib/GNUstep/SOGo/Templates
	dh_link -psogo-common usr/share/GNUstep/SOGo/WebServerResources usr/lib/GNUstep/SOGo/WebServerResources
	mkdir -p debian/tmp/usr/share/GNUstep/Libraries/Resources
	mv debian/tmp/usr/lib/GNUstep/Libraries/Resources/NGCards debian/tmp/usr/share/GNUstep/Libraries/Resources
	rmdir debian/tmp/usr/lib/GNUstep/Libraries/Resources debian/tmp/usr/lib/GNUstep/Libraries

#	Framework Resources
	mkdir -p debian/tmp/usr/share/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)
	mv debian/tmp/usr/lib/GNUstep/Frameworks/SOGo.framework/Resources debian/tmp/usr/share/GNUstep/Frameworks/SOGo.framework
	mv debian/tmp/usr/lib/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)/Resources debian/tmp/usr/share/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)
	dh_link -psogo-common usr/share/GNUstep/Frameworks/SOGo.framework/Resources usr/lib/GNUstep/Frameworks/SOGo.framework/Resources
	dh_link -psogo-common usr/share/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)/Resources usr/lib/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)/Resources
	dh_link -psogo-common usr/share/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION) usr/share/GNUstep/Frameworks/SOGo.framework/Versions/Current

#	Remove MaterialIcons-Regular fonts
	rm debian/tmp/usr/share/GNUstep/SOGo/WebServerResources/fonts/MaterialIcons-Regular.*

#	make all other fonts have to be non executable
	chmod 644 debian/tmp/usr/share/GNUstep/SOGo/WebServerResources/fonts/*

#	Make links to packaged version for font MaterialIcons-Regular
	dh_link -psogo-common usr/share/fonts/eot/material-design-icons-iconfont/MaterialIcons-Regular.eot usr/share/GNUstep/SOGo/WebServerResources/fonts/MaterialIcons-Regular.eot
	dh_link -psogo-common usr/share/fonts/truetype/material-design-icons-iconfont/MaterialIcons-Regular.ttf usr/share/GNUstep/SOGo/WebServerResources/fonts/MaterialIcons-Regular.ttf
	dh_link -psogo-common usr/share/fonts/woff/material-design-icons-iconfont/MaterialIcons-Regular.woff usr/share/GNUstep/SOGo/WebServerResources/fonts/MaterialIcons-Regular.woff
	dh_link -psogo-common usr/share/fonts/woff/material-design-icons-iconfont/MaterialIcons-Regular.woff2 usr/share/GNUstep/SOGo/WebServerResources/fonts/MaterialIcons-Regular.woff2


#	Headers for development package
	mkdir -p debian/tmp/usr/share/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)
	mv debian/tmp/usr/lib/GNUstep/Frameworks/SOGo.framework/Headers debian/tmp/usr/share/GNUstep/Frameworks/SOGo.framework
	mv debian/tmp/usr/lib/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)/Headers debian/tmp/usr/share/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)
	dh_link -psogo-dev usr/share/GNUstep/Frameworks/SOGo.framework/Headers usr/lib/GNUstep/Frameworks/SOGo.framework/Headers
	dh_link -psogo-dev usr/share/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)/Headers usr/lib/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)/Headers

	dh_install

override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG.md
