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

%:
	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

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 Scripts/logrotate debian/tmp/etc/logrotate.d/sogo
	cp Scripts/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 the most of SOGo
	mkdir -p debian/tmp/usr/share/GNUstep/SOGo
	for i in AdministrationUI.SOGo Appointments.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
#	in jessie the link Resources ist not available, therefore create it
	-ln -s Versions/Current/Resources debian/tmp/usr/lib/GNUstep/Frameworks/SOGo.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
	dh_link -psogo-common usr/share/GNUstep/Frameworks/SOGo.framework/Resources usr/lib/GNUstep/Frameworks/SOGo.framework/Resources
	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/Versions/$(VERSION)/Resources usr/lib/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)/Resources
#	cannot move the link Current, because it is used in the directory /usr/lib/GNUstep/Frameworks/SOGo.framework/sogo
#	mv debian/tmp/usr/lib/GNUstep/Frameworks/SOGo.framework/Versions/Current debian/tmp/usr/share/GNUstep/Frameworks/SOGo.framework/Versions
#	dh_link -psogo-common usr/share/GNUstep/Frameworks/SOGo.framework/Versions/Current usr/lib/GNUstep/Frameworks/SOGo.framework/Versions/Current
#	manually create link Current, because it cannot move from /usr/lib/GNUstep/Frameworks/SOGo.framework/Versions
	ln -s 4 debian/tmp/usr/share/GNUstep/Frameworks/SOGo.framework/Versions/Current
#	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
	dh_link -psogo-dev usr/share/GNUstep/Frameworks/SOGo.framework/Headers usr/lib/GNUstep/Frameworks/SOGo.framework/Headers
	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/Versions/$(VERSION)/Headers usr/lib/GNUstep/Frameworks/SOGo.framework/Versions/$(VERSION)/Headers
	dh_install
