<?xml version="1.0" encoding="iso-8859-1"?>

<!--
	Copyright (c) 2004-2006 The Dojo Foundation, Licensed under the Academic
	Free License version 2.1 or BSD licenses
-->
<!--
	TODO:
		- single-file builds
		- task for firing off jslink.pl against a set of "app files". TBD:
		  should we support these as HTML? Or just JS files?
		  NOTE: the following command issued from the top-level seems to work correctly:
		  	perl buildscripts/jslink.pl -pre cat -i release/testUsage.js -l src/bootstrap1.js -l src/bootstrap2.js -l src/hostenv_browser.js -l src/io/IO.js -l src/io/BrowserIO.js -o - > release/minimal.js
		  This correctly "links" the files together, but mangles some
		  identifiers and places anonymous inner functions at the wrong places
		  OTOH, running the linker over the pre-compressed version of the file doesn't seem to work at all
		- a task for running the compressor across the user's files as well as our own
		- doc generation (pending new mods to Rhino)
-->
<project name="dojo" default="help" basedir=".">
	<description>build the Dojo toolkit</description>

	<!-- project properties -->
	<property name="project" value="dojo"/>
	<property name="build_dir" value="../build"/>
	<property name="root" value=".."/>
	<property name="src" value="src"/>
	<property name="version" value="0.0.0.dev"/>
	<property name="lang" value="en"/>
	<property name="releaseName" value="${project}"/>
	<property name="dojoLoader" value="default"/>
	<property name="release_dir" value="${root}/release/${releaseName}"/>
	<property name="docs_dir" value="${root}/documents"/>
	<property name="articles_dir" value="${docs_dir}/articles"/>
	<property name="release_docs_dir" value="${release_dir}/documents"/>
	<property name="rest_style_sheet" value="${docs_dir}/styles/dojo.css"/>
	<property name="rest_files" value=""/>
	<property name="website_dir" value="${docs_dir}/website"/>
	<property name="release_website_dir" value="/srv/www/htdocs"/>
	<property name="profile" value=""/>
	<property name="locales" value="en-gb,en-us,de-de,es-es,fr-fr,it-it,pt-br,ko-kr,zh-tw,zh-cn,ja-jp"/>
	<!-- FIXME: where should this list come from? -->

	<loadfile property="copyright" srcFile="./copyright.txt"/>
	<loadfile property="build_notice" srcFile="./build_notice.txt"/>

	<!-- help task -->
	<target name="help" depends="-fix-config">
		<echo>
