<?xml version="1.0"?>

<project name="azkaban" basedir="." default="all">
	<property file="build.properties" />
	<property name="base.dir" value="${basedir}" />
	<property name="dist.jar.dir" value="${basedir}/dist/jars" />
	<property name="dist.dust.dir" value="${basedir}/dist/dust" />
	<property name="dist.less.dir" value="${basedir}/dist/less" />
	<property name="dist.classes.dir" value="${basedir}/dist/classes" />
	<property name="dist.packages.dir" value="${basedir}/dist/packages" />
	<property name="dist.web.package.dir" value="${dist.packages.dir}/azkaban-web-server" />
	<property name="dist.exec.package.dir" value="${dist.packages.dir}/azkaban-exec-server" />
	<property name="dist.solo.package.dir" value="${dist.packages.dir}/azkaban-solo-server" />
	<property name="dist.sql.package.dir" value="${dist.packages.dir}/sql" />	

	<property name="conf.dir" value="${basedir}/conf" />
	<property name="web.package.dir" value="${basedir}/src/package/webserver" />
	<property name="exec.package.dir" value="${basedir}/src/package/execserver" />
	<property name="solo.package.dir" value="${basedir}/src/package/soloserver" />
	
	<property name="lib.dir" value="${basedir}/lib" />
	<property name="bin.dir" value="${basedir}/bin" />
	<property name="java.src.dir" value="${basedir}/src/java" />
	<property name="dust.src.dir" value="${basedir}/src/tl" />
	<property name="less.src.dir" value="${basedir}/src/less" />
	<property name="web.src.dir" value="${basedir}/src/web" />
	<property name="sql.src.dir" value="${basedir}/src/sql" />
	
	<property name="azkaban.jar" value="${dist.jar.dir}/${name}-${version}.jar" />

	<property environment="env" />

	<path id="main.classpath">
		<fileset dir="${lib.dir}">
			<include name="*.jar" />
		</fileset>

		<pathelement path="${dist.classes.dir}" />
	</path>

	<!-- set the build number based on environment variable, otherwise blank -->
	<property environment="env" description="System environment variables (including those set by Hudson)" />

	<target name="all" depends="clean, jars" description="Builds all jars" />

	<target name="clean" description="Delete generated files.">
		<echo message="Deleting generated files in dist" />
		<delete dir="${dist.jar.dir}" />
		<delete dir="${dist.classes.dir}" />
	</target>

	<target name="build" description="Compile main source tree java files">
		<delete dir="${dist.classes.dir}" />
		<mkdir dir="${dist.classes.dir}" />
		<delete dir="${dist.dust.dir}" />
		<mkdir dir="${dist.dust.dir}" />
		<delete dir="${dist.less.dir}" />
		<mkdir dir="${dist.less.dir}" />
		
		<!-- copy non-java files to classes dir to load from classpath -->
		<copy todir="${dist.classes.dir}">
			<fileset dir="${java.src.dir}">
				<exclude name="**/*.java" />
			</fileset>
		</copy>
		
		<javac fork="true" destdir="${dist.classes.dir}"
			target="1.6" debug="true" deprecation="false" failonerror="true">
			<src path="${java.src.dir}" />
			<classpath refid="main.classpath" />
		</javac>

		<!-- Compile dustjs templates -->
		<!-- Note: Because apply does not support multiple srcfile and targetfile
				 elements, and for and foreach requires ant-contrib, we use targetfile 
				 for the template name parameter and then redirect the output of dustc
				 to the final output file -->
		<echo message="Compiling Dust templates." />
		<apply dir="${dust.src.dir}" executable="dustc" relative="true">
			<mapper type="glob" from="*.tl" to="*" />
			<targetfile prefix="--name=" />
			<srcfile />
			<fileset dir="${dust.src.dir}" includes="*.tl" />
			<redirector>
				<outputmapper id="out" type="glob" from="*.tl" to="${dist.dust.dir}/*.js" />
			</redirector>
		</apply>

		<!-- Compile LESS to CSS -->
		<echo message="Compiling LESS style sheets." />
		<apply dir="${less.src.dir}" executable="lessc" relative="true">
			<mapper type="glob" from="*.less" to="*.css" />
			<srcfile />
			<fileset dir="${less.src.dir}" includes="*.less" />
			<redirector>
				<outputmapper id="out" type="glob" from="*.less" to="${dist.less.dir}/*.css" />
			</redirector>
		</apply>
	</target>
	
	<target name="jars" depends="build" description="Create azkaban jar">
		<mkdir dir="${dist.jar.dir}" />
		<jar destfile="${azkaban.jar}">
			<fileset dir="${dist.classes.dir}">
				<include name="**/*.*" />
			</fileset>
		</jar>
	</target>
	
	<target name="create-update-script" description="Prepare the creation of the Azkaban Scripts">
		<!-- Generic update table scripts -->
		<concat destfile="${dist.sql.package.dir}/update-all-sql-${updateVersion}.sql" fixlastline="yes">
			<fileset dir="${sql.src.dir}" >
				<include name="update.*.${updateVersion}.sql"/>
			</fileset>	
		</concat>
	</target>
	
	<target name="create-update-script-2.1" description="Prepare the creation of the Azkaban Scripts">
		<!-- 2.1 added the active_sla table -->
		<concat destfile="${dist.sql.package.dir}/update-all-sql-2.1.sql" fixlastline="yes">
			<fileset dir="${sql.src.dir}" >
				<include name="create.active_sla.sql"/>
				<include name="update.*.2.1.sql"/>
			</fileset>	
		</concat>
	</target>
	
	<target name="create-update-script-2.2" description="Prepare the creation of the Azkaban Scripts">
		<!-- 2.2 added the properties table -->
		<concat destfile="${dist.sql.package.dir}/update-all-sql-2.2.sql" fixlastline="yes">
			<fileset dir="${sql.src.dir}" >
				<include name="create.properties.sql"/>
				<include name="update.*.2.2.sql"/>
			</fileset>	
		</concat>
	</target>
	
	<target name="package-sql-scripts" description="Creates a package of sql">
		<delete dir="${dist.sql.package.dir}" />
		<mkdir dir="${dist.sql.package.dir}" />

		<concat destfile="${dist.sql.package.dir}/create-all-sql-${version}.sql" fixlastline="yes">
			<fileset dir="${sql.src.dir}" >
				<exclude name="update.*.sql"/>
				<exclude name="database.properties"/>
			</fileset>	
		</concat>
		
		<!-- Collect various update scripts. -->
		<!-- Not sure how to do this better yet. -->
		<antcall target="create-update-script-2.1"></antcall>
		<antcall target="create-update-script-2.2"></antcall>
		<!-- End script collection-->
		
		<copy todir="${dist.sql.package.dir}" >
			<fileset dir="${sql.src.dir}" />
		</copy>
		<echo file="${dist.sql.package.dir}/database.properties" append="true">version=${version}</echo>

		<tar destfile="${dist.sql.package.dir}/${name}-sql-script-${version}.tar.gz" compression="gzip" longfile="gnu">
			<tarfileset dir="${dist.sql.package.dir}" prefix="azkaban-${version}" filemode="755" />
		</tar>
	</target>

	<target name="package-web-server" depends="jars" description="Creates a package for the webserver">
		<delete dir="${dist.web.package.dir}" />
		<mkdir dir="${dist.web.package.dir}" />
		<mkdir dir="${dist.web.package.dir}/conf" />
		<mkdir dir="${dist.web.package.dir}/bin" />
		<mkdir dir="${dist.web.package.dir}/lib" />
		<mkdir dir="${dist.web.package.dir}/web" />
		<mkdir dir="${dist.web.package.dir}/plugins" />
		<mkdir dir="${dist.web.package.dir}/extlib" />	
			
		<!-- Copy Azkaban jars and libs-->
		<copy file="${azkaban.jar}" todir="${dist.web.package.dir}/lib" />
		<copy todir="${dist.web.package.dir}/lib" >
			<fileset dir="${lib.dir}" >
				<exclude name="hadoop-core*.jar"/>
			</fileset>		
		</copy>
		
		<!-- Copy bin files for web server only-->
		<copy todir="${dist.web.package.dir}/bin">
			<fileset dir="${web.package.dir}/bin"/>
		</copy>
		
		<!-- Copy web files -->
		<copy todir="${dist.web.package.dir}/web">
			<fileset dir="${web.src.dir}" />
		</copy>

		<!-- Copy compiled dust templates -->
		<copy todir="${dist.web.package.dir}/web/js">
			<fileset dir="${dist.dust.dir}" />
		</copy>

		<!-- Copy compiled less CSS -->
		<copy todir="${dist.web.package.dir}/web/css">
			<fileset dir="${dist.less.dir}" />
		</copy>
		
		<!-- Copy conf create table scripts -->
		<copy todir="${dist.web.package.dir}/conf">
			<fileset dir="${web.package.dir}/conf" />
		</copy>
		
		<!-- Tarball it -->
		<tar destfile="${dist.web.package.dir}/${name}-web-server-${version}.tar.gz" compression="gzip" longfile="gnu">
			<tarfileset dir="${dist.web.package.dir}" prefix="azkaban-${version}" filemode="755" includes="bin/*" />
			
			<tarfileset dir="${dist.web.package.dir}" prefix="azkaban-${version}" includes="**">
				<exclude name="bin/*"/>
			</tarfileset>
		</tar>
	</target>
	
	<target name="package-exec-server" depends="jars" description="Creates a package for the execserver">
		<delete dir="${dist.exec.package.dir}" />
		<mkdir dir="${dist.exec.package.dir}" />
		<mkdir dir="${dist.exec.package.dir}/conf" />
		<mkdir dir="${dist.exec.package.dir}/bin" />
		<mkdir dir="${dist.exec.package.dir}/lib" />
		<mkdir dir="${dist.exec.package.dir}/plugins" />
		<mkdir dir="${dist.exec.package.dir}/extlib" />
				
		<!-- Copy Azkaban jars and libs-->
		<copy file="${azkaban.jar}" todir="${dist.exec.package.dir}/lib" />
		<copy todir="${dist.exec.package.dir}/lib" >
			<fileset dir="${lib.dir}" >
				<exclude name="hadoop-core*.jar"/>
			</fileset>
		</copy>
		
		<!-- Copy bin files for exec server only-->
		<copy todir="${dist.exec.package.dir}/bin" >
			<fileset dir="${exec.package.dir}/bin"/>
		</copy>

		<!-- Copy conf files -->
		<copy todir="${dist.exec.package.dir}/conf" >
			<fileset dir="${exec.package.dir}/conf" />
		</copy>
		
		<!-- Tarball it -->
		<tar destfile="${dist.exec.package.dir}/${name}-executor-server-${version}.tar.gz" compression="gzip" longfile="gnu">
			<tarfileset dir="${dist.exec.package.dir}" prefix="azkaban-${version}" filemode="755" includes="bin/*" />
			
			<tarfileset dir="${dist.exec.package.dir}" prefix="azkaban-${version}" includes="**">
				<exclude name="bin/*"/>
			</tarfileset>
		</tar>
	</target>
	
	<target name="package-solo-server" depends="jars" description="Creates a package for the solo server">
		<delete dir="${dist.solo.package.dir}" />
		<mkdir dir="${dist.solo.package.dir}" />
		<mkdir dir="${dist.solo.package.dir}/conf" />
		<mkdir dir="${dist.solo.package.dir}/bin" />
		<mkdir dir="${dist.solo.package.dir}/lib" />
		<mkdir dir="${dist.solo.package.dir}/plugins" />
		<mkdir dir="${dist.solo.package.dir}/extlib" />
		<mkdir dir="${dist.solo.package.dir}/sql" />
				
		<!-- Copy Azkaban jars and libs-->
		<copy file="${azkaban.jar}" todir="${dist.solo.package.dir}/lib" />
		<copy todir="${dist.solo.package.dir}/lib" >
			<fileset dir="${lib.dir}" >
				<exclude name="hadoop-core*.jar"/>
			</fileset>
		</copy>
		
		<!-- Copy bin files for exec server only-->
		<copy todir="${dist.solo.package.dir}/bin" >
			<fileset dir="${solo.package.dir}/bin"/>
		</copy>

		<!-- Copy conf files -->
		<copy todir="${dist.solo.package.dir}/conf" >
			<fileset dir="${solo.package.dir}/conf" />
		</copy>
		
		<!-- Copy web files -->
		<copy todir="${dist.solo.package.dir}/web" >
			<fileset dir="${web.src.dir}" />
		</copy>
		
		<!-- Copy compiled dust templates -->
		<copy todir="${dist.solo.package.dir}/web/js">
			<fileset dir="${dist.dust.dir}" />
		</copy>
		
		<!-- Copy compiled less CSS -->
		<copy todir="${dist.solo.package.dir}/web/css">
      <fileset dir="${dist.less.dir}">
        <include name="azkaban.css" />
        <include name="azkaban-svg.css" />
      </fileset>
		</copy>
		
		<!-- Copy sql files -->
		<copy todir="${dist.solo.package.dir}/sql" >
			<fileset dir="${sql.src.dir}" />
		</copy>
		<echo file="${dist.solo.package.dir}/sql/database.properties" append="true">version=${version}</echo>
			
		<!-- Tarball it -->
		<tar destfile="${dist.solo.package.dir}/${name}-solo-server-${version}.tar.gz" compression="gzip" longfile="gnu">
			<tarfileset dir="${dist.solo.package.dir}" prefix="azkaban-${version}" filemode="755" includes="bin/*" />
			
			<tarfileset dir="${dist.solo.package.dir}" prefix="azkaban-${version}" includes="**">
				<exclude name="bin/*"/>
			</tarfileset>
		</tar>
	</target>
	
	<target name="package-all" depends="package-exec-server, package-web-server, package-solo-server, package-sql-scripts" description="Create all packages">
	</target>

	<target name="package" depends="package-all" description="Create all packages">
	</target>

</project>
