<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="makeall">
	
	<taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask">
		<classpath>
			<pathelement location="../res/compiler.jar" />
		</classpath>
	</taskdef>
	
	<taskdef resource="net/sf/antcontrib/antlib.xml">
	  <classpath>
	    <pathelement location="../res/ant-contrib.jar"/>
	  </classpath>
	</taskdef>
	
	<delete dir="${basedir}/../../build" />
	<mkdir  dir="${basedir}/../../build" />
		
	<target name="makeall" depends="concatenate,compile,jslint" />
	
	<target name="make_templates" description="Concatenate the html templates together">
	    <for param="file">
			<path>
	    		<fileset dir="../../src/templates" includes="*.html" />
			</path>
	    	<sequential>
	    		
	    		<basename property="filename" file="@{file}"/>
	    		<basename property="filehead" file="@{file}" suffix=".html"/>
	    		
	    		<echo file="../../build/compiled_templates.js" append="yes" 
	    				message="${line.separator}IriSP.${filehead}_template = &quot;" />
	    		
	    		<concat append="yes" destfile="../../build/compiled_templates.js">
	    			<filelist dir="../../src/templates" files="${filename}"></filelist>
	    			<filterchain>
	    				<striplinebreaks/>
	    			</filterchain>
	    		</concat>	    			    	
	    		<echo file="../../build/compiled_templates.js" append="yes" message="&quot;;" />
	    		
	    		<var name="filename" unset="true" />
	    		<var name="filehead" unset="true" />
	    	</sequential>
		</for>
	</target>
	
	<target name="compile" depends="make_templates">
		<jscomp compilationLevel="simple" warning="quiet" debug="false"
			output="../../build/LdtPlayer.min.raw.js">
			<externs dir="${basedir}/../../res/">
				<file name="js/jquery.min.js" />
				<file name="js/jquery.tools.min.js" />
				<file name="js/jquery-ui.min.js" />
				<file name="js/swfobject.js" />
			</externs>
			<sources dir="${basedir}/../../src/js">
				<file name="LdtPlayer.js" />
			</sources>
		</jscomp>
		<concat destfile="../../build/LdtPlayer.min.js"
			append="false">
			<filelist dir="../../src/js/" files="header.js" />
	        <filelist dir="../../build/" files="LdtPlayer.min.raw.js" />
		</concat>
		<delete file="../../build/LdtPlayer.min.raw.js" />
		<delete file="../../build/compiled_templates.js" />
	</target>
	    	
	<target name="concatenate" description="Build the developer release file" depends="make_templates">
	    <concat encoding="UTF-8" outputencoding="UTF-8" destfile="../../build/LdtPlayer-release.js">
			<filelist dir="../../src/js/" files="header.js" />
	        <filelist dir="../../src/js/libs" files="popcorn.js popcorn.youtube.js popcorn.code.js popcorn.jwplayer.js popcorn.mediafragment.js jwplayer.js mustache.js raphael.js" />
	        <filelist dir="../../src/js" files="LdtPlayer.js" />
	    	<filelist dir="../../build" files="compiled_templates.js" />
	    	<filelist dir="../../src/js" files="utils.js data.js site.js ui.js widgets.js layout.js init.js" />
			<fileset dir="../../src/js/widgets" casesensitive="yes">
					<include name="**/*.js"/>					
			</fileset>
			<fileset dir="../../src/js/serializers" casesensitive="yes">
					<include name="**/*.js"/>					
			</fileset>
	    	<filterchain>
	    	    <deletecharacters chars="&#xFEFF;" />
	    	</filterchain>
	    </concat>
	</target>
	
	<property name="rhino.jar" value="${basedir}/../res/rhino.jar"/>  
	<property name="jslint-wrapper.js" value="${basedir}/../res/jslint-wrapper.js"/>
	<property name="jslint.js" value="${basedir}/../res/jslint.js"/>
	
	<target name="jslint" description="Run jslint on the files" depends="concatenate">
		<apply executable="java" parallel="false">  
			<filelist dir="../../build">  
				<file name="LdtPlayer-release.js"/>  
			</filelist>  
			<arg line="-jar"/>  
			<arg path="${rhino.jar}"/>  
			<arg path="${jslint-wrapper.js}"/>  
			<arg path="${jslint.js}"/>  
		</apply>
	</target>
</project>