<?xml version="1.0" encoding="utf-8"?>

<project name="OmegaT-WebStart" default="pack" basedir=".">
	<property name="root" value="../.." />
	<property file="../../../webstart.properties" />
	
	<condition property="has.layout.defaults">
		<available file="uiLayout.xml" />
	</condition>
	
	<!-- Ant-contrib is required in order to mark all JARs as "Trusted-Library: true"
		 so as to suppress a Java Web Start "mixed content" warning on launch.
		 Install ant-contrib.jar in ANT_HOME/lib or ~/.ant/lib.
		 http://ant-contrib.sourceforge.net/ -->
	<condition property="ant-contrib.installed">
		<available classname="net.sf.antcontrib.logic.ForEach" />
	</condition>
	<taskdef resource="net/sf/antcontrib/antlib.xml" />
	
	<!-- Build WebStart pack -->
	<target name="pack" depends="clean,copy-jars,set-bundled-defaults,make-trusted,sign-no-timestamp,sign-and-timestamp"
		description="Build WebStart pack">
		<mkdir dir="${root}/dist2" />
		<zip destfile="${root}/dist2/OmegaT-WebStart.zip" basedir="${root}/build/webstart" />
	</target>

	<target name="clean">
		<delete dir="${root}/build/webstart" failonerror="false" />
	</target>
	
	<target name="copy-jars">
		<mkdir dir="${root}/build/webstart" />
		<copy todir="${root}/build/webstart">
			<fileset dir="." includes="*.jnlp" />
			<!-- Replace @@HTTP_BASE@@ placeholder -->
			<filterset begintoken="@@" endtoken="@@">
				<filter token="HTTP_BASE" value="${webstart.codebase}" />
			</filterset>
		</copy>

		<copy todir="${root}/build/webstart" flatten="yes">
			<fileset dir="${root}/lib" includes="**/*.jar" excludes="**/sources/**" />
			<fileset dir="${root}/lib-mnemonics/dist" includes="**/*.jar" />
			<fileset dir="${root}/dist" includes="OmegaT.jar" />
		</copy>

		<!-- Remove signatures from any pre-signed JARs.
			 TODO: Automate detection and application of this. -->
		<unsignjar jarFile="${root}/build/webstart/org.eclipse.jgit-3.7.1.201504261725-r.jar" />
		
		<jar jarfile="${root}/build/webstart/docs.jar" basedir="${root}" 
			includes="docs/changes.txt docs/OmegaT-license.txt
								docs/**/instantStartGuideNoTOC.html
								docs/**/OmegaT.css
								docs/**/images/NewProject.png
								docs/**/images/InstantGuide_1.png 
								docs/**/images/InstantGuide_2.png" />

		<jar jarfile="${root}/build/webstart/hunspell-linux64.jar" basedir="${root}/native" includes="libhunspell-linux64.so" />
		<jar jarfile="${root}/build/webstart/hunspell-linux32.jar" basedir="${root}/native" includes="libhunspell-linux32.so" />
		<jar jarfile="${root}/build/webstart/hunspell-macos64.jar" basedir="${root}/native" includes="libhunspell-macos64.dylib" />
		<jar jarfile="${root}/build/webstart/hunspell-macos32.jar" basedir="${root}/native" includes="libhunspell-macos32.dylib" />
		<jar jarfile="${root}/build/webstart/hunspell-win64.jar" basedir="${root}/native" includes="hunspell-win64.dll" />
		<jar jarfile="${root}/build/webstart/hunspell-win32.jar" basedir="${root}/native" includes="hunspell-win32.dll" />
	</target>
	
	<!-- Macro to un-sign a JAR. From:
		 https://techtavern.wordpress.com/2014/04/01/ant-task-to-remove-signature-from-jar-file/ -->
	<macrodef name="unsignjar">
		<attribute name="jarFile" />
		<sequential>
			<jar update="yes" jarfile="@{jarFile}.tmp">
				<zipfileset src="@{jarFile}" casesensitive="false">
					<include name="**"/>
					<exclude name="META-INF/*.SF"/>
					<exclude name="META-INF/*.DSA"/>
					<exclude name="META-INF/*.RSA"/>
				</zipfileset>
			</jar>
			<move file="@{jarFile}.tmp" tofile="@{jarFile}" overwrite="true" />
		</sequential>
	</macrodef>
	
	<target name="set-bundled-defaults" depends="set-layout-defaults">
		<!-- Place an omegat.prefs (omegat-$BRANDING.prefs if using branding)
			 in release/WebStart-specific to bundle default preferences for new users. -->
		<jar destfile="${root}/build/webstart/OmegaT.jar" update="true">
			<zipfileset dir="." includes="*.prefs" prefix="org/omegat/util/" />
		</jar>
	</target>
	
	<target name="set-layout-defaults" description="Set window layout defaults" if="has.layout.defaults">
		<!-- Place a uiLayout.xml in release/WebStart-specific to bundle a custom
			 default window layout for new users. -->
		<zip destfile="temp.jar">
			<zipfileset src="${root}/build/webstart/OmegaT.jar" excludes="org/omegat/gui/main/DockingDefaults.xml" />
		</zip>
		<move file="temp.jar" tofile="${root}/build/webstart/OmegaT.jar" />
		<jar destfile="${root}/build/webstart/OmegaT.jar" update="true" >
			<zipfileset dir="." includes="uiLayout.xml" fullpath="org/omegat/gui/main/DockingDefaults.xml" />
		</jar>
	</target>
	
	<target name="make-trusted" if="ant-contrib.installed"
		description="Mark each included JAR as Trusted-Library: true">
		<foreach target="make-trusted-jar" param="trusted.jar">
			<path>
				<fileset dir="${root}/build/webstart/" includes="*.jar" />
			</path>
		</foreach>
	</target>
	
	<target name="make-trusted-jar">
		<jar destfile="${trusted.jar}" update="true">
			<manifest>
				<attribute name="Trusted-Library" value="true" />
			</manifest>
		</jar>
	</target>
	
	<target name="sign-no-timestamp" unless="signkey.tsaurl">
		<signjar keystore="${signkey.keystore}" alias="${signkey.alias}" storepass="${signkey.storepass}"
			keypass="${signkey.keypass}">
			<fileset dir="${root}/build/webstart/" includes="*.jar" />
		</signjar>
	</target>
	
	<target name="sign-and-timestamp" if="signkey.tsaurl">
		<signjar keystore="${signkey.keystore}" alias="${signkey.alias}" storepass="${signkey.storepass}"
			keypass="${signkey.keypass}" tsaurl="${signkey.tsaurl}">
			<fileset dir="${root}/build/webstart/" includes="*.jar" />
		</signjar>
	</target>
		
</project>
