<?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/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 -->
	<!-- FIXME: use location instead of value where appropriate? -->
	<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" location="${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="strip_and_compress" value="false"/>
	<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? -->
	<property name="currencies" value="GBP,USD,CAD,AUD,EUR,CHF,HKD,JPY"/> 	<!-- FIXME: arbitrary list. Can be altered or eliminated for all currencies -->
	<property name="cldrIn" location="${root}/cldr"/>
	<property name="cldrOut" location="${root}/${src}/i18n/cldr"/>

	<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}

	api - builds release (optimized) api reference application

	cldr - transforms the CLDR localization repository into JSON in the source tree

	test - builds and runs command line unit tests

	testScripting - tests the JS 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 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 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>
	</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" />

		<condition property="config_ok">
			<and>
				<isset property="bsf_ok" />
				<isset property="ant_bsf_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="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 -->

	<!-- api task -->
	<target name="api"
		description="Builds a release (optimized) api reference application">
		<antcall>
			<param name="profileFile" location="${root}/api/api.profile.js" />
			<target name="clean" />
			<target name="release" />
			<target name="intern-strings" />
		</antcall>
		<copy todir="${release_dir}/api" includeEmptyDirs="false" preservelastmodified="true">
			<fileset dir="${root}/api" />
		</copy>
		<copy todir="${release_dir}/docscripts/output/local/json" includeEmptyDirs="false" preservelastmodified="true">
			<fileset dir="${root}/docscripts/output/local/json" />
		</copy>
	</target>
	<!-- end api task -->

	<!-- docparser task -->
	<target name="docparser"
		description="Runs the doc parser against the source tree to generate JSON">
		<exec dir="${root}/docscripts" executable="php5" failonerror="true">
			<arg value="-d" />
			<arg value="memory_limit=100M" />
			<arg value="-d" />
			<arg value="error_reporting=2039" />
			<arg file="docparser.php" />
		</exec>
	</target>
	<!-- end docparser task -->

	<!-- ldml task -->
	<target name="ldml" depends="-ldml-check" unless="ldmlUnpacked"
		description="Unpack CLDR repository">
		<mkdir dir="${cldrIn}/ldml" />
		<unzip src="${cldrIn}/core.zip" dest="${cldrIn}/ldml" />
	</target>
	<target name="-ldml-check">
			<available property="ldmlUnpacked" file="${cldrIn}/ldml" type="dir" />
	</target>
	<!-- end ldml task -->

	<!-- cldr task -->
	<target name="cldr" depends="-fix-config, ldml"
		description="Transforms CLDR (localization data) into JSON">

		<scriptdef name="removeEmptyOutput" language="javascript">
			<attribute name="dir"/>
			<![CDATA[
		function isEmpty(o){
			for(x in o){
				return false;
			}
			return true;
		}
		
		var delTask = project.createTask("delete");
		function deleteResource(f){
			f.isDirectory() ? delTask.setDir(f) : delTask.setFile(f);
			delTask.perform();
		}

	var fs = project.createDataType("fileset");
	var dir = new java.io.File(attributes.get("dir"));
	fs.setDir(dir);
	fs.setIncludes("*.js");
	ds = fs.getDirectoryScanner(project);
	jsFiles = ds.getIncludedFiles();

	var load = project.createTask("loadfile");
	load.setEncoding("UTF8");
	for (var i=0; i<jsFiles.length; i++) {
		var srcFile = java.io.File(dir, jsFiles[i]);
		load.setSrcFile(srcFile);
		//FIXME: spawn task as separate ant process to avoid stale properties, collisions?
		var uniqueId = "removeEmptyOutput.json."+dir+"."+i;
		load.setProperty(uniqueId);
		load.perform();
	
		var contents = String(project.getProperty(uniqueId));
		if(contents.charCodeAt(0) == 0xfeff){ contents = contents.substring(1); } // JDK bug workaround

		try{
			var value = eval(contents);
		}catch(e){
			java.lang.System.out.print("Syntax error in "+srcFile.getCanonicalPath());
			throw e;
		}
		if(isEmpty(value)){
			deleteResource(srcFile);
		}
	}

	// delete the directory if empty
	ds.scan();
	if(!ds.getIncludedFilesCount()){
		deleteResource(dir);
	}
			]]>
		</scriptdef>

		<macrodef name="processLdml">
			<attribute name="in"/>
			<attribute name="destdir"/>

			<sequential>
				<mkdir dir="@{destdir}"/>
				<saxon in="@{in}" style="${cldrIn}/ldml/main/cldrCalendar.xsl" destdir="@{destdir}"/>
				<saxon in="@{in}" style="${cldrIn}/ldml/main/cldrNumber.xsl" destdir="@{destdir}"/>
				<saxon in="@{in}" style="${cldrIn}/ldml/main/cldrCurrency.xsl" destdir="@{destdir}"/>
				<echo level="info" message="Transformed @{in} to @{destDir}" taskName="cldr"/>
				<removeEmptyOutput dir="@{destdir}" />
			</sequential>
		</macrodef>
		<macrodef name="saxon">
			<!-- XSLT ant tasks run for a while, then hang. Perhaps there's a file handle leak?
				 Kludge: Run as separate JVM processes instead to be safe. -->
			<attribute name="in"/>
			<attribute name="style"/>
			<attribute name="destdir"/>

			<sequential>
				<java fork="true" dir="@{destdir}" jar="lib/saxon8.jar">
					<!-- Reference the DTD locally to speed things up -->
					<!-- resolver and CatalogManager.properties have to be on the bootclasspath, apparently
						 in order for JAXP to find them -->
					<jvmarg value="-Xbootclasspath/a:${cldrIn}/../buildscripts/lib/resolver.jar"/>
					<jvmarg value="-Xbootclasspath/a:${cldrIn}"/>
					<arg value="-r"/>
					<arg value="org.apache.xml.resolver.tools.CatalogResolver"/>
					<arg value="-x"/>
					<arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
					<arg value="-y"/>
					<arg value="org.apache.xml.resolver.tools.ResolvingXMLReader"/>
					<arg file="@{in}"/>
					<arg file="@{style}"/>
					<arg value="currencyList=${currencies}"/>
				</java>
			</sequential>
		</macrodef>

		<scriptdef name="transformCldr" language="javascript">
			<attribute name="destdir"/>
			<element name="fileset" type="fileset"/>
			<![CDATA[
	var fs = elements.get("fileset").get(0);
	ds = fs.getDirectoryScanner(project);
	srcFiles = ds.getIncludedFiles();
	var basedir = fs.getDir(project);
	var destDir = attributes.get("destdir");

	for (i=0; i<srcFiles.length; i++) {
		var filename = srcFiles[i];
		var locale = filename.substring(0, filename.indexOf(".")).replace("_","-").toLowerCase();
		var outDir = new java.io.File(locale == "root" ? destDir : destDir+"/"+locale);

		var ldml = project.createTask("processLdml");
		ldml.setDynamicAttribute("in", new java.io.File(basedir+"/"+filename));
		ldml.setDynamicAttribute("destdir", outDir);
		ldml.perform();
	}
			]]>
		</scriptdef>

		<!-- kludge to fix the "Fail to load **.xml" exception which occurs in IBM JDK 1.4.2
			where saxon uses wrong base with relative URIs -->
		<copy todir="${cldrIn}/ldml/main">
		   <fileset dir=".">
		     <include name="**/cldr*.xsl"/>
		   </fileset>
		</copy>	

		<delete dir="${cldrOut}/nls" />
		<transformCldr destdir="${cldrOut}/nls">
			<fileset dir="${cldrIn}/ldml/main" includes="**/*.xml" />
		</transformCldr>
		<java jar="lib/js.jar" failonerror="true" fork="true" logError="true" >
			<arg value="cldrArrayInherit.js" />
			<arg value="${cldrOut}/nls" />
			<sysproperty key="DOJO_LOADER" value="${dojoLoader}" />
		</java>
	</target>
	<!-- end cldr 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 match="^\/\/.*$" byline="true" replace="" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js" />
		</replaceregexp>
		<replaceregexp byline="false" flags="g">
			<regexp pattern="([^:(&quot;.'\\-][^:C0-9&quot;(\\])(\/\/.*)" />
			<substitution expression="\1" />
			<fileset dir="${release_dir}" includes="**/*.js" />
		</replaceregexp>
		<replaceregexp match="\s\/\/.*$" byline="true" replace="" flags="g">
			<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>
		<replaceregexp match="\n\n" replace="" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>
		
		<replaceregexp match="\t" replace="" byline="false" flags="g">
			<fileset dir="${release_dir}" includes="**/*.js"/>
		</replaceregexp>
	</target>
	<!-- end gen-strip-docs task -->

	<!-- strip-resource-comments -->
		<target name="strip-resource-comments">
		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true" 
				logerror="true">
			<arg value="stripComments.js" />
			<arg value="${release_dir}/src"/>
			<arg value="false"/>
			<arg value="${strip_and_compress}"/>
		</java>
	</target>
	<!-- end strip-resource-comments -->

	<!-- 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-profileFile task -->
	<target name="-set-profileFile" depends="-fix-config"
		description="Configures profileFile property.">
		<!-- Set up the profileFile property. -->
		<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>
	</target>
	<!-- end -set-profileFile task -->

	<!-- list-profile-deps task -->
	<target name="list-profile-deps" depends="-fix-config, -set-profileFile"
		description="Lists the files that will be included in dojo.js for a given profile">
		<!-- Call the JS script that will generate the file list. -->
		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true"
			logError="true">
			<arg value="listProfileDeps.js" />
			<arg value="${profileFile}" />
			<arg value="${version}" />
			<sysproperty key="DOJO_LOADER" value="${dojoLoader}" />
		</java>
	</target>
	<!-- end list-profile-deps task -->

	<!-- compress task -->
	<target name="compress" depends="-fix-config, -set-profileFile"
		description="runs compression code over release JS files">
		<!-- Create the release dir if it doesn't exist. -->
		<mkdir dir="${release_dir}"/>

		<echo level="info" taskName="compress">profileFile: ${profileFile}</echo>

		<!-- Call the JS script that will generate the raw dojo.js file -->
		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true"
			logError="true">
			<arg value="makeDojoJs.js" />
			<arg value="${profileFile}" />
			<arg value="${release_dir}" />
			<arg value="source.__package__.js" />
			<arg value="${version}" />
			<sysproperty key="DOJO_LOADER" value="${dojoLoader}" />
		</java>

		<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" depends="-set-profileFile"
		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>

		<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 level="info" message="Generated flattened resources for prefix ${buildPrefix} in the following locales: ${generatedLocales}" taskName="-flatten-resources"/>
	</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, -set-profileFile"
		description="pulls template paths specified in prototypes into templateStrings which can be used to speed up widget construction">

		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true"
			logError="true">
			<arg value="internStrings.js" />
			<arg value="${profileFile}" />
			<arg value="${dojoLoader}" />
			<arg value="${release_dir}" />
		</java>
	</target>
	<!-- end intern-strings task -->

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

		<echo level="info" taskName="release" 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" taskName="release">
			<![CDATA[
			function echo(msg){
				var echo = dojo.createTask("echo");
				echo.setMessage(msg);
				echo.setTaskName("release");
				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="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="api-prep"
		description="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>
	</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 level="info" taskName="buildFileStorageProvider">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 level="info" taskName="buildFlash" 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 -->

	<!-- xd-dojo-config task -->
	<target name="xd-dojo-config" depends="-fix-config"
		description="Sets dojo.js to use xdomain loading and sets dojo module path to a specific URL.">
		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true"
			logError="true">
			<arg value="setXdDojoConfig.js" />
			<arg value="${release_dir}/dojo.js" />
			<arg value="${xdDojoUrl}" />
		</java>
		<java jar="./lib/custom_rhino.jar" failonerror="true" fork="true"
			logError="true">
			<arg value="setXdDojoConfig.js" />
			<arg value="${release_dir}/dojo.js.uncompressed.js" />
			<arg value="${xdDojoUrl}" />
		</java>
	</target>
	<!-- end xd-dojo-config task -->

	<!-- xdgenRelease task -->
	<target name="xdgenRelease" if="xdgenRun" depends="xdgenRemove, -set-profileFile">
		<echo level="info" message="Generating xd resource files..." taskName="xdgenRelease"/>
		<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 level="info" message="Finished generating xd resource files." taskName="xdgenRelease"/>
	</target>
	<!-- end xdgenRelease task -->

	<!-- xdgenRemove task -->
	<target name="xdgenRemove" depends="-set-profileFile"
			description="Removes package files that were created by xdgen.">
		<!-- Remove .xd.js files -->
		<echo level="info" message="Removing xd resource files..." taskName="xdgenRemove"/>
		<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 level="info" message="Finished removing xd resource files." taskName="xdgenRemove"/>
	</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>
