<?xml version="1.0" encoding="UTF-8"?>
<project name="Dojo Installer" default="compile">

  <property file="build-dojo.properties"/>
  <property name="localRepository" value="${user.home}/.m2/repository"/>
  <property name="dojo.version" value="1.0.2"/>
  <property name="context.path" value="unitedfront2"/>
  <property name="dojo.path" value="/js/dojo"/>
  <property name="build.location" value="${basedir}/target/${context.path}/${dojo.path}"/>
  <property name="deploy.location" value="${basedir}/../.metadata/.plugins/org.eclipse.wst.server.core/${eclipse.wst.server.dir}/${eclipse.wst.webapps.dir}/${context.path}/${dojo.path}"/>

  <target name="install" depends="check-installed" unless="already.installed">
    <mkdir dir="${basedir}/target/download/dojo/"/>
    <get src="http://download.dojotoolkit.org/release-${dojo.version}/dojo-release-${dojo.version}.zip"
        dest="${basedir}/target/download/dojo/dojo-release-${dojo.version}.zip" verbose="true" usetimestamp="true"/>
    <exec executable="mvn${maven.suffix}" dir="${basedir}" failonerror="false">
      <arg line="install:install-file -DgroupId=dojo -DartifactId=dojo -Dversion=${dojo.version} -Dpackaging=zip -DgeneratePom=true -Dfile=${basedir}/target/download/dojo/dojo-release-${dojo.version}.zip"/>
    </exec>
  </target>

  <target name="compile" depends="check-installed">
    <fail message="dojo zip file not installed in local repository: ${localRepository}" unless="already.installed"/>
    <mkdir dir="${localRepository}/dojo/dojo/${dojo.version}"/>
    <unzip src="${localRepository}/dojo/dojo/${dojo.version}/dojo-${dojo.version}.zip"
         dest="${localRepository}/dojo/dojo/${dojo.version}/" overwrite="false"/>
    <copy todir="${build.location}">
      <fileset dir="${localRepository}/dojo/dojo/${dojo.version}/dojo-release-${dojo.version}">
        <exclude name="**/demos/**"/>
        <exclude name="**/tests/**"/>
      </fileset>
    </copy>

    <!-- Compile the patch files to build dir -->
    <copy todir="${build.location}" overwrite="true">
      <fileset dir="${basedir}/src/main/javascript/dojo"/>
    </copy>
  </target>

  <target name="check-installed">
    <condition property="already.installed" >
      <available file="${localRepository}/dojo/dojo/${dojo.version}/dojo-${dojo.version}.zip"/>
    </condition>
    <condition property="maven.suffix" value="">
      <os family="unix"/>
    </condition>
    <condition property="maven.suffix" value=".bat">
      <os family="windows"/>
    </condition>
  </target>

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

  <target name="deploy" description="Deploy dojo to Eclipse Server">
    <antcall target="compile"/>
    <copy todir="${deploy.location}">
      <fileset dir="${build.location}"/>
    </copy>
  </target>

  <target name="undeploy" description="Undeploy dojo from Eclipse Server">
    <delete dir="${deploy.location}"/>
  </target>

</project>
