<?xml version="1.0" encoding="UTF-8"?>
<project name="Open-Xchange Groupware" default="default" basedir=".">

    <description>
        Open-Xchange Collaboration Suite Server
    </description>

    <!-- Customizable values -->
    <!-- This values must be changed for defining other installation places -->
    <property name="destdir" value=""/>
    <property name="prefix" location="/opt/open-xchange"/>
    <property name="log.install.dir" value="/var/log/open-xchange"/>
    <property name="distribution" value="debian"/>

    <!-- Constants -->
    <property name="srcDir" value="src"/>
    <property name="src5Dir" value="src5"/>
    <property name="src6Dir" value="src6"/>
    <property name="tmp.dir" value="tmp"/>
    <property name="conf.dir" value="conf/groupware"/>
    <property name="commonConfDir" value="conf/common"/>
    <property name="importCSVDir" value="conf/importCSV"/>
    <property name="bin.dir" value="sbin"/>
    <property name="lib.dir" value="lib"/>

    <property file="META-INF/MANIFEST.MF"/>
    <property file="build.properties"/>

    <property name="version" value="${Bundle-Version}"/>
    <property name="bundle.name" value="${Bundle-SymbolicName}.jar"/>
    <property name="composite.name" value="open-xchange-server-${version}"/>

    <fileset dir="." id="metainf">
        <include name="META-INF/**"/>
        <exclude name="CVS"/>
    </fileset>

    <!-- Install Constants -->
    <property name="bundlesdir" value="${prefix}/bundles"/>
    <property name="gwbundle.d" value="${prefix}/etc/groupware/osgi/bundle.d"/>
    <property name="adbundle.d" value="${prefix}/etc/admindaemon/osgi/bundle.d"/>

    <!-- Replacements -->
    <property name="bundles.install.dir" value="${destdir}${bundlesdir}"/>
    <property name="common.conf.install.dir" value="${prefix}/etc"/>
    <property name="conf.install.dir" value="${common.conf.install.dir}/groupware"/>
    <property name="confInstallDir2" value="${common.conf.install.dir}/common"/>
    <property name="groupwareConfInstallCopyDir" location="${destdir}${conf.install.dir}"/>
    <property name="commonConfInstallCopyDir" location="${destdir}${confInstallDir2}"/>
    <property name="log.install.copy.dir" location="${destdir}${log.install.dir}"/>
    <property name="bin.install.dir" value="${prefix}/sbin/"/>
    <property name="bin.install.copy.dir" location="${destdir}${bin.install.dir}"/>

    <!-- Bundle dependencies -->
    <property name="commonBundle" value="com.openexchange.common"/>
    <property name="cachingBundle" value="com.openexchange.caching"/>
    <property name="xmlBundle" value="com.openexchange.xml"/>
    <property name="configreadBundle" value="com.openexchange.configread"/>

    <!-- JAR names for classpath -->
    <property name="osgijar" value="org.eclipse.osgi_3.6.1.R36x_v20100806.jar"/>
    <property name="osgiservicesjar" value="org.eclipse.osgi.services_3.2.100.v20100503.jar"/>
    <property name="servletjar" value="javax.servlet_2.4.0.v200706111738.jar"/>
    <property name="commonsclijar" value="org.apache.commons.cli_1.0.0.v20080604-1500.jar"/>
    <property name="commonsloggingjar" value="org.apache.commons.logging_1.0.4.v200706111724.jar"/>
    <property name="globaljar" value="com.openexchange.global.jar"/>
    <property name="retentionjar" value="com.openexchange.dataretention.jar"/>
    <property name="conversionjar" value="com.openexchange.conversion.jar"/>
    <property name="configjar" value="com.openexchange.configread.jar"/>
    <property name="monitoringjar" value="com.openexchange.monitoring.jar"/>
    <property name="secretjar" value="com.openexchange.secret.jar"/>
    <property name="managementjar" value="com.openexchange.management.jar"/>
    <property name="cachingjar" value="com.openexchange.caching.jar"/>
    <property name="commonjar" value="common.jar"/>
    <property name="xmlbundlejar" value="com.openexchange.xml.jar"/>
    <property name="threadpooljar" value="com.openexchange.threadpool.jar"/>
    <property name="publishjar" value="com.openexchange.publish.jar"/>
    <property name="pushjar" value="com.openexchange.push.jar"/>
    <property name="messagingjar" value="com.openexchange.messaging.jar"/>
    <property name="genericonfjar" value="com.openexchange.datatypes.genericonf.jar"/>
    <property name="secretjar" value="com.openexchange.secret.jar"/>
    <property name="htmljar" value="com.openexchange.html.jar"/>
    <property name="filestoragejar" value="com.openexchange.file.storage.jar"/>
    <property name="txjar" value="com.openexchange.tx.jar"/>
	<property name="cryptojar" value="com.openexchange.crypto.jar"/>
    <property name="filestoragecompositionjar" value="com.openexchange.file.storage.composition.jar"/>

    <property name="activationjar" value="activation-1.1.1.jar"/>
    <property name="chardetjar" value="chardet-1.1.jar"/>
    <property name="commonscodecjar" value="commons-codec-1.4.jar"/>
    <property name="commonsfileuploadjar" value="commons-fileupload-1.2.1.jar"/>
    <property name="commonshttpclientjar" value="commons-httpclient-3.1.jar"/>
    <property name="commonsiojar" value="commons-io-1.4.jar"/>
    <property name="jakartawebdavjar" value="jakarta-slide-webdavlib-2.1.jar"/>
    <property name="jdomjar" value="jdom-1.1.1.jar"/>
    <property name="jsonjar" value="json.jar"/>
    <property name="mailjar" value="mail-1.4.3.jar"/>
    <property name="libidnjar" value="libidn-1.9.jar"/>
    <property name="springbeanjar" value="spring-beans_1.2.7.jar"/>
    <property name="springcorejar" value="spring-core_1.2.7.jar"/>
    <property name="tnefjar" value="tnef-1.6.0.jar"/>
    <property name="trovejar" value="trove-2.1.0.jar"/>
    <!-- <property name="highscalejar" value="high-scale-lib-1.1.2.jar" /> -->

    <!-- Third party libs -->
    <fileset dir="${basedir}" id="3rdpartyjars">
        <include name="${lib.dir}/*.jar"/>
        <exclude name="CVS"/>
    </fileset>

    <!-- Classpath -->
    <path id="project.classpath">
        <pathelement location="${bundlesdir}/${osgijar}"/>
        <pathelement location="${bundlesdir}/${osgiservicesjar}"/>
        <pathelement location="${bundlesdir}/${servletjar}"/>
        <pathelement location="${bundlesdir}/${commonsclijar}"/>
        <pathelement location="${bundlesdir}/${commonsloggingjar}"/>

        <pathelement location="${bundlesdir}/${globaljar}"/>
        <pathelement location="${bundlesdir}/${retentionjar}"/>
        <pathelement location="${bundlesdir}/${conversionjar}"/>
        <pathelement location="${bundlesdir}/${configreadBundle}/${configjar}"/>
        <pathelement location="${bundlesdir}/${monitoringjar}"/>
        <pathelement location="${bundlesdir}/${secretjar}"/>
        <pathelement location="${bundlesdir}/${managementjar}"/>
        <pathelement location="${bundlesdir}/${cachingBundle}/${cachingjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/${commonjar}"/>
        <pathelement location="${bundlesdir}/${xmlBundle}/${xmlbundlejar}"/>
        <pathelement location="${bundlesdir}/${threadpooljar}"/>
        <pathelement location="${bundlesdir}/${publishjar}"/>
        <pathelement location="${bundlesdir}/${pushjar}"/>
        <pathelement location="${bundlesdir}/${messagingjar}"/>
        <pathelement location="${bundlesdir}/${genericonfjar}"/>
        <pathelement location="${bundlesdir}/${secretjar}"/>
        <pathelement location="${bundlesdir}/${htmljar}"/>
        <pathelement location="${bundlesdir}/${filestoragejar}"/>
        <pathelement location="${bundlesdir}/${txjar}"/>
    	<pathelement location="${bundlesdir}/${cryptojar}"/>
        <pathelement location="${bundlesdir}/${filestoragecompositionjar}"/>

        <pathelement location="${bundlesdir}/javax.activation/lib/${activationjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${commonscodecjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${commonsfileuploadjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${commonshttpclientjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${commonsiojar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${jakartawebdavjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${jsonjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${mailjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${libidnjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${tnefjar}"/>
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${trovejar}"/>
        <!-- <pathelement location="${bundlesdir}/${commonBundle}/lib/${highscalejar}" /> -->
        <pathelement location="${bundlesdir}/${commonBundle}/lib/${chardetjar}"/>
        <pathelement location="${bundlesdir}/${xmlBundle}/lib/${jdomjar}"/>
        <pathelement location="${bundlesdir}/${xmlBundle}/lib/${springbeanjar}"/>
        <pathelement location="${bundlesdir}/${xmlBundle}/lib/${springcorejar}"/>

        <fileset refid="3rdpartyjars"/>
    </path>

    <!-- JARs for running server -->
    <fileset dir="${tmp.dir}" id="server-jars">
        <include name="${bundle.name}"/>
        <!-- TODO -->
        <include name="ox_languages.jar"/>
    </fileset>

    <!-- Compile -->
    <target name="prepare">
        <mkdir dir="${output..}"/>
        <mkdir dir="${tmp.dir}"/>
        <condition property="isJava5">
            <equals arg1="1.5" arg2="${ant.java.version}"/>
        </condition>
        <condition property="isJava6">
            <available classname="java.sql.NClob"/>
        </condition>
    </target>

    <target name="java5" if="isJava5">
        <path id="source">
            <pathelement location="${srcDir}"/>
            <pathelement location="${src5Dir}"/>
        </path>
    </target>

    <target name="java6" if="isJava6">
        <path id="source">
            <pathelement location="${srcDir}"/>
            <pathelement location="${src6Dir}"/>
        </path>
    </target>

    <target name="compile" depends="prepare,java5,java6">
        <javac memoryMaximumSize="192M" fork="yes" destdir="${output..}" debug="true" debuglevel="lines,vars,source" encoding="US-ASCII" source="1.5" compiler="javac1.5" target="1.5">
            <src refid="source"/>
            <classpath refid="project.classpath"/>
        </javac>
    </target>

    <target name="jar" depends="compile,manifest">
        <jar jarfile="${tmp.dir}/${bundle.name}" manifest="META-INF/MANIFEST.MF.package">
            <fileset dir="${output..}"/>
            <fileset refid="3rdpartyjars"/>
        </jar>
    </target>

    <!-- TODO add create-lang-jar -->
    <target name="build" depends="jar"/>

    <!-- Install targets -->
    <target name="installInclude">
        <mkdir dir="${destdir}/${gwbundle.d}"/>
        <echo file="${destdir}/${gwbundle.d}/${Bundle-SymbolicName}.ini" message="${bundlesdir}/${bundle.name}@start${line.separator}"/>
        <mkdir dir="${destdir}/${adbundle.d}"/>
        <echo file="${destdir}/${adbundle.d}/${Bundle-SymbolicName}.ini" message="${bundlesdir}/${bundle.name}@start${line.separator}"/>
    </target>

    <target name="installJars" depends="build,installInclude">
        <mkdir dir="${bundles.install.dir}"/>
        <copy todir="${bundles.install.dir}" overwrite="true">
            <fileset refid="server-jars"/>
        </copy>
    </target>

    <target name="patchConfig" depends="prepare">
        <property name="patch.conf.dir" value="${tmp.dir}/${conf.dir}"/>
        <mkdir dir="${patch.conf.dir}"/>
        <copy todir="${patch.conf.dir}" overwrite="true">
            <fileset dir="${conf.dir}"/>
        </copy>
        <antcall target="patchConfigFiles">
            <param name="conf.subst.patch" value="${conf.install.dir}"/>
            <param name="common.conf.subst.patch" value="${confInstallDir2}"/>
            <param name="root.conf.subst.patch" value="${prefix}"/>
        </antcall>
    </target>

    <target name="installConfig" depends="patchConfig">
        <mkdir dir="${groupwareConfInstallCopyDir}"/>
        <copy todir="${groupwareConfInstallCopyDir}" overwrite="true">
            <fileset dir="${patch.conf.dir}">
                <include name="**"/>
                <exclude name="*.in"/>
                <exclude name="CVS"/>
                <exclude name="osgi/**"/>
            </fileset>
        </copy>
        <chmod perm="640">
            <fileset dir="${groupwareConfInstallCopyDir}">
                <include name="mail.properties,configdb.properties,server.properties"/>
            </fileset>
        </chmod>
        <mkdir dir="${commonConfInstallCopyDir}"/>
        <copy todir="${commonConfInstallCopyDir}">
            <fileset dir="${commonConfDir}">
                <include name="**"/>
                <exclude name="*.in"/>
                <exclude name="CVS"/>
            </fileset>
        </copy>

        <mkdir dir="${destdir}/${prefix}/importCSV"/>
        <copy todir="${destdir}/${prefix}/importCSV" overwrite="true">
            <fileset dir="${importCSVDir}">
                <include name="**"/>
                <exclude name="CVS"/>
            </fileset>
        </copy>
        <!--        <chmod perm="640">
            <fileset dir="${commonConfInstallCopyDir}">
                <include name="**"/>
            </fileset>
        </chmod> -->
    </target>

    <target name="patchScripts" depends="prepare">
        <property name="patch.bin.dir" value="${tmp.dir}/${bin.dir}"/>
        <mkdir dir="${patch.bin.dir}"/>
        <copy todir="${patch.bin.dir}" overwrite="true">
            <fileset dir="${bin.dir}"/>
        </copy>
        <antcall target="moveAndPatchScripts">
            <param name="common.conf.subst.patch" value="${conf.install.dir}"/>
            <param name="conf.subst.patch" value="${conf.install.dir}"/>
            <param name="lib.subst.patch" value="${lib.install.dir}"/>
        </antcall>
    </target>

    <target name="installScripts" depends="patchConfig,patchScripts">
        <mkdir dir="${groupwareConfInstallCopyDir}"/>
        <copy todir="${groupwareConfInstallCopyDir}" overwrite="true">
            <fileset dir="${patch.conf.dir}">
                <include name="osgi/**"/>
            </fileset>
        </copy>
        <copy todir="${bin.install.copy.dir}" overwrite="true">
            <fileset dir="${patch.bin.dir}"/>
        </copy>
        <chmod perm="755" file="${bin.install.copy.dir}/open-xchange-groupware"/>
        <chmod perm="755" file="${bin.install.copy.dir}/checkconsistency"/>
        <chmod perm="755" file="${bin.install.copy.dir}/checkconfigconsistency"/>
        <chmod perm="755" file="${bin.install.copy.dir}/listExecutedUpdateTasks"/>
        <chmod perm="755" file="${bin.install.copy.dir}/logincounter"/>
        <chmod perm="755" file="${bin.install.copy.dir}/oxreport"/>
        <chmod perm="755" file="${bin.install.copy.dir}/resetversion"/>
        <chmod perm="755" file="${bin.install.copy.dir}/runupdate"/>
        <chmod perm="755" file="${bin.install.copy.dir}/schemasandversions"/>
        <chmod perm="755" file="${bin.install.copy.dir}/forceupdatetask"/>
        <chmod perm="755" file="${bin.install.copy.dir}/restoregabdefaults"/>

        <copy todir="${destdir}/" overwrite="true">
            <fileset dir="system"/>
            <mapper type="glob" from="*.${distribution}" to="*"/>
        </copy>
        <chmod perm="755" file="${destdir}/etc/init.d/open-xchange-groupware"/>
    </target>

    <target name="installExceptJars" depends="installScripts">
        <mkdir dir="${log.install.copy.dir}"/>
    </target>

    <target name="install" depends="build,installJars,installConfig,installExceptJars"/>

    <target name="default" depends="install"/>

    <target name="clean">
        <delete dir="${tmp.dir}"/>
        <delete dir="${output..}"/>
        <delete file="${source..}/com/openexchange/server/Version.java"/>
    </target>

    <!-- Dist -->
    <target name="dist" depends="clean">
        <property name="dist-package" value="dist-package"/>
        <delete dir="${dist-package}"/>
        <mkdir dir="${dist-package}/${composite.name}"/>
        <copy todir="${dist-package}/${composite.name}">
            <fileset dir=".">
                <exclude name="**${dist-package}/**"/>
                <exclude name="**debian/**"/>
                <exclude name="**CVS/**"/>
                <exclude name="Todo"/>
            </fileset>
        </copy>
        <tar destfile="../${composite.name}.tar.gz" basedir="${dist-package}" includes="${composite.name}/" compression="gzip"/>
        <!-- dpkg-source does not like anything else -->
        <delete dir="${dist-package}"/>
    </target>

    <target name="patchConfigFiles">
        <fail message="Define root.conf.subst.patch." unless="root.conf.subst.patch"/>
        <fail message="Define conf.subst.patch." unless="conf.subst.patch"/>
        <fail message="Define common.conf.subst.patch." unless="common.conf.subst.patch"/>
        <antcall target="patchOSGiIni"/>
        <antcall target="patchFileLoggingProperties"/>
        <antcall target="patchScriptConf"/>
        <antcall target="patchPathInProperties"/>
    </target>

    <target name="patchOSGiIni">
        <fail message="Define bundlesdir." unless="bundlesdir"/>
        <fail message="Define log.install.dir." unless="log.install.dir"/>
        <move tofile="${patch.conf.dir}/osgi/config.ini.template" file="${patch.conf.dir}/osgi/config.ini.template.in" overwrite="true"/>
        <replace file="${patch.conf.dir}/osgi/config.ini.template">
            <replacefilter token="@bundlesdir@" value="${bundlesdir}"/>
        </replace>
        <replace file="${patch.conf.dir}/osgi/config.ini.template">
            <replacefilter token="@logfile@" value="${log.install.dir}/open-xchange-osgi.log"/>
        </replace>
    </target>

    <target name="patchFileLoggingProperties">
        <move tofile="${patch.conf.dir}/file-logging.properties" file="${patch.conf.dir}/file-logging.properties.in" overwrite="true"/>
        <replace file="${patch.conf.dir}/file-logging.properties">
            <replacefilter token="@serverlogfile@" value="${log.install.dir}/open-xchange.log"/>
        </replace>
    </target>

    <target name="patchScriptConf">
        <move tofile="${patch.conf.dir}/ox-scriptconf.sh" file="${patch.conf.dir}/ox-scriptconf.sh.in" overwrite="true"/>
        <replace file="${patch.conf.dir}/ox-scriptconf.sh">
            <replacefilter token="@propertiesdir@" value="${conf.subst.patch}"/>
            <replacefilter token="@commonPropertiesDir@" value="${common.conf.subst.patch}"/>
            <replacefilter token="@loggingproperties@" value="${conf.subst.patch}/file-logging.properties"/>
            <replacefilter token="@oxgroupwaresysconfdir@" value="${conf.subst.patch}"/>
        </replace>
    </target>

    <target name="patchPathInProperties">
        <move tofile="${patch.conf.dir}/import.properties" file="${patch.conf.dir}/import.properties.in" overwrite="true"/>
        <move tofile="${patch.conf.dir}/sessiond.properties" file="${patch.conf.dir}/sessiond.properties.in" overwrite="true"/>
        <move tofile="${patch.conf.dir}/system.properties" file="${patch.conf.dir}/system.properties.in" overwrite="true"/>

        <replace dir="${patch.conf.dir}" includes="*.properties">
            <replacefilter token="@oxgroupwaresysconfdir@" value="${conf.subst.patch}"/>
            <replacefilter token="@rootinstalldir@" value="${root.conf.subst.patch}"/>
        </replace>
    </target>

    <target name="moveAndPatchScripts">
        <move tofile="${patch.bin.dir}/checkconsistency" file="${patch.bin.dir}/checkconsistency.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/checkconfigconsistency" file="${patch.bin.dir}/checkconfigconsistency.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/open-xchange-groupware" file="${patch.bin.dir}/open-xchange-groupware.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/oxreport" file="${patch.bin.dir}/oxreport.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/resetversion" file="${patch.bin.dir}/resetversion.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/runupdate" file="${patch.bin.dir}/runupdate.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/schemasandversions" file="${patch.bin.dir}/schemasandversions.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/forceupdatetask" file="${patch.bin.dir}/forceupdatetask.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/restoregabdefaults" file="${patch.bin.dir}/restoregabdefaults.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/listExecutedUpdateTasks" file="${patch.bin.dir}/listExecutedUpdateTasks.in" overwrite="true"/>
        <move tofile="${patch.bin.dir}/logincounter" file="${patch.bin.dir}/logincounter.in" overwrite="true"/>
        <replace dir="${patch.bin.dir}" includes="checkconsistency checkconfigconsistency open-xchange-groupware oxreport resetversion runupdate schemasandversions listExecutedUpdateTasks logincounter forceupdatetask restoregabdefaults">
            <replacefilter token="@oxfunctions@" value="${common.conf.install.dir}/oxfunctions.sh"/>
            <replacefilter token="@oxscriptconf@" value="${conf.subst.patch}/ox-scriptconf.sh"/>
            <replacefilter token="@prefix@" value="${prefix}"/>
            <replacefilter token="@consolelogfile@" value="${log.install.dir}/open-xchange-console.log"/>
            <replacefilter token="@configini@" value="${conf.subst.patch}/osgi/config.ini"/>
            <replacefilter token="@bundle.d@" value="${gwbundle.d}"/>
        </replace>
    </target>

    <!-- generate manifest as shipped in jar -->
    <target name="manifest">
        <copy file="META-INF/MANIFEST.MF" tofile="META-INF/MANIFEST.MF.package"/>
        <manifest mode="update" file="META-INF/MANIFEST.MF.package">
            <attribute name="Codename" value="${codename}"/>
        </manifest>
    </target>

</project>
