<!-- Created by Jean Bovet -->

<project name="distribution" default="distribute" basedir=".">

    <property file="build.properties" />

    <property name="dist.dir" value="${basedir}/dist" />
    <property name="build.dir" value="${dist.dir}/build" />
    <property name="lib.dir" value="${basedir}/lib" />
    <property name="src.dist.dir" value="${dist.dir}/src" />
    <property name="osx.dir" value="${basedir}/osx" />
    <property name="osx.build.dir" value="${build.dir}/osx" />

    <property name="jar.file" value="${dist.dir}/antlrworks-${version}.jar" />

    <property name="plugin.build.dir" value="${build.dir}/plugin" />
    <property name="plugin.lib.dir" value="${plugin.build.dir}/ANTLRWorksIDEAPlugIn/lib" />
    <property name="plugin.jar.file" value="${plugin.lib.dir}/antlrworks-${version}.jar" />

    <path id="classpath">
        <fileset dir="${aw.lib}" includes="*.jar"/>
    </path>

    <condition property="is-macos">
        <os family="mac" />
    </condition>

    <condition property="intellij.available">
        <available file="${intellij.lib}" />
    </condition>

    <target name="clean">
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
    </target>

    <target name="compile-mac" if="is-macos" description="Compile for the Mac">
        <mkdir dir="${build.dir}"/>
        <javac
                destdir="${build.dir}"
                source="1.5"
                target="1.5"
                excludes="org/antlr/works/test/**">
            <classpath refid="classpath"/>
            <src path="${aw.source}"/>
            <src path="${xj.source}"/>
        </javac>
    </target>

    <target name="compile-other" unless="is-macos" description="Compile for generic OS">
        <mkdir dir="${build.dir}"/>
        <javac
                destdir="${build.dir}"
                source="1.5"
                target="1.5"
                excludes="org/antlr/works/test/**, **/XJApplicationMacOS.java">
            <classpath refid="classpath"/>
            <src path="${aw.source}"/>
            <src path="${xj.source}"/>
        </javac>
    </target>

    <target name="build" depends="compile-mac, compile-other" description="Build antlrworks.jar">
        <mkdir dir="${dist.dir}"/>

        <jar jarfile="${jar.file}">
            <fileset dir="${aw.source}" includes="**/*.stg, **/*.st, **/*.txt, **/*.properties, **/*.png"/>
            <fileset dir="${xj.source}" includes="**/*.stg, **/*.st, **/*.txt, **/*.properties, **/*.png"/>
            <fileset dir="${build.dir}" includes="**/*.class"/>

            <zipfileset src="${aw.lib}/${antlr2.jar}" excludes="**/*.txt"/>
            <zipfileset src="${aw.lib}/${antlr3.jar}" excludes="**/*.txt"/>
            <zipfileset src="${aw.lib}/${jgoodies.jar}"/>
            <zipfileset src="${aw.lib}/${st.jar}" excludes="**/*.txt"/>

            <manifest>
                <attribute name="Version" value="${version}"/>
                <attribute name="Main-Class" value="org.antlr.works.IDE"/>
            </manifest>
        </jar>
    </target>

    <target name="build-plugin" if="intellij.available">
        <mkdir dir="${plugin.build.dir}"/>
        <mkdir dir="${plugin.lib.dir}"/>

        <javac
                destdir="${plugin.build.dir}"
                source="1.5"
                target="1.5">
            <classpath refid="classpath"/>
            <classpath>
                <fileset dir="${intellij.lib}" includes="*.jar"/>
                <fileset file="${jar.file}"/>
            </classpath>
            <src path="${plugin.source}"/>
        </javac>

        <jar jarfile="${plugin.jar.file}">
            <fileset dir="${plugin.build.dir}" includes="**/*.class"/>
            <zipfileset src="${jar.file}"/>
            <fileset dir="${plugin.source}/org/antlr/works/plugin/intellij" includes="**/*.xml"/>
            <fileset dir="${plugin.source}" includes="**/*.stg, **/*.st, **/*.txt, **/*.properties, **/*.png"/>

            <manifest>
                <attribute name="Version" value="${version}"/>
            </manifest>
        </jar>

        <zip destfile="${dist.dir}/ANTLRWorksIDEAPlugIn.zip"
                basedir="${plugin.build.dir}"
                excludes="org/**">
        </zip>
    </target>

    <target name="build-noplugin" unless="intellij.available">
        <echo>The plugin for IntelliJ was not build because the path to IntelliJ does not exist</echo>
    </target>

    <target name="run" description="Launch ANTLRWorks">
        <java jar="${jar.file}"
              fork="true"
              spawn="true"
                >
            <jvmarg value="-Xmx400m"/>
        </java>
    </target>

    <target name="prepare-osx">
        <copy todir="${osx.build.dir}">
            <fileset dir="${osx.dir}" excludes="Info.plist"/>
        </copy>

        <copy file="${jar.file}" tofile="${osx.build.dir}/ANTLRWorks.app/Contents/Resources/Java/antlrworks.jar" overwrite="true"/>

        <exec executable="chmod" dir="${osx.build.dir}/ANTLRWorks.app/Contents/MacOS/">
            <arg value="+x"/>
            <arg value="JavaApplicationStub"/>
        </exec>

        <copy file="${osx.dir}/Info.plist" tofile="${osx.build.dir}/ANTLRWorks.app/Contents/Info.plist" overwrite="true">
            <filterset>
                <filter token="VERSION" value="${version}"/>
            </filterset>
        </copy>

        <!-- Use the Unix zip command because the ant zip task does not preserve the Unix permission needed
        for the application to launch on Mac OS X -->
        <exec executable="zip" dir="${osx.build.dir}">
            <arg value="-r"/>
            <arg value="${dist.dir}/antlrworks-${version}.zip"/>
            <arg value="ANTLRWorks.app"/>
        </exec>
    </target>

    <target name="distribute" depends="clean, build, build-plugin, build-noplugin, prepare-osx">
    </target>

</project>