build targets:
	clean - deletes all files in the ${release_dir} directory

	website - copies website files into the correct location and sets
		permissions correctly

	compress - copies the files in ${root}/${src} to the
		${release_dir}/${project}/${src} directory where it
		compresses all the .js files with a set of regular
		expressions and removes all non .js files

	gen-strip-docs - Strips out most comments,line feeds,etc in ALL js found
			in the genereated ${release_dir}.

	src - Take ./${src} and build the ${release_dir}/${project}/${src}_src
		directory. More or less a copy of the source files, documentation and
		all.

	docs - builds documentation in all formats from ./${src} and place it in ${release_docs_dir}

	test - builds and runs command line unit tests

	testScripting - tests the JS and Jython integration in the build process

	buildFlash - compiles a Flash ActionScript 2 file to use dojo.flash.comm; provide
	relative path to file using -Ddojo.flash.file.

	buildDojoFlash - compiles the dojo.flash package, including its testing
	and example files.

	buildDojoStorage - compiles the dojo.storage package

	**** all following build types can take an optional parameter in the form of: -Dversion='currentVer'
		where 'currentVer' is the version of ${project} being built, default is 'development'

	zip - zips the ${release_dir}/${project} directory for distribution
	tar - tars and gzips the ${release_dir}/${project} directory for distribution
	release - creates a full release of ${project}

	full build example:
		ant -Dversion=0.3.0 clean release

	build example with tarball:
		ant -Dversion=0.3.0 clean compress tar

	just generate documentation:
		ant -Dversion=0.3.0 clean docs

	full build, but without docs:
		ant -Dversion=0.3.0 -Ddocless=true clean release gen-strip-docs

		</echo>
	</target>
	<!-- end help task -->

	<!-- Defines the dojo test task. -->
	<taskdef name="dojo-test" classname="org.dojotoolkit.ant.DojoTestTask" classpath="lib/ant-dojotest.jar" />

	<!-- test task -->
	<target name="test"
		depends="-fix-config"
		description="builds and runs unit tests for Dojo under Rhino and JUM">
		<dojo-test dojosrc="../" testsrc="../tests"/>
	</target>
	<!-- end test task -->

	<!-- test-scripting task -->
	<target name="testScripting"
		depends="-fix-config"
		description="runs minimal test on our BSF+JS+Jython integration">
		<script language="javascript">
			<![CDATA[
			function print(){
				var args = [];
				for(var x=0; x<arguments.length; x++){
					args.push(arguments[x]);
				}
				java.lang.System.out.println.apply(java.lang.System.out, args);

			}

			function echo(msg, parentTask){
				var echo = dojo.createTask("echo");
				parentTask.addTask(echo);
				echo.setMessage(msg);
			}

			/*
			function load(){
				for(var x=0; x<arguments.length; x++){
					var scriptTask = dojo.createTask("script");
					scriptTask.setAttribute(); // FIXME: will this work?
				}
			}
			*/

			print("foo");
			echo("JavaScript (Rhino) integration is working!", testScripting);
		]]>
		</script>
		<script language="jython">
			<![CDATA[
import sys
# make the python standard library avialable
sys.path.append(project.resolveFile("lib/pyLib.zip").getPath())

# import re

echo = dojo.createTask("echo")
testScripting.addTask(echo)
echo.setMessage("Jython integration is working!");

print "look ma! Jython!"
]]>
		</script>
	</target>
	<!-- end testScripting task -->

	<!-- -check-config task -->
	<target name="-check-config"
		description="checks to make sure than we're sane before doing anything else">
		<available property="bsf_ok" file="${user.home}/.ant/lib/bsf.jar" />
		<available property="ant_bsf_ok" file="${user.home}/.ant/lib/ant-apache-bsf.jar" />
		<available property="jython_ok" file="${user.home}/.ant/lib/jython.jar" />
		<available property="jython_libs_ok" file="${user.home}/.ant/lib/pyLib.zip" />

		<condition property="config_ok">
			<and>
				<isset property="bsf_ok" />
				<isset property="ant_bsf_ok" />
				<isset property="jython_ok" />
				<isset property="jython_libs_ok" />
			</and>
		</condition>

	</target>
	<!-- end -check-config task -->

	<!-- -fix-config task -->
	<target name="-fix-config"
		depends="-check-config"
		unless="config_ok"
		description="fixes our ant classpath (if possible)">
		<copy todir="${user.home}/.ant/lib"
			preservelastmodified="true">
			<fileset dir="lib/">
				<include name="bsf.jar"/>
				<include name="jython.jar"/>
				<include name="pyLib.zip"/>
				<include name="js.jar"/>
				<include name="ant-apache-bsf.jar"/>
			</fileset>
		</copy>

		<echo message=""/>
		<echo message="+--------------------------------------------------------+" />
		<echo message="| Due to some horrendous design decisions by the authors |" />
		<echo message="| of Ant, it has been necessary to install some jar     |" />
		<echo message="| files to your ~/.ant/ directory. Given the nature of   |" />
		<echo message="| the problem, it will be necessary for you to re-run   |" />
		<echo message="| your build command.                                    |" />
		<echo message="|                                                        |" />
		<echo message="| The Dojo team apologies for this inconvenience.        |" />
		<echo message="|                                                        |" />
		<echo message="| The system will now exit.                              |" />
		<echo message="+--------------------------------------------------------+" />
		<echo message=""/>

		<fail message="Sorry, please re-run your build command, it should work now"/>
	</target>
	<!-- end -fix-config task -->

	<!-- website task -->
	<!--
		FIXME: 	need to build docs here and copy the HTML files generated from
				reST to website/docs/
	-->
	<target name="website"
		depends="-fix-config"
		description="'builds' the website and moves files into the correct directory, taking care to ensure that file permissions are correct">

		<copy todir="${release_website_dir}" overwrite="true">
			<fileset dir="${website_dir}">
				<exclude name="**/.svn*" />
				<include name="**/*" />
			</fileset>
		</copy>
		<antcall target="docs" />
		<copy todir="${release_website_dir}/docs/" overwrite="true">
			<fileset dir="${release_docs_dir}">
				<exclude name="**/.svn*" />
				<include name="*.html" />
			</fileset>
		</copy>
		<chgrp group="www" type="both">
			<fileset dir="${release_website_dir}" followsymlinks="false" includes="**/**" />
		</chgrp>
		<chmod perm="g+rwx" type="dir">
			<fileset dir="${release_website_dir}" followsymlinks="false" includes="**/**" />
		</chmod>
		<chmod perm="g+rw" type="file">
			<fileset dir="${release_website_dir}" followsymlinks="false" includes="**/**" />
		</chmod>
	</target>
	<!-- end website task -->

	<!-- FIXME: need to fix this task!! -->
	<!-- docs task -->
	<target name="docs"
		unless="docless"
		depends="-fix-config"
		description="'builds' the docs">
		<!-- first, clobber the docs directory and rebuild it -->
		<!--
			FIXME: how best can we check for staleness here? Shouldn't Ant be
			doing this for us? Clobbering entire dirs seems a little
			heavyweight.
		-->
		<delete dir="${release_docs_dir}"/>
		<mkdir dir="${release_docs_dir}" />
		<mkdir dir="${release_docs_dir}/requirements" />
		<script language="jython">
			<![CDATA[
import sys
# make the python standard library avialable
sys.path.append(project.resolveFile("lib/pyLib.zip").getPath())
sys.path.append(project.resolveFile(".").getPath())

# import re
import os
from buildUtil import *

buildRestFiles( dojo.getProperty("articles_dir"),
				dojo.getProperty("release_docs_dir"),
				dojo.getProperty("rest_style_sheet"),
				dojo.getProperty("rest_files"))
]]>
		</script>

		<!-- fix body tags -->
		<replaceregexp match="^&lt;body&gt;$" byline="true"
			replace="&lt;body class=&quot;docs&quot;&gt;" flags="g">
			<fileset dir="${release_docs_dir}">
				<exclude name="**/.svn*" />
				<include name="**/*.html" />
			</fileset>
		</replaceregexp>

	</target>
	<!-- end docs task -->

	<!-- clean task -->
	<target name="clean"
		description="removes release files">
		<delete dir="${release_dir}"/>
	</target>
	<!-- end clean task -->

	<!-- gen-strip-docs task -->
	<target name="gen-strip-docs">

		<!-- strip /* */ (multiline) comments -->
		<replaceregexp match="\/\*(.*?)\*\/" replace="" byline="false" flags="sg">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>

		<!-- strip // comments -->
		<replaceregexp byline="false"  flags="g">
			<regexp pattern="([^:(&quot;.'\\])(\/\/.*)|^\/\/.*"/>
			<substitution expression="\1"/>
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>

		<replaceregexp match="^[ \t]+|[ \t]+$" replace="" byline="false"  flags="mg">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>

		<!-- clobber leading and trailing DocBook comment -->
		<replaceregexp match="^&lt;\!--" replace="" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>
		<replaceregexp match="--&gt;$" replace="" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>

		<replaceregexp match="(\r)" replace="" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>

		<replaceregexp match="\n+" replace="NWNEWLINEHERE" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>

		<!-- add copyright notice -->
		<!--
		<replaceregexp match="^" byline="false" replace="${copyright}">
			<fileset dir="${destdir}">
				<include name="**/*.js" />
				<include name="**/*.as" />
				<not>
					<contains text="copyright" casesensitive="no"  />
				</not>
			</fileset>
		</replaceregexp>
		-->

		<replace dir="${release_dir}">
			<include name="**/*.js"/>
			<replacetoken>NWNEWLINEHERE</replacetoken>
			<replacevalue><![CDATA[
]]></replacevalue>
		</replace>

		<replaceregexp match=";\n*" replace=";" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>
		<replaceregexp match="\{(\s*)\n" replace="\{" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>
		<replaceregexp match=",(\n)" replace="," byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>
		<replaceregexp match="}(\n*)(\t*)\s*}" replace="}}" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>
		<replaceregexp match="\n*}" replace="}" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>
	</target>
	<!-- end gen-strip-docs task -->

	<!-- src task -->
	<target name="src"
		depends="-fix-config"
		unless="nosrc"
		description="creates a copy of the source files for distribution">

		<property name="ddir" value="${release_dir}/${project}/${src}_src"/>
		<!-- make sure our dir structure is right -->
		<delete dir="${ddir}" />
		<mkdir dir="${ddir}" />

		<copy todir="${ddir}" preservelastmodified="true">
			<fileset dir="./${src}" includes="**" />
		</copy>

		<!-- make sure we're dealing with unix newlines -->
		<fixcrlf srcdir="${ddir}"
			eol="lf" eof="remove"
			includes="**/*.js" />

		<!-- ensure everyone can read -->
		<chmod perm="ugo+rx" type="dir" dir="${release_dir}" includes="**" />
		<chmod perm="ugo+r" type="file" dir="${release_dir}" includes="**/*.*" />
	</target>
	<!-- end src task -->

	<!-- -set-profile-deps task -->
	<target name="-set-profile-deps" depends="-fix-config"
		description="populates the contents of the dependency list for a given profile">
		<!--
			FIXME: this is just a stub! need to fill it in!
		-->
		<script language="javascript">
			<![CDATA[
			function echo(msg){
							var echo = dojo.createTask("echo");
							echo.setMessage(msg);
							echo.execute();
						}
			var pf = dojo.getProperty("profileFile");
			if((!pf)||(pf=="null")||(pf=="")){
				// echo("profile: "+dojo.getProperty("profile"), compress);
				var prof = dojo.getProperty("profile");
				if((prof)&&(prof!="null")&&(prof!="")){
					dojo.setNewProperty("profileFile", "profiles/"+prof+".profile.js");
				}else{
					dojo.setNewProperty("profileFile", "getDependencyList.js");
				}
			}
		]]>
		</script>

		<!-- FIXME: need to actually get a list of js files to be processed  -->

		<java jar="./lib/custom_rhino.jar" failonerror="true"
			fork="true" outputproperty="concatFilesProp" logError="true">
			<arg value="-opt" />
			<arg value="-1" />
			<arg value="-f" />
			<arg value="${profileFile}" />
			<sysproperty key="DOJO_LOADER" value="${dojoLoader}" />
		</java>

		<script language="javascript">
			<![CDATA[
			var currentTask = dojo.getTargets().get("-set-profile-deps");
			var files = dojo.getProperty("concatFilesProp");
			dojo.setNewProperty("profileDeps", files);
		]]>
		</script>
	</target>
	<!-- end -set-profile-deps task -->

	<!-- list-profile-deps task -->
	<target name="list-profile-deps" depends="-fix-config, -set-profile-deps"
		description="lists out the contents of the file dependencies">
		<echo>${profileDeps}</echo>
	</target>
	<!-- end list-profile-deps task -->

	<!-- list-profile-sizes task -->
	<target name="list-profile-sizes" depends="-fix-config, -set-profile-deps"
		description="lists out the contents of the file dependencies">
		<script language="javascript">
			<![CDATA[
			function echo(msg){
				var echo = dojo.createTask("echo");
				echo.setMessage(msg);
				echo.execute();
			}

			var files = new String(dojo.getProperty("profileDeps"));
			var outparts = files.split("\n");
			for(var x=0; x<outparts.length; x++){
				if(outparts[x][outparts[x].length-1]==","){
					outparts[x] = outparts[x].substring(0, outparts[x].length-1);
				}
				var flen  = parseInt(new java.io.File(new java.lang.String(outparts[x])).length());
				echo(((flen>=1024) ? parseInt(flen/1024)+"K" : flen+"B") +": \t"+ outparts[x]);
			}
		]]></script>
		<!-- <echo>${profileDeps}</echo> -->
	</target>
	<!-- end list-profile-sizes task -->

	<!-- compress task -->
	<target name="compress" depends="-fix-config, list-profile-deps"
		description="runs compression code over release JS files">
		<echo>profileFile: ${profileFile}</echo>

		<!-- Create the release dir if it doesn't exist. -->
		<mkdir dir="${release_dir}"/>
		<!-- write the list of files included to the release directory -->
		<echo file="${release_dir}/build.txt">
