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

<!--
	Copyright (c) 2004-2011 The Dojo Foundation, Licensed under the Academic
	Free License version 2.1 or BSD licenses
-->
<project name="dojo" default="cldr" basedir=".">
	<description>Build the Dojo toolkit</description>

	<property name="project" value="dojo"/>
	<property name="root" location="../../.."/>
	<property name="dojoLoader" value="default"/>
	<property name="ldml" location="ldml"/>
	<property name="logDir" location="."/>
	<property name="json" location="${root}/dojo/cldr/nls"/>

	<!-- Arbitrary defaults. locales and currencies properties can be altered or eliminated to build the entire set -->
	<property name="locales" value="ar,ca,cs,da,de-de,el,en-au,en-ca,en-gb,en-us,es-es,fi,fr-ch,fr-fr,he-il,hu,it-it,ja-jp,ko-kr,nb,nl,pl,pt-br,pt-pt,ro,ru,sk,sl,sv,th,tr,zh,zh-cn,zh-hant,zh-hans,zh-hk,zh-tw" />
	<property name="currencies" value="GBP,USD,CAD,AUD,EUR,CHF,HKD,JPY,CNY" />

	<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="js_ok" file="${user.home}/.ant/lib/js.jar" />
		<available property="saxon9_ok" file="${user.home}/.ant/lib/saxon9.jar" />
		<available property="resolver_ok" file="${user.home}/.ant/lib/resolver.jar" />
		<available property="logger_ok" file="${user.home}/.ant/lib/commons-logging-1.1.1.jar" />
		
		<condition property="config_ok">
			<and>
				<isset property="bsf_ok" />
				<isset property="ant_bsf_ok" />
				<isset property="js_ok" />
				<isset property="saxon9_ok" />
				<isset property="resolver_ok" />
				<isset property="logger_ok" />
			</and>
		</condition>
	</target>

	<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="ant-apache-bsf.jar"/>
				<include name="saxon9.jar"/>
				<include name="resolver.jar"/>
				<include name="commons-logging-1.1.1.jar"/>
			</fileset>
		</copy>
		<copy todir="${user.home}/.ant/lib"
			preservelastmodified="true">
			<fileset dir="../../shrinksafe">
				<include name="js.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>

	<target name="ldml" depends="-ldml-check" unless="ldmlUnpacked"
		description="Unpack LDML (CLDR repository XML markup)">
		<mkdir dir="${ldml}/core" />
		<unzip src="${ldml}/core.zip" dest="${ldml}/core" />
	</target>
	<target name="-ldml-check">
		<available property="ldmlUnpacked" file="${ldml}/core" type="dir" />
	</target>

	<target name="clean">
		<delete dir="${ldml}/core" />
		<delete dir="${json}" />
		<mkdir dir="${json}" />
	</target>

	<target name="cldr" depends="-fix-config, ldml"
		description="Transforms LDML from the CLDR into JSON">

		<scriptdef name="removeEmptyOutput" language="javascript">
			<attribute name="dir"/>
			<![CDATA[
		var isEmpty = function(o){
			for(x in o){
				return false;
			}
			return true;
		}

		var delTask = project.createTask("delete");
		var deleteResource = function(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

		var value;

		try{
			value = eval(contents);
		}catch(e){
			java.lang.System.out.print("Syntax error in "+srcFile.getCanonicalPath());
java.lang.System.out.print(contents);
			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="basedir"/>
			<attribute name="destdir"/>

			<sequential>
				<mkdir dir="@{destdir}"/>
				<transform in="@{in}" style="calendar.xsl" basedir="@{basedir}" destdir="@{destdir}"/>
				<transform in="@{in}" style="number.xsl" basedir="@{basedir}" destdir="@{destdir}"/>
				<transform in="@{in}" style="currency.xsl" basedir="@{basedir}" destdir="@{destdir}"/>
				<echo level="info" message="Transformed @{in} to @{destDir}" taskName="cldr"/>
				<removeEmptyOutput dir="@{destdir}" />
			</sequential>
		</macrodef>
		<macrodef name="transform">
			<attribute name="in"/>
			<attribute name="style"/>
			<attribute name="basedir"/>
			<attribute name="destdir"/>
			<sequential>
				<xslt includes="@{in}" style="@{style}" destdir="@{destdir}" basedir="@{basedir}" extension=".tmp">
					<factory name="net.sf.saxon.TransformerFactoryImpl" />
					<xmlcatalog>
						<catalogpath path="ldml/catalog" />
					</xmlcatalog>
					<param name="basedir" expression="@{basedir}/" />
					<param name="currencyList" expression="${currencies}" />
				</xslt>
				<delete>
					<fileset dir="@{destdir}" includes="*.tmp" />
				</delete>
			</sequential>
		</macrodef>

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

	var filter = project.getProperty("locales");
	if(filter){
		filter = String(filter).replace(/-/g,'_').split(",");
		for(var i in filter){
			filter[i] = filter[i].replace(/\s/g,'');
			var variants = filter[i].split("_");
			for(var j = 1; j <= variants.length - 1; j++){
				var partial = variants.slice(0, j).join("_");
				for(var k = 0; k < filter.length; k++){
					if(filter[k]==partial){
						partial=null; continue;
					}
				}
				if(partial){filter.push(partial)};
			}
		}
		filter.push("root");
		var fs = project.createDataType("fileset");
		fs.setDir(basedir);
		fs.setCaseSensitive(false);
		var orSelector = new Packages.org.apache.tools.ant.types.selectors.OrSelector();
		for(i in filter){
			var locale=filter[i];
			var selector = new Packages.org.apache.tools.ant.types.selectors.FilenameSelector();
			selector.setName(locale + ".xml");
			selector.setCasesensitive(false);
			orSelector.addFilename(selector);
		}
		fs.addOr(orSelector);
	}
		
	ds = fs.getDirectoryScanner(project);
	srcFiles = ds.getIncludedFiles();
	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(filename));
		ldml.setDynamicAttribute("destdir", outdir);
		ldml.setDynamicAttribute("basedir", basedir);
		ldml.perform();
	}
			]]>
		</scriptdef>

		<transformCldr destdir="${json}">
			<fileset dir="${ldml}/core/common/main" includes="**/*.xml" />
		</transformCldr>
		<java jar="../../shrinksafe/js.jar" failonerror="true" fork="true" logError="true" >
			<arg value="arrayInherit.js" />
			<arg value="${json}" />
			<arg value="${logDir}"/>
			<sysproperty key="DOJO_LOADER" value="${dojoLoader}" />
		</java>
		<java jar="../../shrinksafe/js.jar" failonerror="true" fork="true" logError="true" >
			<arg value="specialLocale.js" />
			<arg value="${json}" />
			<arg value="${locales}" />
			<arg value="${logDir}"/>
			<sysproperty key="DOJO_LOADER" value="${dojoLoader}" />
		</java>
		<java jar="../../shrinksafe/js.jar" failonerror="true" fork="true" logError="true" >
			<arg value="alias.js" />
			<arg value="${json}" />
			<arg value="${logDir}"/>
			<sysproperty key="DOJO_LOADER" value="${dojoLoader}" />
		</java>
		<java jar="../../shrinksafe/js.jar" failonerror="true" fork="true" logError="true" >
			<arg value="wrap.js" />
			<arg value="${json}" />
			<arg value="${logDir}"/>
			<sysproperty key="DOJO_LOADER" value="${dojoLoader}" />
		</java>
	</target>
</project>
