<project name="AppleCommander" default="all" basedir="..">
	<description>
		This script builds the distribution components.
	</description>

	<!-- Fix ant warning (explained: https://stackoverflow.com/questions/5103384) -->
	<presetdef name="javac">
		<javac includeantruntime="false" />
	</presetdef>

	<property name="work" value="work"/>
	<property name="classes" value="${work}/classes"/>
	<property name="dist" value="${work}/dist"/>
	<property name="source" value="src"/>
	<property name="testsrc" value="test"/>
	<property name="build" value="build"/>
	<property name="doc" value="documentation"/>
	<property name="web" value="web"/>
	<property name="javadoc" value="${work}/javadoc"/>
	<property name="builddir" value="${basedir}/build" />
	<property name="mac.dir" value="${basedir}/mac"/>
	<property file="${builddir}/ACBuild.properties"/>
	<property file="${builddir}/ACBuild-default.properties"/>

	<!-- If you want to add a platform, create a conditional property for it here -->
	<condition property="isWin" value="true">
		<os family="windows" />
	</condition>
	<condition property="isMac" value="true">
		<and>
			<os family="mac" />
			<os family="unix" />
		</and>
	</condition>
	<condition property="isUnix" value="true">
		<!-- macOS also provides unix, so we must explicitly exclude it here -->
		<and>
			<os family="unix" />
			<not>
				<os family="mac" />
			</not>
		</and>
	</condition>

	<condition property="signing-needed">
		<available file="${keyconf}" property="keyconf-exists" />
	</condition>

	<taskdef name="jarbundler"
		classname="com.ultramixer.jarbundler.JarBundler"
		classpath="mac/jarbundler-core-3.3.0.jar" />

	<target name="version" description="Get version from source.">
		<property name="main.path" value="src/com/webcodepro/applecommander/ui"/>
		<loadfile srcfile="${main.path}/AppleCommander.java" property="acVersion">
			<filterchain>
				<linecontainsregexp>
					<regexp pattern='^.*String VERSION = ".*";.*$'/>
				</linecontainsregexp>
				<tokenfilter>
					<replaceregex pattern='^.*String VERSION = "(.*)";.*$' replace='\1'/>
				</tokenfilter>
				<striplinebreaks/>
			</filterchain>
		</loadfile>
		<echo>Building version ${acVersion}...</echo>
	</target>

	<target name="init-win" if="isWin">
		<property name="antjar" value="${antJarPathWin}" />
		<property name="swtjar" value="${swtJarPathWin}" />
		<property name="juintjar" value="${junitPathWin}" />
	</target>

	<target name="init-mac" if="isMac">
		<property name="antjar" value="${antJarPathMac}" />
		<property name="swtjar" value="${swtJarPathMac}" />
		<property name="juintjar" value="${junitPathMac}" />
		<condition property="noMacGui" value="true">
			<isset property="noGui" />
		</condition>
	</target>

	<target name="init-unix" if="isUnix">
		<property name="antjar" value="${antJarPathUnix}" />
		<property name="swtjar" value="${swtJarPathUnix}" />
		<property name="juintjar" value="${junitPathUnix}" />
	</target>

	<target name="init" depends="version,init-win,init-mac,init-unix"
		description="Ensure work directory is present">
		<mkdir dir="${work}"/>
		<mkdir dir="${classes}"/>
		<mkdir dir="${dist}"/>
		<mkdir dir="${javadoc}"/>
		<property name="guijar" value="${dist}/AppleCommander-${acVersion}.jar"/>
		<property name="cmdjar" value="${dist}/AppleCommander-${acVersion}-ac.jar"/>
		<property name="maczip" value="${dist}/AppleCommander-${acVersion}-mac.zip"/>
		<property name="srczip" value="${dist}/AppleCommander-${acVersion}-src.zip"/>
		<property name="webzip" value="${dist}/AppleCommander-${acVersion}-web.zip"/>
	</target>
	
	<target name="clean" depends="init" description="Wipes out work contents">
		<delete dir="${classes}"/>
		<delete dir="${dist}"/>
		<delete dir="${javadoc}"/>
	</target>
	
	<target name="all" depends="init,jars,macBundle,sourceZip,javadoc,websiteZip" description="Everything">
	    <!-- Nothing else to do - depends handles it all. -->
	</target>
	
	<target name="jars" depends="init,executableGuiJar,executableCmdJar,signJars" description="Build executable jars">
	    <!-- Nothing else to do - depends handles it all. -->
	</target>
	
	<target name="signJars" depends="init" if="signing-needed" description="Signs the jars">
		<!-- In ACBuild.properties => keyconf=${user.home}/.secret alias=name -->
		<!-- See man keytool, http://java.sun.com/j2se/1.5.0/docs/tooldocs/ -->
		<loadfile srcfile="${keyconf}" property="password"/>
		<signjar alias="${alias}" storepass="${password}">
			<fileset dir="${basedir}">
				<include name="${cmdjar}"/>
				<include name="${guijar}"/>
			</fileset>
		</signjar>
	</target>

	<target name="executableGuiJar" depends="init" unless="noGui"
		description="Build GUI executable JAR">
		<javac srcdir="${source}" destdir="${classes}" classpath="${antjar}:${swtjar}">
			<include name="**/*.java"/>
			<exclude name="**/*Test.java"/>
			<compilerarg value="-Xlint:unchecked"/>
		</javac>
		<jar jarfile="${guijar}" manifest="build/manifest.mf">
			<fileset dir="${classes}"/>
			<fileset dir="${source}">
				<include name="**/*.dump"/>
				<include name="**/*.properties"/>
				<include name="**/*.gif"/>
			</fileset>
			<fileset dir=".">
			    <include name="LICENSE"/>
			    <include name="TODO"/>
			    <include name="VERSIONS"/>
			</fileset>
		</jar>
	</target>

	<target name="executableCmdJar" depends="init"
		description="Build command-line only executable JAR">
		<!-- SWT needs to be in classpath if executableGuiCmdJar didn't run -->
		<javac srcdir="${source}" destdir="${classes}" classpath="${antjar}:${swtjar}">
			<include name="**/*.java"/>
			<exclude name="**/*Test.java"/>
			<compilerarg value="-Xlint:unchecked"/>
			<compilerarg value="-XDignore.symbol.file"/>
		</javac>
		<jar jarfile="${cmdjar}" manifest="build/ac.mf">
			<fileset dir="${classes}">
				<exclude name="com/webcodepro/applecommander/ui/swt/**"/>
				<exclude name="com/webcodepro/applecommander/ui/images/**"/>
			</fileset>
			<fileset dir="${source}">
				<include name="**/*.dump"/>
				<include name="**/*.properties"/>
			</fileset>
			<fileset dir=".">
			    <include name="LICENSE"/>
			    <include name="TODO"/>
			    <include name="VERSIONS"/>
			</fileset>
		</jar>
	</target>

	<target name="macBundle" depends="init,jars" if="isMac" unless="noMacGui"
		description="Build Mac application bundle.">
		<property name="mac.app" value="${dist}/AppleCommander.app"/>

		<!--
		     NB: This seems like the only obvious way to turn ${swtjar}
		     into something we can install using jarbundler to both
		     copy and add to classpath.  If you have a better way… :)
		-->
		<copy file="${swtjar}" tofile="${work}/swt/swt.jar" />

		<jarbundler dir="${dist}"
			name="AppleCommander" version="${acVersion}"
			verbose="true" showPlist="true"
			mainclass="com.webcodepro.applecommander.ui.AppleCommander"
			bundleid="com.webcodepro.applecommander"
			stubfile="${mac.dir}/universalJavaApplicationStub"
			icon="${mac.dir}/AppleCommander.icns"
			arguments="-swt" startonmainthread="true"
			jvmversion="1.6+" useJavaXKey="true" developmentregion="English"
			copyright="Copyright 2002-2008 Rob Greene and John B. Matthews.">
			<jarfilelist dir="${dist}" files="AppleCommander-${acVersion}.jar" />
			<jarfilelist dir="${work}/swt" files="swt.jar" />
		</jarbundler>

		<zip destfile="${maczip}">
			<zipfileset dir="${dist}/AppleCommander.app" prefix="AppleCommander-${acVersion}-mac/AppleCommander.app" />
			<zipfileset dir="${dist}" prefix="AppleCommander-${acVersion}-mac/jars">
				<include name="AppleCommander-${acVersion}*.jar" />
			</zipfileset>
		</zip>
	</target>

	<target name="sourceZip" depends="init" description="Build source ZIP archive">
	    <zip zipfile="${srczip}">
	    	<zipfileset dir=".">
	    		<include name="ECLIPSE-CONFIG"/>
	    		<include name="HEADER"/>
	    		<include name="LICENSE"/>
	    		<include name="NATIVE-COMPILE"/>
	    		<include name="TODO"/>
	    		<include name="VERSIONS"/>
	    	</zipfileset>
	        <zipfileset dir="${source}" prefix="${source}">
				<include name="**/*.java"/>
				<include name="**/*.dump"/>
				<include name="**/*.properties"/>
				<include name="**/*.gif"/>
	        </zipfileset>
	        <zipfileset dir="${testsrc}" prefix="${testsrc}">
				<include name="**/*.java"/>
				<include name="**/*.dump"/>
				<include name="**/*.properties"/>
				<include name="**/*.gif"/>
	        </zipfileset>
	        <zipfileset dir="${build}" prefix="${build}"/>
	        <zipfileset dir="${doc}" prefix="${doc}">
	        	<include name="**/*.txt"/>
	        </zipfileset>
	        <zipfileset dir="${web}" prefix="${web}">
	        	<include name="**/*.html"/>
	        	<include name="**/*.gif"/>
	        	<include name="**/*.png"/>
	        </zipfileset>
	    </zip>
	</target>
	
	<target name="javadoc" depends="init" description="Build javadoc">
		<javadoc destdir="${javadoc}" author="true" windowtitle="AppleCommander ${acVersion} JavaDoc"
				classpath="${swtjar};${junitjar}">
			<fileset dir="${source}">
				<exclude name="**/*gif"/>
				<exclude name="**/*dump"/>
				<exclude name="**/*properties"/>
			</fileset>
			<fileset dir="${testsrc}">
				<exclude name="**/*properties"/>
			</fileset>
		</javadoc>
    </target>
	
	<target name="websiteZip" depends="javadoc" description="Build website ZIP archive">
	    <zip zipfile="${webzip}">
	        <zipfileset dir="${web}">
	        	<include name="**/*.html"/>
	        	<include name="**/*.gif"/>
	        	<include name="**/*.png"/>
	        </zipfileset>
	        <zipfileset dir="${javadoc}" prefix="javadoc"/>
	    </zip>
	</target>
</project>
