<?xml version="1.0" encoding="utf-8"?>
<project name="Risk" default="distribution" basedir=".">
	<!--
		Risk ant build file
		http://jrisk.sourceforge.net
		@author Christian Weiske <cweiske@cweiske.de>
	-->
	<description>
		This ant build file contains all the necessary information
		to compile the Risk classes, manual files and to generate
		distribution files
	</description>

	<property name="dir.build" location="build"/>
	<property name="dir.build.classes" location="${dir.build}/classes"/>
	<property name="dir.build.manual" location="${dir.build}/manual"/>
	<!-- all required dirs and files will be copied to there before packing the zip -->
	<property name="dir.build.game" location="${dir.build}/game"/>
	<property name="dir.oldmanual" location="${basedir}/help"/>
	<property name="dir.dist" location="dist"/>

	<property name="file.src" location="${dir.dist}/src.zip"/>

	<!-- if the izpack installer jar is available -->
	<property name="izpack.compiler" value="${basedir}/installer/izpack/standalone-compiler.jar"/>
	<available file="${izpack.compiler}" property="izpack.available"/>


	<xmlcatalog>
		<dtd
			publicId="-//OASIS//DTD DocBook XML V4.2//EN"
			location="${basedir}/newhelp/dtd/docbookx.dtd"/>
	</xmlcatalog>


	<target name="classes"
		description="compiles all the classes"
		depends="init">

		<mkdir dir="${dir.build.classes}"/>

		<!-- compile the classes -->
		<javac debug="on" srcdir="${basedir}" destdir="${dir.build.classes}" excludes="installer/**,risk/lobby/**" 
		source="1.4" target="1.4" />
		<!-- copy translation files -->
		<copy todir="${dir.build.classes}/risk/engine/translation">
			<fileset dir="risk/engine/translation" includes="*.properties" />
		</copy>
		<!-- copy images -->
		<copy todir="${dir.build.classes}/risk/ui/FlashGUI">
			<fileset dir="risk/ui/FlashGUI" includes="*.jpg"/>
			<fileset dir="risk/ui/FlashGUI" includes="*.gif"/>
		</copy>
		<copy todir="${dir.build.classes}/risk/ui/Increment1GUI">
			<!-- FIXME: is this needed? -->
			<fileset dir="risk/ui/Increment1GUI" includes="*.gif"/>
		</copy>
		<copy todir="${dir.build.classes}/risk/engine/guishared">
			<fileset dir="risk/engine/guishared" includes="*.gif"/>
			<fileset dir="risk/engine/guishared" includes="*.png"/>
		</copy>
		<copy todir="${dir.build.classes}/risk/ui/SimpleGUI">
			<fileset dir="risk/ui/SimpleGUI" includes="*.gif"/>
		</copy>
		<copy todir="${dir.build.classes}/risk/ui/SwingGUI">
			<fileset dir="risk/ui/SwingGUI" includes="*.gif"/>
			<fileset dir="risk/ui/SwingGUI" includes="*.jpg"/>
		</copy>
		<copy todir="${dir.build.classes}/risk/tools/translation">
			<fileset dir="risk/tools/translation" includes="*.gif"/>
			<fileset dir="risk/tools/translation" includes="*.png"/>
		</copy>
<!--
		<copy todir="${dir.build.classes}/risk/lobby/client">
			<fileset dir="risk/lobby/client" includes="*.gif"/>
			<fileset dir="risk/lobby/client" includes="*.jpg"/>
		</copy>
		<copy todir="${dir.build.classes}/risk/lobby/server">
			<fileset dir="risk/lobby/server" includes="*.gif"/>
			<fileset dir="risk/lobby/server" includes="*.jpg"/>
		</copy>
-->
	</target>



	<target name="manual"
		description="compiles the manual from docbook into html"
		depends="init" >

		<delete dir="${dir.build.manual}"/>
		<mkdir dir="${dir.build.manual}"/>
		<!-- compile the manual with xmlto -->
		<exec executable="xmlto" dir="newhelp">
			<arg value="-o"/>
			<arg value="${dir.build.manual}/"/>
			<arg value="xhtml"/>
			<arg value="manual.xml"/>
		</exec>

		<!-- compile the frame files -->
		<!-- the following doesn't really work
			as xalan wants
				a) to connect *always* to the net to get the DTDs
				b) has a problem with "Writing frame_index.html: Bad file descriptor Cause: java.io.IOException"
		<xslt basedir="newhelp" destdir="${dir.build.manual}"
			style="newhelp/xslt/frames.xsl" includes="manual.xml" />
		-->
		<exec executable="xsltproc" dir="newhelp">
			<arg value="-o"/>
			<arg value="${dir.build.manual}/"/>
			<arg value="xslt/frames.xsl"/>
			<arg value="manual.xml"/>
		</exec>
	</target>



	<target name="distribution"
		description="generates distribution packages"
		depends="distribution.zip,distribution.izpack">
	</target>



	<target name="distribution.jar"
		description="creates the basic jar file with the classes"
		depends="classes">

		<jar destfile="${dir.dist}/Risk.jar"
			basedir="${dir.build.classes}"
			manifest="META-INF/MANIFEST.MF"/>
	</target>



	<target name="distribution.izpack"
		description="creates the java-based installer (.jar file)"
		depends="distribution.izpack.available,distribution.izpack.unavailable"
	>
		<!-- just a switch for available and unavailable -->
	</target>

	<target name="distribution.izpack.available"
		description="creates the java-based installer (.jar file)"
		if="izpack.available"
		>
