#
VERSION	= 6.18.0.0
BRANCH		= branches/bf_6_18_0
ARCHNAME	= open-xchange-usm
DISTARCH	= ../$(ARCHNAME)-$(VERSION)
DESTDIR		= /
INSTPREFIX	= /opt/open-xchange
BUNDLESDIR	= $(INSTPREFIX)/bundles
USMZIP		= USMBuild/current-build/usm.zip
EXCLLIST	= \(trunk\|tags\|branches\|com.openexchange.usm.test\|com.openexchange.usm.sample_protocol\|com.openexchange.usm.database.mysql\|com.openexchange.usm.database.hsql\|com.openexchange.usm.authentication\|com.openexchange.usm.cache.impl\|org.apache.jcs\|USMXXXReferencedOXClasses\|com.openexchange.usm.eas\|com.openexchange.usm.configuration.impl\)
GROUPWAREBLIST	= com.openexchange.usm*.jar org.eclipse.equinox.ds_*.jar org.eclipse.equinox.util_*.jar
ADMINBLIST	= org.eclipse.equinox.ds_*.jar org.eclipse.equinox.util_*.jar com.openexchange.usm.api*.jar com.openexchange.usm.database.ox*.jar com.openexchange.usm.journal*.jar com.openexchange.usm.util*.jar

dist:	clean checkout
	rm -rf $(DISTARCH)
	mkdir -p $(DISTARCH)
	cp oxbuild/ant-build.sh USMBuildLaunchers/ant
	cp -r * $(DISTARCH)
	find $(DISTARCH) -name .svn | xargs rm -rf
	svnversion com.openexchange.usm > $(DISTARCH)/USMBuildLaunchers/ant/USMREVISION
	tar zcvpf ../$(ARCHNAME)-$(VERSION).tar.gz $(DISTARCH)
	rm -rf $(DISTARCH)

install: 
	mkdir -p $(DESTDIR)/$(BUNDLESDIR)
	mkdir -p $(DESTDIR)/$(INSTPREFIX)/etc/groupware/osgi/bundle.d/
	mkdir -p $(DESTDIR)/$(INSTPREFIX)/etc/admindaemon/osgi/bundle.d/
	{ cd USMBuildLaunchers/ant; \
	  sh ant-build.sh $(VERSION) $$(<USMREVISION); }
	unzip $(USMZIP)
	cp com.openexchange.usm*.jar $(DESTDIR)/$(BUNDLESDIR)
	cp org.eclipse.equinox.ds_*.jar $(DESTDIR)/$(BUNDLESDIR)
	cp org.eclipse.equinox.util_*.jar $(DESTDIR)/$(BUNDLESDIR)
	@for i in $(GROUPWAREBLIST); do \
		echo $(INSTPREFIX)/bundles/$$i/@start > $(DESTDIR)/$(INSTPREFIX)/etc/groupware/osgi/bundle.d/$$(basename $$i .jar).ini; \
	done
	@for i in $(ADMINBLIST); do \
		echo $(INSTPREFIX)/bundles/$$i/@start > $(DESTDIR)/$(INSTPREFIX)/etc/admindaemon/osgi/bundle.d/$$(basename $$i .jar).ini; \
	done
	cp com.openexchange.usm/usm.properties $(DESTDIR)/$(INSTPREFIX)/etc/groupware/
	cp com.openexchange.usm/usm_json.properties $(DESTDIR)/$(INSTPREFIX)/etc/groupware/

checkout:
	for i in \
	  $$(svn ls https://svn.open-xchange.com/USM|grep -vE ^$(EXCLLIST)); do \
		test -d $$i && svn up $$i || \
		svn co https://svn.open-xchange.com/USM/$$i/$(BRANCH) $$i; \
	done

clean:
	@find . -name "*~" -exec rm -f {} \; -print
	@find . -name "\.#*" -exec rm -f {} \; -print
