<?xml version="1.0" encoding="UTF-8"?>
<project name="com.openexchange.common" default="install" basedir=".">

    <!-- Customizable values -->
    <property name="destdir" value=""/>
    <property name="prefix" value="/opt/open-xchange"/>

    <!-- Constants -->
    <property name="tmp.dir" value="tmp"/>
    <property name="etcdir"  value="etc"/>
    <property name="libDir" 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}"/>
    <property name="distpackage.name" value="open-xchange-common"/>
    <property name="composite.name" value="${distpackage.name}-${version}"/>

    <fileset dir="${libDir}" id="3rdpartyjars">
        <include name="*.jar"/>
        <exclude name="CVS"/>
    </fileset>

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

    <fileset dir="jars" id="framework">
        <include name="javax.servlet*"/>
        <include name="org.apache.commons.*"/>
        <include name="org.eclipse.equinox*"/>
        <include name="org.eclipse.osgi*"/>
        <include name="geronimo*"/>
        <include name="org.apache.servicemix*"/>
    </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"/>

    <!-- JAR names for classpath -->
    <property name="commonsloggingjar" value="org.apache.commons.logging_1.0.4.v200706111724.jar"/>

    <!-- Classpath -->
    <path id="project.classpath">
        <pathelement location="jars/${commonsloggingjar}"/>
        <fileset refid="3rdpartyjars"/>
    </path>

    <!-- Compile -->
    <target name="prepare">
        <mkdir dir="${output..}"/>
        <mkdir dir="${tmp.dir}"/>
    </target>

    <target name="compile" depends="prepare">
        <javac srcdir="${source..}" destdir="${output..}" debug="true" encoding="US-ASCII" source="1.5" compiler="javac1.5" target="1.5">
            <classpath refid="project.classpath" />
        </javac>
    </target>

    <target name="jar" depends="prepare,compile">
        <jar jarfile="${tmp.dir}/common.jar">
            <fileset dir="${output..}"/>
        </jar>
    </target>

    <!-- Install targets -->
    <target name="install" depends="installJars,installConfig,installScripts"/>

    <target name="installJars" depends="jar">
        <property name="bundlesdestdir" value="${destdir}${bundlesdir}"/>
        <!-- Framework -->
        <mkdir dir="${bundlesdestdir}"/>
        <copy todir="${bundlesdestdir}">
            <fileset refid="framework"/>
        </copy>
        <!-- Common Bundle -->
        <property name="commondir" value="${bundlesdestdir}/${bundle.name}"/>
        <mkdir dir="${commondir}"/>
        <copy todir="${commondir}" file="${tmp.dir}/common.jar"/>
        <mkdir dir="${commondir}/lib"/>
        <copy todir="${commondir}/lib">
            <fileset refid="3rdpartyjars"/>
        </copy>
        <copy todir="${commondir}">
            <fileset refid="metainf"/>
        </copy>
    </target>

    <target name="installConfig">
        <mkdir dir="${destdir}/${gwbundle.d}"/>
        <echo file="${destdir}/${gwbundle.d}/org.eclipse.osgi.services.ini" message="${bundlesdir}/org.eclipse.osgi.services_3.2.100.v20100503.jar@start${line.separator}"/>
        <echo file="${destdir}/${gwbundle.d}/org.eclipse.equinox.event.ini" message="${bundlesdir}/org.eclipse.equinox.event_1.2.0.v20100503.jar@start${line.separator}"/>
        <echo file="${destdir}/${gwbundle.d}/org.apache.commons.cli.ini" message="${bundlesdir}/org.apache.commons.cli_1.0.0.v20080604-1500.jar@start${line.separator}"/>
        <echo file="${destdir}/${gwbundle.d}/org.apache.commons.lang.ini" message="${bundlesdir}/org.apache.commons.lang_2.4.0.v20081016-1030.jar@start${line.separator}"/>
        <echo file="${destdir}/${gwbundle.d}/org.apache.commons.logging.ini" message="${bundlesdir}/org.apache.commons.logging_1.0.4.v200706111724.jar@start${line.separator}"/>
        <echo file="${destdir}/${gwbundle.d}/geronimo.annotation.ini" message="${bundlesdir}/geronimo-annotation_1.0_spec-1.1.1.jar@start${line.separator}"/>
    	<echo file="${destdir}/${gwbundle.d}/org.apache.servicemix.bundles.jaxb.ini" message="${bundlesdir}/org.apache.servicemix.bundles.jaxb-impl-2.1.6_1.jar@start${line.separator}"/>
    	<echo file="${destdir}/${gwbundle.d}/org.apache.servicemix.bundles.woodstox.ini" message="${bundlesdir}/org.apache.servicemix.bundles.woodstox-3.2.7_1.jar@start${line.separator}"/>
    	<echo file="${destdir}/${gwbundle.d}/org.apache.servicemix.specs.jaxb-api.ini" message="${bundlesdir}/org.apache.servicemix.specs.jaxb-api-2.1-1.3.0.jar@start${line.separator}"/>
    	<echo file="${destdir}/${gwbundle.d}/org.apache.servicemix.specs.stax-api.ini" message="${bundlesdir}/org.apache.servicemix.specs.stax-api-1.0-1.3.0.jar@start${line.separator}"/>
    	<echo file="${destdir}/${gwbundle.d}/javax.servlet.ini" message="${bundlesdir}/javax.servlet_2.4.0.v200706111738.jar@start${line.separator}"/>
        <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}/org.eclipse.osgi.services.ini" message="${bundlesdir}/org.eclipse.osgi.services_3.2.100.v20100503.jar@start${line.separator}"/>
        <echo file="${destdir}/${adbundle.d}/org.eclipse.equinox.event.ini" message="${bundlesdir}/org.eclipse.equinox.event_1.2.0.v20100503.jar@start${line.separator}"/>
        <echo file="${destdir}/${adbundle.d}/org.apache.commons.cli.ini" message="${bundlesdir}/org.apache.commons.cli_1.0.0.v20080604-1500.jar@start${line.separator}"/>
        <echo file="${destdir}/${adbundle.d}/org.apache.commons.lang.ini" message="${bundlesdir}/org.apache.commons.lang_2.4.0.v20081016-1030.jar@start${line.separator}"/>
        <echo file="${destdir}/${adbundle.d}/org.apache.commons.logging.ini" message="${bundlesdir}/org.apache.commons.logging_1.0.4.v200706111724.jar@start${line.separator}"/>
        <echo file="${destdir}/${adbundle.d}/javax.servlet.ini" message="${bundlesdir}/javax.servlet_2.4.0.v200706111738.jar@start${line.separator}"/>
        <echo file="${destdir}/${adbundle.d}/${Bundle-SymbolicName}.ini" message="${bundlesdir}/${bundle.name}@start${line.separator}"/>
    </target>

    <target name="installScripts">
        <mkdir dir="${destdir}/${prefix}/${etcdir}"/>
        <copy todir="${destdir}/${prefix}/${etcdir}">
            <fileset dir="etc">
                <include name="*"/>
                <exclude name="CVS"/>
            </fileset>
        </copy>
    </target>

    <target name="installDist">
        <property name="3rdpartydir" value="${destdir}/3rdparty"/>
        <copy todir="${3rdpartydir}">
            <fileset dir="lib/dist">
                <include name="*"/>
                <exclude name="CVS"/>
            </fileset>
        </copy>
    </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>

    <!-- Clean -->
    <target name="clean">
        <delete file="${disttar}"/>
        <delete dir="${tmp.dir}"/>
        <delete dir="${output..}"/>
    </target>

</project>