Files baked into this package:

${profileDeps}

		</echo>

		<!-- now write 'em all to a single file -->
		<concat fixlastline="yes" eol="crlf" destfile="${release_dir}/source.__package__.js">
			<!--
		<concat fixlastline="yes" eol="crlf" destfile="${release_dir}/dojo.js">
		-->
			<filelist
				dir="."
				files="${profileDeps}" />
		</concat>
		<!--
			clobber requireIf and requireAfterIf statements since they
			aren't applicable in build
		-->
		<replaceregexp match="dojo.requireIf\((.*?)\);"
			byline="true" flags="g" file="${release_dir}/source.__package__.js">
			<substitution expression=""/>
		</replaceregexp>

		<replaceregexp match="dojo.requireAfterIf\((.*?)\);"
			byline="true" flags="g" file="${release_dir}/source.__package__.js">
			<substitution expression=""/>
		</replaceregexp>

		<!-- Set the version number -->
		<script language="jython">
			<![CDATA[
import sys
# make the python standard library avialable
sys.path.append(project.resolveFile("lib/pyLib.zip").getPath())
sys.path.append(project.resolveFile(".").getPath())

# import re
import os
from buildUtil import *

print "Setting version for Dojo to: " + dojo.getProperty("version")
replaceVersion(project.resolveFile(dojo.getProperty("release_dir") + "/source.__package__.js").getPath(), dojo.getProperty("version"))
print "Removing dojo.require calls in source.__package__.js."
removeRequires(project.resolveFile(dojo.getProperty("release_dir") + "/source.__package__.js").getPath())
]]>
		</script>

		<antcall target="-preload-resources">
			<param name="srcFile" value="${release_dir}/source.__package__.js" />
			<param name="dstFile" value="${release_dir}/source.__package__.js" />
			<param name="buildPrefix" value="dojo" />
		</antcall>

		<!--
			clobber requireLocalization statements.  They need to be come no-ops since the resources
			are now prebuilt and also because we do not want dojo.js to trigger XHRs on load
		-->
		<replaceregexp match="dojo.requireLocalization\((.*?)\);"
			byline="true" flags="g" file="${release_dir}/source.__package__.js">
			<substitution expression=""/>
		</replaceregexp>

		<antcall target="-rhino-compress">
			<param name="srcFile" value="${release_dir}/source.__package__.js" />
			<param name="dstFile" value="${release_dir}/dojo.js" />
		</antcall>

		<antcall target="-no-rhino-compress">
			<param name="srcFile" value="${release_dir}/source.__package__.js" />
			<param name="dstFile" value="${release_dir}/dojo.js" />
		</antcall>

		<!-- add build notice -->
		<replaceregexp match="^" byline="false" replace="${build_notice}">
			<fileset dir="${release_dir}" includes="dojo.js"/>
		</replaceregexp>

		<!-- add copyright notice -->
		<replaceregexp match="^" byline="false" replace="${copyright}">
			<fileset dir="${release_dir}">
				<include name="**/*.js" />
				<include name="**/*.as" />
				<not>
					<contains text="copyright" casesensitive="no"  />
				</not>
			</fileset>
		</replaceregexp>

		<delete file="${release_dir}/source.__package__.js"/>

		<!-- we copy over iframe_history.html here just in case this is a truly
		stand-alone build that isn't being kicked off via release -->
		<copy file="../iframe_history.html" todir="${release_dir}/" />

		<!-- copy our dojo.storage and dojo.flash SWFs -->
		<copy todir="${release_dir}/" preservelastmodified="true">
			<fileset dir="${root}/">
				<include name="**/*.swf" />
				<include name="*.jar" />
			</fileset>
		</copy>

	</target>
	<!-- end compress task -->

	<target name="-rhino-compress" unless="nostrip">
		<copy overwrite="true" file="${srcFile}" tofile="${dstFile}.uncompressed.js" />
		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true"
				logerror="true" output="${dstFile}">
			<arg value="-strict" />
			<arg value="-opt"/>
			<arg value="-1" />
			<arg value="-c" />
			<arg value="${srcFile}" />
		</java>
	</target>

	<target name="-no-rhino-compress"
		if="nostrip">
		<copy overwrite="true" file="${srcFile}" tofile="${dstFile}" />
	</target>

	<target name="-flatten-resources"
		description="loads all localization bundles in the build for a particular locale, flattens the object, and outputs a preloaded version">
		<!-- FIXME: should also remove dojo.requireLocalization() calls from the build? -->
		<copy overwrite="true" file="${srcFile}" tofile="${release_dir}/localizations.tmp.js">
			<filterchain>
				<tokenfilter>
					<containsregex pattern="dojo\.requireLocalization\(.*\);"/>
				</tokenfilter>
			</filterchain>
		</copy>

		<!-- Hmm. have to repeat this work here even though it was done in -set-profile-deps. I guess script block changes
		to properties only last within that target? -->
		<script language="javascript">
			<![CDATA[
			var pf = dojo.getProperty("profileFile");
			if((!pf)||(pf=="null")||(pf=="")){
				var prof = dojo.getProperty("profile");
				if((prof)&&(prof!="null")&&(prof!="")){
					dojo.setNewProperty("profileFile", "profiles/"+prof+".profile.js");
				}else{
					dojo.setNewProperty("profileFile", "getDependencyList.js");
				}
			}
		]]>
		</script>
		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true"
			logError="true" outputproperty="generatedLocales" >
		<!--java jar="./lib/custom_rhino.jar" failonerror="true" fork="true"
			logError="true" -->
			<arg value="flattenResources.js" />
			<arg value="${release_dir}" />
			<arg value="${profileFile}" />
			<arg value="${release_dir}/localizations.tmp.js" />
			<arg value="${release_dir}/nls" />
			<arg value="${buildPrefix}" />
			<arg value="${locales}" />
			<sysproperty key="DOJO_LOADER" value="${dojoLoader}" />
		</java>

		<delete file="${release_dir}/localizations.tmp.js" />
		<available property="hasResources" file="${release_dir}/nls" type="dir" />
		<echo message="Generated flattened resources for prefix ${buildPrefix} in the following locales: ${generatedLocales}."/>
	</target>

	<target name="-preload-resources" depends="-flatten-resources" if="hasResources"
		description="inserts data necessary for the bootstrap to preload the flattened resource files">
		<replaceregexp match="/\*\*\*BUILD:localesGenerated\*\*\*/" byline="false" replace="=${generatedLocales}"
			file="${dstFile}"/>
	</target>

	<!-- intern-strings task -->
	<target name="intern-strings" depends="-fix-config"
		description="pulls template paths specified in prototypes into templateStrings which can be used to speed up widget construction">

		<!-- Hmm. have to repeat this work here even though it was done in -set-profile-deps. I guess script block changes
		to properties only last within that target? -->
		<script language="javascript">
			<![CDATA[
			var pf = dojo.getProperty("profileFile");
			if((!pf)||(pf=="null")||(pf=="")){
				var prof = dojo.getProperty("profile");
				if((prof)&&(prof!="null")&&(prof!="")){
					dojo.setNewProperty("profileFile", "profiles/"+prof+".profile.js");
				}else{
					dojo.setNewProperty("profileFile", "getDependencyList.js");
				}
			}
		]]>
		</script>
		<script language="jython">
			<![CDATA[
import sys
# make the python standard library avialable
sys.path.append(project.resolveFile("lib/pyLib.zip").getPath())
sys.path.append(project.resolveFile(".").getPath())

# import re
import os
from buildUtil import *

print "Interning strings for directory: " + dojo.getProperty("release_dir")
internTemplateStrings(project.resolveFile(dojo.getProperty("profileFile")).getPath(), dojo.getProperty("dojoLoader"), project.resolveFile(dojo.getProperty("release_dir")).getPath())
]]>
		</script>
	</target>
	<!-- end intern-strings task -->

	<!-- release task -->
	<target name="release" depends="-fix-config"
		description="builds a release">

		<echo message="Using version number: ${version} for the release."/>

		<antcall target="clean" />
		<!--
		<antcall target="src" />
		-->
		<copy todir="${release_dir}/${src}" includeEmptyDirs="false" preservelastmodified="true">
			<fileset dir="${root}/${src}">
				<include name="**" />
				<exclude name="**/*.swp" />
				<exclude name="**/.svn*" />
			</fileset>
		</copy>

		<copy todir="${release_dir}/demos" includeEmptyDirs="false" preservelastmodified="true">
			<fileset dir="${root}/demos">
				<include name="**" />
				<exclude name="**/*.swp" />
				<exclude name="**/.svn*" />
			</fileset>
		</copy>

		<copy todir="${release_dir}/tests" includeEmptyDirs="false" preservelastmodified="true">
			<fileset dir="${root}/tests">
				<include name="**" />
				<exclude name="**/*.swp" />
				<exclude name="**/.svn*" />
			</fileset>
		</copy>

		<!--
			Call our Dojo Flash and Storage tasks
			in such a way that they won't cause the
			entire release task to fail if mtasc is not
			available - make mtasc optional.
		-->
		<script language="javascript">
			<![CDATA[
			function echo(msg){
				var echo = dojo.createTask("echo");
				echo.setMessage(msg);
				echo.execute();
			}

			function antCall(target){
				var antCall = dojo.createTask("antcall");
				antCall.setTarget(target);
				antCall.execute();
			}

			try{
				antCall("buildDojoFlash");
				antCall("buildDojoStorage");
			}catch(exp){
				echo("WARNING: buildDojoFlash and buildDojoStorage could not run.");
				echo("Please download and install mtasc from http://www.mtasc.org and place it in your path.");
			}
		]]>
		</script>
		
		<!-- 
			Generate optional Java class needed for Safari and Opera
			for dojo.storage.browser.FileStorageProvider
		-->
		<antcall target="buildFileStorageProvider" />

		<antcall target="-docs-and-tests" />
		<antcall target="compress" />
		<!-- <antcall target="intern-strings" /> -->

		<!-- Run xdgen if it xdomain loading is wanted. -->
		<condition property="xdgenRun">
			<equals arg1="xdomain" arg2="${dojoLoader}"/>
		</condition>
		<antcall target="xdgenRelease"/>

		<copy overwrite="true" file="../iframe_history.html" todir="${release_dir}/"  preservelastmodified="true" />

		<copy file="${root}/LICENSE" todir="${release_dir}/"  preservelastmodified="true" />
		<copy file="${root}/README" todir="${release_dir}/"  preservelastmodified="true" />
	</target>
	<!-- end release task -->

	<target name="-docs-and-tests"
		unless="docless"
		description="builds docs, copies in tests, and constructs API outlines">
		<copy todir="${release_dir}/apis" includeEmptyDirs="false" preservelastmodified="true">
			<fileset dir="${root}/documents/apis">
				<include name="**" />
				<exclude name="**/*.swp" />
				<exclude name="**/.svn*" />
			</fileset>
		</copy>

		<antcall target="docs" />
	</target>

	<!-- zip task -->
	<target name="zip"
		description="builds zip file of release files">
		<zip destfile="${root}/release/${project}_${version}.zip" basedir="${root}/release/${project}"/>
	</target>
	<!-- end zip task -->

	<!-- tar task -->
	<target name="tar"
		description="builds tarball of release files">
		<tar tarfile="${root}/release/${project}_${version}.tar" basedir="${root}/release" includes="${project}/**"/>
		<gzip zipfile="${root}/release/${project}_${version}.tar.gz" src="${root}/release/${project}_${version}.tar"/>
		<delete file="${root}/release/${project}_${version}.tar"/>
	</target>
	<!-- end tar task -->
	
	<!-- buildFileStorageProvider task -->
	<target name="buildFileStorageProvider"
			description="Builds a Java class needed by dojo.storage.browser.FileStorageProvider">
		<echo>
