<project name="freemind" default="dist" basedir=".">
	<property name="ver" value="0_8_1" />
	<property name="src" value="." />
	<property name="src_base_for_post" value="../" />
	<property name="build" value="../bin/classes" />
	<property name="dist" value="../bin/dist" />
	<property name="dist.lib" value="${dist}/lib" />
	<property name="dist_macos_name" value="Mac_OS_X_Freemind-${ver}" />	
	<property name="dist_macos_base" value="../bin/dist_macos" />	
	<property name="dist_macos" value="${dist_macos_base}/${dist_macos_name}" />	
	<property name="dist_macos_info" value="${dist_macos}/FreeMind.app/Contents/Info.plist"></property>
    <property name="FM.jar" value="${dist.lib}/freemind.jar"/>
	<property name="post" value="../post" />
	<property name="debug" value="on" />
	<property name="java_source_version" value="1.2" />
	<property name="java_target_version" value="1.2" />
	<property name="build.compiler" value="modern" />
	<property name="ant.dir" value="lib/ant/lib"/>
	<property name="jarbundler.jar" value = "jarbundler-1.4.jar"/>

    <taskdef name="jarbundler"
               classpath="${src}/lib/${jarbundler.jar}"
               classname="com.loomcom.ant.tasks.jarbundler.JarBundler" />


	
	
	<!-- begin generator part -->
	<property name="jaxb" value="${ant.dir}/jaxb-xjc.jar:${ant.dir}/jaxb-api.jar:${ant.dir}/jaxb-impl.jar:${ant.dir}/jaxb-libs.jar:${ant.dir}/namespace.jar:${ant.dir}/relaxngDatatype.jar:${ant.dir}/xsdlib.jar:${ant.dir}/jax-qname.jar"/>
	<property name="xml" value="${ant.dir}/sax.jar:${ant.dir}/dom.jar"/> 
	<property name="classpath" value="${jaxb}:${xml}:lib/commons-lang-2.0.jar:lib/forms-1.0.5.jar"/>
	<property name="xsd.dir" value="." />
	<property name="gen.dir" value="generated" />
	<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask" 
	   classpath="${jaxb}" />

  <!-- uses property ${gen.file} as the directory name of the outputs. -->
  <target name="xmlbind.checkStatusOfGeneration">
    <uptodate property="xmlbind.isUpToDate"
              srcfile="${xsd.dir}/${xsd.file}"
              targetfile="${gen.dir}/${gen.file}/freemind/controller/${gen.file}/generated/instance/jaxb.properties"/>
  </target>

  <!-- uses property ${gen.file} as the directory name of the outputs. -->
  <target name="xmlbind" 
          unless="xmlbind.isUpToDate" 
          depends="xmlbind.checkStatusOfGeneration">
    <mkdir dir="${gen.dir}/${gen.file}" />
    <xjc schema="${xsd.dir}/${xsd.file}"
         target="${gen.dir}/${gen.file}" 
         package="freemind.controller.${gen.file}.generated.instance" />
  </target>

  <!-- uses property ${gen.file} as the directory name of the outputs. -->
  <target name="-buildgen">
    <javac srcdir="${gen.dir}/${gen.file}"
           destdir="${build}"
           debug="${debug}"
    	   source="${java_source_version}"
	       target="${java_target_version}"
    	>
      <classpath path="${classpath}" />
      <include name="**/*.java" />
    </javac>
    <copy todir="${build}/freemind/controller/${gen.file}/generated" >
      <fileset dir="${gen.dir}/${gen.file}/freemind/controller/${gen.file}/generated" >
        <include name="**/jaxb.properties" />
        <include name="**/*.ser" />
      </fileset>
    </copy>
  </target>


  <target name="gen">
    <antcall target="xmlbind">
      <param name="gen.file" value="actions"/>
      <param name="xsd.file" value="freemind_actions.xsd"/>
    </antcall>
  </target>



  <target name="build-generated">
    <antcall target="-buildgen">
      <param name="gen.file" value="actions"/>
      <param name="xsd.file" value="freemind_actions.xsd"/>
    </antcall>
  </target>
	<!-- end generator part -->

	<target name="build" depends="gen">
		<!-- Perform Mac Changes as a plugin. -->
		<exec dir="${src}/accessories/plugins" executable="ln" os="Mac OS X">
			<arg value="-s"/>
			<arg value="MacChanges.java.not_here"/>
			<arg value="MacChanges.java"/>
		</exec>
		<exec dir="${src}/accessories/plugins" executable="ln" os="Mac OS X">
			<arg value="-s"/>
			<arg value="MacChanges.xml.not_here"/>
			<arg value="MacChanges.xml"/>
		</exec>
		<!-- end mac changes. -->
		<mkdir dir="${build}"/>
	    <!-- build generated files -->
	    <antcall target="build-generated"/>
	    <!-- end build generated files -->
		<javac	srcdir="${src}"
			destdir="${build}"
			classpath="${classpath}"
			debug="${debug}"
    	   source="${java_source_version}"
	       target="${java_target_version}"
			> 
	      <exclude name="${gen.dir}/**" />
	      <exclude name="accessories/plugins/**" />
	      <exclude name="plugins/**" />
		</javac>
		<ant antfile="accessories/plugins/build.xml" target="build"/>
		<ant antfile="plugins/build.xml" target="build"/>
	</target>

	<target name="dist" depends="jar">

		<copy todir="${dist}"> 
			<fileset dir="${src}"
				includes="freemind.bat,freemind.sh,license,patterns.xml"
			/>
		</copy>
        <!-- user.properties are placed in the documents folder. To avoid trouble, make a short comment here.  -->
        <copy file="${src}/user_comment.properties" tofile="${dist}/user.properties"/>

        <chmod file="${dist}/freemind.sh" perm="u+rx"/>
		<copy todir="${dist}"> 
			<fileset dir="${src}/windows-launcher"
				includes="Freemind.exe"
			/>
		</copy>
		<mkdir dir="${dist}/accessories" />
		<mkdir dir="${dist}/plugins" />
		<copy todir="${dist}/accessories" > 
			<fileset dir="${src}/accessories" excludes="plugins" includes="*"/>
			<!-- exclude jars too!
			<fileset dir="${build}/accessories"/> -->
		</copy> 
		<!-- third party libs -->
		<mkdir dir="${dist.lib}" />
		<copy todir="${dist.lib}"> 
			<fileset dir="${src}/lib">
				<exclude name="**/jaxb-xjc.jar"/>
				<exclude name="${jarbundler.jar}"/>
			</fileset>
		</copy>
		<ant antfile="accessories/plugins/build.xml" target="dist"/>
		<ant antfile="plugins/build.xml" target="dist"/>
		<!-- Mac OS X -->
	    <condition property="isMacOs">
		  <os family="mac"/>
		</condition>
		<antcall target="dist_macos"/>
	</target>

	<target name="dist_macos" if="isMacOs">
		<delete dir="${dist_macos}"/>
		<mkdir dir="${dist_macos}"/>
		<!-- change freemind.properties -->
		<copy todir="${dist_macos}" file="${src}/freemind.properties"/>
		<replaceregexp byline="true" file="${dist_macos}/freemind.properties" match="^keystroke(.*)=(.*)\bcontrol\b" replace="keystroke\1=\2meta" flags="gim"></replaceregexp>
		<replaceregexp byline="true" file="${dist_macos}/freemind.properties" match="^keystroke(.*)=(.*)\binsert\b" replace="keystroke\1=\2TAB" flags="gim"></replaceregexp>
		<!-- update jar -->
		<jar file="${FM.jar}" update="true" basedir="${dist_macos}"><fileset dir="${dist_macos}"><include name="${dist_macos}/freemind.properties"/></fileset></jar>
		  <jarbundler dir="${dist_macos}"
		                    name="FreeMind"
		                    mainclass="freemind.main.FreeMind"
		                    icon="${src}/images/FreeMindWindowIcon.icns"
		                    version="${ver}"
		                    infostring="FreeMind ${ver} application (c) 2008"
		                    aboutmenuname="FreeMind"
		                    bundleid="freemind.main.FreeMind"
		                    signature="FM"
		                    jvmversion="1.4+"
		                    liveresize="true"
		                    screenmenu="true" 
		  				    > 
		  	<jarfileset dir="${dist}">
		            <include name="**/*.jar" />
		            <exclude name="**/CVS" />
		            <exclude name="accessories/**" />
		            <exclude name="plugins/**" />
          	</jarfileset>
        </jarbundler>
		<!-- Associate Freemind with mm -->
		<xslt style="mac_file_association.xslt" 
			in ="${dist_macos_info}" 
			out="${dist_macos_info}2"/>
		<move file="${dist_macos_info}2"
			  tofile="${dist_macos_info}"/>
		<!-- Done. -->
		<mkdir dir="${dist_macos}/accessories" />
		<copy todir="${dist_macos}/accessories"> 
			<fileset dir="${dist}/accessories"/>
		</copy> 
		<!-- link accessories in app -->
		<exec dir="${dist_macos}/FreeMind.app/Contents/Resources/Java" executable="ln" os="Mac OS X">
			<arg value="-s"/>
			<arg value="../../../../accessories/"/>
			<arg value="accessories"/>
		</exec>
		<mkdir dir="${dist_macos}/plugins" />
		<copy todir="${dist_macos}/plugins"> 
			<fileset dir="${dist}/plugins"/>
		</copy> 
	</target>

	<target name="post" depends="dist,browser,doc">
		<mkdir dir="${post}" />
		<tar tarfile="${post}/freemind-bin.tar"
			basedir="${dist}"
			longfile="gnu"
			excludes="browser/,doc/javadoc/,plugins/**" />
		<tar tarfile="${post}/freemind-src.tar"
			longfile="gnu"
			basedir="${src_base_for_post}"
			includes="freemind/**,.classpath,.project" />
		<tar tarfile="${post}/freemind-browser.tar"
			longfile="gnu"
			basedir="${dist}/browser" />
		<gzip src="${post}/freemind-bin.tar"
			zipfile="${post}/freemind-bin-${ver}.tar.gz" />
		<gzip src="${post}/freemind-src.tar"
			zipfile="${post}/freemind-src-${ver}.tar.gz" />
		<gzip src="${post}/freemind-browser.tar"
			zipfile="${post}/freemind-browser-${ver}.tar.gz" />
		<delete>
			<fileset dir="${post}" includes="*.tar" />
		</delete> 
		<zip destfile="${post}/freemind-bin-${ver}.zip"
			basedir="${dist}"
			excludes="browser/,doc/javadoc/,plugins/**" />
		<!-- fc, 14.2.2005: new max export -->
		<zip destfile="${post}/freemind-bin-max-${ver}.zip"
			basedir="${dist}"
			excludes="browser/,doc/javadoc/" />
		<zip destfile="${post}/freemind-src-${ver}.zip"
			basedir="${src_base_for_post}"
			includes="freemind/**,.classpath,.project" />
		<zip destfile="${post}/freemind-browser-${ver}.zip"
			basedir="${dist}/browser" />
		<ant antfile="plugins/build.xml" target="post"/>
		<antcall target="post_macos"></antcall>
	</target>

	<target name="post_macos">
		<!-- doc -->
		<mkdir dir="${dist_macos}/doc" />
		<copy todir="${dist_macos}/doc/" file="${dist}/doc/freemind.mm" />

		<exec dir="." executable="bash" os="Mac OS X">
			<arg value="./mkdmg.sh"/>
			<arg path="${dist_macos}"/>
		</exec>
		<exec dir="." executable="mv" os="Mac OS X">
			<arg path="/tmp/${dist_macos_name} Archive.dmg"/>
			<arg path="${post}/${dist_macos_name}.dmg"/>
		</exec>

	</target>
	
	<target name="jar" depends="build">
		<mkdir dir="${dist.lib}" />
		<jar jarfile="${FM.jar}"
		     manifest="${src}/MANIFEST.MF"
			update="true">
			<fileset dir="${build}">
				<exclude name="accessories/**"/> 
				<exclude name="plugins/**"/> 
			</fileset>
			<fileset dir="${src}">
				<include name="images/*"/>
				<include name="images/icons/*.png"/>
				<include name="freemind.properties"/>
				<include name="user.properties"/>
				<include name="patterns.xml"/>
				<include name="Resources*"/>
				<include name="mindmap_menus.xml"/>
				<include name="**/freemind_version_updater.xslt"/>
			</fileset>
		</jar>
	</target>

	<target name="browser" depends="build">
		<mkdir dir="${dist}/browser" />
		<jar jarfile="${dist}/browser/freemindbrowser.jar">
			<fileset dir="${build}">
				<include name="freemind/extensions/*"/>
				<include name="freemind/view/*"/>
				<include name="freemind/view/mindmapview/*"/>
				<include name="freemind/main/*"/>
				<include name="freemind/controller/*"/>	
				<include name="freemind/modes/*"/>
				<include name="freemind/modes/browsemode/*"/>
				<include name="accessories/plugins/**"/>
			</fileset>
			<fileset dir="${src}">
				<include name="images/*"/>
				<include name="images/icons/*.png"/>
				<include name="*.properties"/>
				<include name="accessories/plugins/**"/>
			</fileset>
		</jar>
                <copy file="${src}/freemind.properties" tofile="${src}/user.properties"/>
		<copy todir="${dist}/browser" >
			<fileset dir="${src}/html">
				<include name="*.*html"/>
			</fileset>
		</copy>
	</target>

	<target name="doc">
		<mkdir dir="${dist}/doc" />
		<mkdir dir="${dist}/doc/javadoc" />
		<copy todir="${dist}/doc" file="${src}/doc/freemind.mm"/>
		<javadoc sourcepath="${src}"
		         destdir="${dist}/doc/javadoc"
		         packagenames="freemind.*" />
	</target>

	<target name="clean">
		<delete dir="${build}"  quiet="true"/>
		<delete dir="${dist}"  quiet="true"/>
		<delete dir="${dist_macos_base}"  quiet="true"/>
		<delete dir="${gen.dir}"  quiet="true"/>
		<delete dir="${post}"  quiet="true"/>
                <delete>
                  <fileset defaultexcludes="no" dir="${src}" includes="**/*~"/>
                </delete>
	</target>

  <target name="run" depends="dist">
    <java classname="freemind.main.FreeMind"
          fork="yes"
          dir="${dist}">
<!--    <jvmarg value="-Djava.util.logging.config.file=logging.properties" /> -->
    <classpath>
       <!--<pathelement location="${dist}"/>-->
       <pathelement location="${FM.jar}"/>
       <pathelement path="${xml}"/>
       <pathelement path="${jaxb}"/>
       <pathelement location="${FM.jar}"/>
    </classpath>
    </java>
  </target>

	<!-- does not work!! -->
	<target name="val">
		<xmlvalidate lenient="false"
		  failonerror="no" warn="true" classname="org.apache.xerces.parsers.SAXParser"
            classpath="plugins/svg/xerces_2_5_0.jar">
			<fileset dir="." includes="**/*.mm"/>
		  <attribute name="http://apache.org/xml/features/validation/schema"
		  value="true"/>
		  <attribute name="http://xml.org/sax/features/namespaces" value="true"/>
		  <property
		  name="http://apache.org/xml/properties/schema/external-schemaLocation"
		  value="freemind.xsd"/>
		</xmlvalidate>
	</target>
	
</project>

