<?xml version="1.0"?>
<project basedir="." default="all">
	
	<property file="build.properties.local" />
	<property file="build.properties" />
	<taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask" classpath="${jscompiler}" />

	<target name="merge">
    	<concat destfile="${basedir}/../../build/shapes.js" fixlastline="yes" append="no">
    		<fileset dir="${war.dir}/shapes" includes="**/*.js"/>
    		<fileset dir="${war.dir}/stencils" includes="**/*.js"/>
    	</concat>
    	
		<echo file="${war.dir}/stencils.xml" append="false"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>]]>${line.separator}</echo>
		<echo file="${war.dir}/stencils.xml" append="true"><![CDATA[<stencils>]]>${line.separator}</echo>
    	<concat destfile="${war.dir}/stencils.xml" fixlastline="yes" append="yes">
    		<fileset dir="${war.dir}/shapes" includes="**/*.xml"/>
    		<fileset dir="${war.dir}/stencils" includes="**/*.xml"/>
    	</concat>
    	<echo file="${war.dir}/stencils.xml" append="true"><![CDATA[</stencils>]]></echo>

		<jscomp compilationLevel="simple" debug="false" forceRecompile="true" output="${war.dir}/js/shapes.min.js">
			<sources dir="${basedir}/../../build">
				<file name="shapes.js" />
			</sources>
		</jscomp>
		
		<tstamp>
      		<format property="time.stamp" pattern="MM/dd/yyyy hh:mm aa" unit="hour"/>
  		</tstamp>
		
		<echo file="${war.dir}/cache.manifest" append="false">CACHE MANIFEST${line.separator}${line.separator}</echo>
		<echo file="${war.dir}/cache.manifest" append="true"># THIS FILE WAS GENERATED. DO NOT MODIFY!${line.separator}</echo>
		<echo file="${war.dir}/cache.manifest" append="true"># ${time.stamp}${line.separator}${line.separator}</echo>
    	<concat destfile="${war.dir}/cache.manifest" fixlastline="yes" append="true">
    		<fileset dir="${basedir}" includes="cache.txt"/>
    	</concat>
    	
		<delete dir="${basedir}/../../build"/>
	</target>

	<target name="app" depends="merge">
		<delete file="${basedir}/.tmp0.js"/>
		<echo file="${basedir}/.tmp0.js">
			var mxBasePath = 'https://www.draw.io/mxgraph/';
			var mxLoadStylesheets = false;
		</echo>

		<delete file="${basedir}/.tmp1.js"/>
		<copy file="${war.dir}/styles/default.xml" tofile="${basedir}/.tmp1.xml" overwrite="true"/>
		<replaceregexp file="${basedir}/.tmp1.xml" match="${line.separator}" flags="g" replace=""/>
		<replaceregexp file="${basedir}/.tmp1.xml" match="\t" flags="g" replace=""/>
		<replaceregexp file="${basedir}/.tmp1.xml" match="'" replace="\\\\'" byline="true"/>
		
		<delete file="${basedir}/Graph-Stylesheet.js"/>
		<echo file="${basedir}/Graph-Stylesheet.js">Graph.prototype.defaultThemes[Graph.prototype.defaultThemeName] = mxUtils.parseXml('</echo>
		<concat destfile="${basedir}/Graph-Stylesheet.js" fixlastline="no" append="true">
			<filelist dir="${basedir}">
				<file name=".tmp1.xml" />
			</filelist>
    	</concat>
		<echo file="${basedir}/Graph-Stylesheet.js" append="true">').documentElement;</echo>

		<jscomp compilationLevel="simple" forceRecompile="true" debug="false" output="${basedir}/grapheditor.min.js">
			<sources dir="${grapheditor.dir}">
				<file name="Editor.js" />
				<file name="EditorUi.js" />
				<file name="Sidebar.js" />
				<file name="Graph.js" />
				<file name="Shapes.js" />
				<file name="Actions.js" />
				<file name="Menus.js" />
				<file name="Format.js" />
				<file name="Toolbar.js" />
				<file name="Dialogs.js" />
			</sources>
		</jscomp>
		
		<jscomp compilationLevel="simple" debug="false" forceRecompile="true" output="${basedir}/sidebar.min.js">
			<sources dir="${war.dir}/js/diagramly/sidebar">
				<file name="Sidebar.js" />
				<file name="Sidebar-Advanced.js" />
				<file name="Sidebar-Android.js" />
				<file name="Sidebar-ArchiMate.js" />
				<file name="Sidebar-ArchiMate3.js" />
				<file name="Sidebar-Arrows2.js" />
				<file name="Sidebar-AWS.js" />
				<file name="Sidebar-AWS3.js" />
				<file name="Sidebar-AWS3D.js" />
				<file name="Sidebar-Azure.js" />
				<file name="Sidebar-Basic.js" />
				<file name="Sidebar-Bootstrap.js" />
				<file name="Sidebar-BPMN.js" />
				<file name="Sidebar-Cabinet.js" />
				<file name="Sidebar-Citrix.js" />
				<file name="Sidebar-EIP.js" />
				<file name="Sidebar-Electrical.js" />
				<file name="Sidebar-ER.js" />
				<file name="Sidebar-Floorplan.js" />
				<file name="Sidebar-Flowchart.js" />
				<file name="Sidebar-GCP.js" />
				<file name="Sidebar-Gmdl.js" />
				<file name="Sidebar-Ios.js" />
				<file name="Sidebar-Ios7.js" />
				<file name="Sidebar-LeanMapping.js" />
				<file name="Sidebar-Mockup.js" />
				<file name="Sidebar-MSCAE.js" />
				<file name="Sidebar-Network.js" />
				<file name="Sidebar-Office.js" />
				<file name="Sidebar-PID.js" />
				<file name="Sidebar-Rack.js" />
				<file name="Sidebar-Sysml.js" />
				<file name="Sidebar-Veeam.js" />
				<file name="Sidebar-WebIcons.js" />
			</sources>
		</jscomp>

		<jscomp compilationLevel="simple" forceRecompile="true" debug="false" output="${basedir}/base-viewer.min.js">
			<sources dir="${basedir}">
				<file name=".tmp0.js"/>
			</sources>
			
			<sources dir="${war.dir}/js/spin">
				<file name="spin.min.js" />
			</sources>
				
			<sources dir="${war.dir}/js/sanitizer">
				<file name="sanitizer.min.js"/>
			</sources>

			<sources dir="${war.dir}/js/deflate">
				<file name="base64.js" />
				<file name="pako.min.js" />
			</sources>

			<sources dir="${war.dir}/js/diagramly">
				<file name="Init.js" />
			</sources>

			<sources dir="${grapheditor.dir}">
				<file name="Init.js" />
			</sources>

			<sources dir="${basedir}/../mxgraph">
				<file name="mxClient.js" />
			</sources>

			<sources dir="${grapheditor.dir}">
				<file name="Editor.js" />
				<file name="EditorUi.js" />
				<file name="Graph.js" />
				<file name="Shapes.js" />
				<file name="Actions.js" />
			</sources>
			
			<sources dir="${war.dir}/js/diagramly">
				<file name="DrawioFile.js" />
				<file name="LocalFile.js" />
				<file name="Editor.js" />
				<file name="EditorUi.js" />
				<file name="Pages.js" />
			</sources>
					
			<sources dir="${basedir}">
				<file name="Graph-Stylesheet.js" />
			</sources>

			<sources dir="${war.dir}/js/diagramly">
				<file name="GraphViewer.js" />
			</sources>
		</jscomp>
		
		<delete file="${basedir}/.tmp1.xml"/>

		<echo file="${basedir}/.tmp1.js">
			(function()
			{
				Editor.initMath();
				GraphViewer.initCss();
				mxStencilRegistry.dynamicLoading = false;
				
				if (window.onDrawioViewerLoad != null)
				{
					window.onDrawioViewerLoad();
				}
				else
				{
					GraphViewer.processElements();
				}
			})();
		</echo>
		
		<jscomp compilationLevel="simple" forceRecompile="true" debug="false" output="${war.dir}/js/viewer.min.js">
			<sources dir="${basedir}">
				<file name="base-viewer.min.js"/>
			</sources>

			<sources dir="${basedir}">
				<file name=".tmp1.js"/>
			</sources>
		</jscomp>
		
		<delete file="${basedir}/.tmp0.js"/>
		<delete file="${basedir}/.tmp1.js"/>

		<jscomp compilationLevel="simple" debug="false" forceRecompile="true" output="${basedir}/base.min.js">
			<sources dir="${war.dir}/js/spin">
				<file name="spin.min.js" />
			</sources>

			<sources dir="${war.dir}/js/sanitizer">
				<file name="sanitizer.min.js" />
			</sources>

			<sources dir="${war.dir}/js/deflate">
				<file name="pako.min.js" />
				<file name="base64.js" />
			</sources>

			<sources dir="${war.dir}/js/diagramly">
				<file name="Init.js" />
			</sources>

			<sources dir="${grapheditor.dir}">
				<file name="Init.js" />
			</sources>

			<sources dir="${basedir}/../mxgraph">
				<file name="mxClient.js" />
			</sources>
			
			<sources dir="${basedir}">
				<file name="grapheditor.min.js"/>
				<file name="sidebar.min.js"/>
			</sources>

			<sources dir="${war.dir}/js/diagramly">
				<file name="DrawioFile.js" />
				<file name="LocalFile.js" />
				<file name="LocalLibrary.js" />
				<file name="StorageFile.js" />
				<file name="StorageLibrary.js" />
				<file name="Dialogs.js" />
				<file name="Editor.js" />
				<file name="EditorUi.js" />
				<file name="Settings.js" />
				<file name="App.js" />
				<file name="Menus.js" />
				<file name="Pages.js" />
			</sources>
				
			<sources dir="${basedir}">
				<file name="Graph-Stylesheet.js" />
			</sources>
		</jscomp>

		<loadfile property="version" srcFile="../../VERSION"/>
		<replace file="${basedir}/base.min.js" token="@DRAWIO-VERSION@" value="${version}"/>
		
		<jscomp compilationLevel="simple" debug="false" forceRecompile="true" output="${war.dir}/js/extensions.min.js">
			<sources dir="${war.dir}/js/diagramly">
				<file name="Extensions.js" />
			</sources>
		</jscomp>
		
		<jscomp compilationLevel="simple" debug="false" forceRecompile="true" output="${war.dir}/js/app.min.js">
			<sources dir="${war.dir}/js/spin">
				<file name="spin.min.js" />
			</sources>

			<sources dir="${war.dir}/js/sanitizer">
				<file name="sanitizer.min.js" />
			</sources>

			<sources dir="${war.dir}/js/deflate">
				<file name="pako.min.js" />
				<file name="base64.js" />
			</sources>

			<sources dir="${war.dir}/js/diagramly">
				<file name="Init.js" />
			</sources>

			<sources dir="${grapheditor.dir}">
				<file name="Init.js" />
			</sources>

			<sources dir="${basedir}/../mxgraph">
				<file name="mxClient.js" />
			</sources>

			<sources dir="${basedir}">
				<file name="grapheditor.min.js"/>
				<file name="sidebar.min.js"/>
			</sources>

			<sources dir="${war.dir}/js/diagramly">
				<file name="DrawioFile.js" />
				<file name="LocalFile.js" />
				<file name="LocalLibrary.js" />
				<file name="StorageFile.js" />
				<file name="StorageLibrary.js" />
				<file name="Dialogs.js" />
				<file name="Editor.js" />
				<file name="EditorUi.js" />
				<file name="Settings.js" />
			</sources>
				
			<sources dir="${basedir}">
				<file name="Graph-Stylesheet.js" />
			</sources>

			<sources dir="${war.dir}/js/diagramly/util">
				<file name="mxAsyncCanvas.js" />
				<file name="mxJsCanvas.js" />
			</sources>

			<sources dir="${war.dir}/js/diagramly">
				<file name="DrawioClient.js" />
				<file name="DrawioUser.js" />
				<file name="UrlLibrary.js" />
				<file name="DriveRealtime.js" />
				<file name="RealtimeMapping.js" />
				<file name="DriveFile.js" />
				<file name="DriveLibrary.js" />
				<file name="DriveClient.js" />
				<file name="DropboxFile.js" />
				<file name="DropboxLibrary.js" />
				<file name="DropboxClient.js" />
				<file name="OneDriveFile.js" />
				<file name="OneDriveLibrary.js" />
				<file name="OneDriveClient.js" />
				<file name="GitHubFile.js" />
				<file name="GitHubLibrary.js" />
				<file name="GitHubClient.js" />
				<file name="ChatWindow.js" />
			</sources>
			
			<sources dir="${war.dir}/js/diagramly">
				<file name="App.js" />
				<file name="Menus.js" />
				<file name="Pages.js" />
			</sources>
		</jscomp>
		
		<replace file="${war.dir}/js/app.min.js" token="@DRAWIO-VERSION@" value="${version}"/>

		<delete file="${basedir}/Graph-Stylesheet.js"/>
		<delete file="${basedir}/grapheditor.min.js"/>
		<delete file="${basedir}/sidebar.min.js"/>
	</target>

	<target name="all" depends="app">
	</target>

	<!-- ================== Stand-alone war creation ============================= -->

	<path id="javac.class.path">
		<fileset dir="${war.dir}/WEB-INF/lib" includes="mxgraph-core.jar" />
		<fileset dir="${basedir}/jars" includes="servlet-api.jar" />
		<fileset dir="${basedir}/jars" includes="commons-codec-1.10.jar" />
		<fileset dir="${basedir}/jars" includes="commons-fileupload-1.3.2.jar" />
		<fileset dir="${basedir}/jars" includes="gson-2.7.jar" />
		<fileset dir="${basedir}/jars" includes="commons-lang3-3.5.jar" />
		<fileset dir="${basedir}/jars" includes="gae-stub-1.0.1.jar" />
	</path>

	<target name="javac" depends="app" description="Java compilation">
		<mkdir dir="${javac.dir}"/>
		<javac srcdir="${src.dir}" excludes="**/EmbedServlet2.java" encoding="utf-8" destdir="${javac.dir}" source="1.7" target="1.7" debug="true" debuglevel="lines,vars,source">
			<classpath refid="javac.class.path" />
		</javac>
		<copy todir="${javac.dir}/META-INF">
			<fileset dir="${src.dir}/META-INF">
			</fileset>
		</copy>
		<copy todir="${javac.dir}" file="${src.dir}/log4j.properties" />
	</target>

	<target name="clean" description="Cleans build directories">
		<delete failonerror="false">
			<fileset dir="${javac.dir}">
			</fileset>
			<fileset dir="${build.dir}">
			</fileset>
		</delete>
	</target>
	
	<target name="war" depends="javac" description="Create the stand-alone war file">
		<zip excludes="**/appengine*.jar,**/jsr*.jar,**/junit*.jar"  destfile="${build.dir}/${war.name}" basedir="${war.dir}" >
		</zip>
	</target>

</project>
