<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="default" name="testbench">

  <target name="default" depends="clean,  update-dependencies, compile,  jar, zip, publish"/>
  <target name="dependency" depends="default"/>
   
    <property name="appname" value="${ant.project.name}"/>
    <property name="org" value="OpenEdit, Inc."/>
    <property name="src" location="${basedir}/src"/>
	<property name="testsrc" location="${basedir}/test"/>
    <property name="lib" location="${basedir}/lib"/>
    <property name="etc" location="${basedir}/etc"/>
    <property name="build" location="${basedir}/build"/>
	<property name="compiled" location="${basedir}/build/compiled"/>
	
    <property name="wardir" location="${build}/wartmp"/>
    <property name="webapp" location="${basedir}/webapp"/>
	<property name="webinf" location="${webapp}/WEB-INF"/>
	<property name="branch" value="em9" />
				<property name="majorversion" value="9" />

	<!-- 
		========================================================================
		Anthill build server properties.  These properties will be passed in
		by anthill when this script is executed on the build server.
		
			- "version" is set by the version adapter
			- "deployDir" is set by anthill.publish.dir
		========================================================================
	--> 
	<property environment="env" />
	
	<property name="version"  value="${majorversion}.${env.BUILD_NUMBER}"/>
  	<property name="deployDir" location="deploy"/>
  	
  	<!-- A convenience variable for documentation output -->
  	<property name="versionDir" location="${deployDir}/${version}"/>
  	<property name="versionedApp" value="${appname}-${version}"/>

  <target name="clean">
     <delete dir="${build}"/>
  </target>

	
	
	<target name="update-dependencies" depends="clean">
	    <mkdir dir="${build}"/>
	    <mkdir dir="${wardir}/WEB-INF/" />
		<get dest="${build}/install.xml" src="http://dev.entermediasoftware.com/jenkins/job/${branch}_entermedia-server/lastSuccessfulBuild/artifact/deploy/install.xml"/>
			<ant antfile="${build}/install.xml" inheritAll="false" dir="${wardir}/WEB-INF/" target="default"/> 

				 <get dest="${wardir}/WEB-INF/install.xml" src="http://dev.entermediasoftware.com/jenkins/job/${branch}_app-mediamoneymaker/lastSuccessfulBuild/artifact/deploy/install.xml"/>
								 <ant antfile="${wardir}/WEB-INF/install.xml" inheritAll="false" dir="${wardir}/WEB-INF/" target="default"/> 
					 	
					
		
		
	</target>
  
	<target name="compile" depends="clean">
	    <mkdir dir="${build}/src"/>
	    
	    <javac debug="true" destdir="${build}/src" srcdir="${src}" source="1.8" target="1.8">
	      <classpath>
	        <fileset dir="${lib}">
	          <include name="*.jar"/>
	          <include name="compile/*.jar"/>
	        </fileset>
	        <fileset dir="${wardir}/WEB-INF/lib/">
	            <include name="*.jar"/>
	          </fileset>
	      </classpath>
	      <include name="**"/>
	    </javac>

	    <mkdir dir="${build}/src"/>
	 	<copy todir="${build}/src">
			<fileset dir="${src}">
			  <include name="*.xml"/>
			</fileset>
		</copy>

	  </target>
  
  	<!-- 
		========================================================================
		Jar classes for the main app, excluding unit tests.
		========================================================================
	-->	  
	<target name="jar" depends="compile">
		<mkdir dir="${deployDir}/builds/"/>
		<jar jarfile="${deployDir}/builds/${appname}.jar" basedir="${src}" >
			<exclude name="**/*Test*"/>
			<manifest>
		    	<attribute name="Built-By" value="${user.name}"/>
			    <attribute name="Implementation-Title" value="${appname}"/>
			    <attribute name="Implementation-Version" value="${version}"/>
			    <attribute name="Implementation-Vendor" value="${org}"/>
		    </manifest>
		</jar>
  </target>
    
    
	
	<target name="zip" depends="compile">
		<mkdir dir="${deployDir}/builds/"/>
		<zip destfile="${deployDir}/builds/${appname}.zip" >			
			<zipfileset dir="${lib}" prefix="lib" excludes="compile/**"/>
			
			<zipfileset dir="${deployDir}/builds/" includes="${appname}.jar" prefix="lib"/>
			<zipfileset dir="${webapp}/WEB-INF/base/bootstrapfrontend" prefix="base/bootstrapfrontend" />
			<zipfileset dir="${etc}/" prefix="etc" includes="install.js" />

		</zip>
  </target>
	    
	
	
  <target name="junit">
  	
    <javac debug="true" destdir="${compiled}" srcdir="${testsrc}" source="1.7" target="1.7">
      <classpath>
        <fileset dir="${lib}">
          <include name="*.jar"/>
          <include name="compile/*.jar"/>
        </fileset>
        <fileset dir="${wardir}/WEB-INF/lib/">
            <include name="*.jar"/>
         </fileset>
      
      </classpath>
      <include name="**"/>
    </javac>

  	<mkdir dir="${deployDir}/junit"/>	
  	<junit haltonfailure="true" showoutput="true" printsummary="true" fork="true" maxmemory="256m">
  		<sysproperty key="oe.root.path" value="${basedir}/resources/test"/>
  	  <classpath>
       <fileset dir="${lib}">
        <include name="**/*.jar"/>
      </fileset>
      <dirset dir="${compiled}">
        <include name="**"/>
      </dirset>
        <fileset dir="${wardir}/WEB-INF/lib/">
            <include name="*.jar"/>
         </fileset>
    </classpath>
  		<test name="org.openedit.html.AllTests" outfile="./junit" todir="${deployDir}">
  			<formatter type="xml"/>
  		</test>
  	</junit>
  </target>
  
    
	
	<target name="publish" depends=" zip">
		  
			<!-- publish results to anthill -->
		 	<copy file="${deployDir}/builds/testbench.zip" overwrite="true"
		        tofile="${basedir}/testbench.zip"/>
		
		
			
		</target>
	  
  
  
 
</project>