Building optional Java class for dojo.storage.browser.FileStorageProvider. Don't
worry if this fails; the generated class file is checked into Subversion and is
available in src/storage/java/ without rebuilding it.
		</echo>		
		
		<javac	srcdir="${root}/src/storage/java/"
				destdir="${root}/src/storage/java/"
				debug="on"
				target="1.1"
				source="1.1"
				failonerror="false"/>
				
		<jar destfile="${root}/DojoFileStorageProvider.jar">
		    <fileset dir="${root}/src/storage/java/"
		             includes="*.class"/>
		 </jar>
	</target>
	<!-- end buildFileStorageProvider task -->

	<!-- buildDojoFlash task -->
	<target name="buildDojoFlash"
					description="builds the Dojo Flash package and its testing files">
		<antcall target="buildFlash">
			<param name="dojo.flash.file" value="${root}/tests/flash/HelloWorld.as"/>
		</antcall>

		<antcall target="buildFlash">
			<param name="dojo.flash.file" value="${root}/tests/flash/UnitTestsComm.as"/>
		</antcall>

		<move todir="${root}/tests/flash/resources">
			<fileset dir="${root}/tests/flash/">
				<include name="*.swf"/>
			</fileset>
		</move>
	</target>
	<!-- end buildDojoFlash task -->

	<!-- buildDojoStorage task -->
	<target name="buildDojoStorage"
					description="builds the Dojo Storage package">
		<antcall target="buildFlash">
			<param name="dojo.flash.file" value="${root}/${src}/storage/Storage.as"/>
		</antcall>

		<move todir="${root}">
			<fileset dir="${root}/${src}/storage/">
				<include name="*.swf"/>
			</fileset>
		</move>
	</target>
	<!-- end buildDojoStorage task -->

	<!-- buildFlash task -->
	<target name="buildFlash"
					description="builds Flash ActionScript class to use dojo.flash.comm">
		<!-- extract the ActionScript class name and path -->
		<script language="javascript">
			<![CDATA[
			var flashFile = dojo.getProperty("dojo.flash.file");

			var matches = flashFile.match(/([A-Za-z0-9_]*)\.as$/);
			var flashClass = matches[1];
			dojo.setNewProperty("flashClass", flashClass);

			var flashFileName = matches[0];
			var endCut = new String(flashFile).length - flashFileName.length;
			var flashPath = flashFile.substring(0, endCut);
			dojo.setNewProperty("flashPath", flashPath);
		]]>
		</script>

		<!-- generate the Flash 6 SWF -->
		<echo message="Flash 6: Compiling ${dojo.flash.file}..."/>

		<exec executable="mtasc">
			<arg value="-version"/>
			<arg value="6"/>

			<arg value="-cp"/>
			<arg value="${root}/${src}/flash/flash6"/>

			<arg value="-swf"/>
			<arg value="${flashPath}/${flashClass}_version6.swf"/>

			<arg value="-main"/>

			<arg value="-header"/>
			<arg value="215:138:10"/>

			<arg value="${dojo.flash.file}"/>
		</exec>

		<!-- generate the Flash 8 SWF -->
		<echo message="Flash 8: Compiling ${dojo.flash.file}..."/>

		<exec executable="mtasc">
			<arg value="-version"/>
			<arg value="8"/>

			<arg value="-cp"/>
			<arg value="${root}/${src}/flash/flash8"/>

			<arg value="-swf"/>
			<arg value="${flashPath}/${flashClass}_version8.swf"/>

			<arg value="-main"/>

			<arg value="-header"/>
			<arg value="215:138:10"/>

			<arg value="${dojo.flash.file}"/>
		</exec>
	</target>
	<!-- end buildFlash task -->

	<!-- xdgenRelease task -->
	<target name="xdgenRelease" if="xdgenRun" depends="xdgenRemove">
		<!-- Hmm. have to repeat this work here even though it was done in -set-profile-deps. I guess script block changes
		to properties only last within that target? -->
		<script language="javascript">
			<![CDATA[
			var pf = dojo.getProperty("profileFile");
			if((!pf)||(pf=="null")||(pf=="")){
				var prof = dojo.getProperty("profile");
				if((prof)&&(prof!="null")&&(prof!="")){
					dojo.setNewProperty("profileFile", "profiles/"+prof+".profile.js");
				}else{
					dojo.setNewProperty("profileFile", "getDependencyList.js");
				}
			}
		]]>
		</script>

		<echo message="Generating xd resource files..."/>
		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true" logError="true">
			<arg value="xdgen.js" />
			<arg value="xdgen" />
			<arg value="${profileFile}" />
			<arg value="${release_dir}" />
			<arg value="${release_dir}/src" />
			<arg value="${release_dir}/nls" />
		</java>
		<echo message="Finished generating xd resource files."/>
	</target>
	<!-- end xdgenRelease task -->

	<!-- xdgenRemove task -->
	<target name="xdgenRemove"
			description="Removes package files that were created by xdgen.">
		<!-- Hmm. have to repeat this work here even though it was done in -set-profile-deps. I guess script block changes
		to properties only last within that target? -->
		<script language="javascript">
			<![CDATA[
			var pf = dojo.getProperty("profileFile");
			if((!pf)||(pf=="null")||(pf=="")){
				var prof = dojo.getProperty("profile");
				if((prof)&&(prof!="null")&&(prof!="")){
					dojo.setNewProperty("profileFile", "profiles/"+prof+".profile.js");
				}else{
					dojo.setNewProperty("profileFile", "getDependencyList.js");
				}
			}
		]]>
		</script>
		<!-- Remove .xd.js files -->
		<echo message="Removing xd resource files..."/>
		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true" logError="true">
			<arg value="xdgen.js" />
			<arg value="xdremove" />
			<arg value="${profileFile}" />
			<arg value="${release_dir}" />
			<arg value="${release_dir}/src" />
			<arg value="${release_dir}/nls" />
		</java>
		<echo message="Finished removing xd resource files."/>
	</target>
	<!-- end xdgenRemove task -->
	<target name="dummy_javac" description="Make eclipse happy to create a project from this build.xml">
		<javac srcdir="./" failonerror="true" destdir="./" debug="true">
		</javac>
	</target>
</project>