<!--		depends="distribution.jar,game"-->


		<!-- Allows us to use the IzPack Ant task -->
		<taskdef name="izpack" classpath="${izpack.compiler}"
				classname="com.izforge.izpack.ant.IzPackTask"/>

		<!-- generate the installation package -->
		<izpack input="${basedir}/installer/izpack/install.xml"
			output="${dir.dist}/Risk-install.izpack.jar"
			installerType="standard"
			basedir="${dir.build.game}"/>
	</target>



	<target name="distribution.izpack.unavailable"
		description="Small notice"
		unless="izpack.available">
		<echo>
The izPack installer is not available on your system.
Get it from http://www.izforge.com/izpack/ if you want
to create the graphical installer.
Copy the standalone installer jar to
${basedir}/installer/izpack/standalone-compiler.jar
		</echo>
	</target>



	<target name="distribution.exe"
		description="Generates a Windows .exe file from the izPack installer"
		depends="distribution.izpack.available">

		<!-- load the ant task -->
		<taskdef name="jsmoothgen"
			classname="net.charabia.jsmoothgen.ant.JSmoothGen"
			classpath="${basedir}/installer/jsmooth/jsmoothgen-ant.jar"/>

		<!-- compile the exe file -->
<!--		Something doesn't work, and so I comment it out until I get it
			<jsmoothgen project="${basedir}/installer/jsmooth/Risk-installer.izpack.jsmooth"
			skeletonroot="${basedir}/installer/jsmooth/skeletons/"/>-->
	</target>




	<target name="game"
		description="copies all the necessary files for the game to one directory, from which it can be played then"
		depends="distribution.jar"><!-- ,manual -->

		<delete dir="${dir.build.game}"/>
		<mkdir dir="${dir.build.game}"/>

		<copy file="BugTrack.txt" todir="${dir.build.game}"/>
		<copy file="gpl.txt" todir="${dir.build.game}"/>
		<copy file="commands.txt" todir="${dir.build.game}"/>
		<copy file="ChangeLog.txt" todir="${dir.build.game}"/>
		<copy file="ReadMe.txt" todir="${dir.build.game}"/>
		<copy file="risk.ini" todir="${dir.build.game}"/>

<!--		<copy todir="${dir.build.game}/manual">
			<fileset dir="${dir.build.manual}"/>
		</copy>-->
		<copy todir="${dir.build.game}/help">
			<fileset dir="${dir.oldmanual}"/>
		</copy>

		<mkdir dir="${dir.build.game}/maps"/>
		<copy todir="${dir.build.game}/maps">
			<fileset dir="maps">
				<include name="*.*"/>
			</fileset>
		</copy>

		<mkdir dir="${dir.build.game}/maps/preview"/>
		<copy todir="${dir.build.game}/maps/preview">
			<fileset dir="maps/preview">
				<include name="*.jpg"/>
			</fileset>
		</copy>

		<mkdir dir="${dir.build.game}/resources"/>
		<copy todir="${dir.build.game}/resources">
			<fileset dir="${basedir}/resources" excludes="scripts_jar/*,scripts_jar,html_jar/*,html_jar,Risk_Language1.1.jar" ></fileset>
		</copy>

		<mkdir dir="${dir.build.game}/saves"/>

		<copy file="${dir.dist}/Risk.jar" todir="${dir.build.game}"/>
		<copy todir="${dir.build.game}">
			<!-- script start files for easy starting -->
			<fileset dir="${basedir}/resources/scripts_jar"/>
			<fileset dir="${basedir}/resources/html_jar"/>
		</copy>
		<!-- remember the installer chmod in installer/izpack/install.xml -->
		<chmod file="${dir.build.game}/*.sh" perm="ugo+x" />

	</target>



	<target name="distribution.source.zip"
		description="creates a zip with all the java files">

		<zip destfile="${file.src}">
			<fileset dir="${basedir}" includes="**/*.java,risk/engine/ChatServerLayout.gif"/>
		</zip>
	</target>



	<target name="distribution.zip"
		description="creates the normal zip distribution file which includes the jar"
		depends="distribution.jar,distribution.source.zip,game">

		<copy file="${file.src}" todir="${dir.build.game}"/>
		<mkdir dir="${dir.build.game}/saves"/> 
		<zip destfile="${dir.dist}/Risk.zip">
 			<zipfileset dir="${dir.build.game}" prefix="Risk" />
		</zip>
	</target>



	<target name="clean"
		description="Removes all the generated classes and files" >

		<delete dir="${dir.build}" includeemptydirs="true"/>
	</target>



	<target name="distclean"
		description="Removes the distribution directory" >

		<delete dir="${dir.dist}" includeemptydirs="true"/>
	</target>



	<target name="cleanall"
		description="Cleans all things generated by this build file"
		depends="clean,distclean" >

	</target>



	<target name="init"
		description="Generates all the necessary directories">

		<mkdir dir="${dir.dist}"/>
	</target>


</project>