<?xml version="1.0"?>

<project name="Sun Games Initiative Client Technologies" basedir="." default="all" xmlns:artifact="urn:maven-artifact-ant">
    <target name="init">
        <!-- If we are running in windows, set dx8 property to true -->
        <condition property="windows" >
            <os family="windows" />
        </condition>
        
        <!-- If we are running in linux, set linux property to true -->
        <condition property="linux" >
            <!--<os family="unix" />-->
            <os name="linux" />
        </condition>
		
		<!-- If we are running in Mac OS X, set osx property to true -->
		<condition property="osx" >
			<and>
				<os family="mac" />
				<os family="unix" />
			</and>
		</condition>
    </target>

	<target name="runtest" depends="dist">
		<java classname="${mainclass}" 
              fork="true" failonerror="true" dir=".">
            <classpath>
				<pathelement location="dist/jinput.jar"/>
				<pathelement location="dist/jinput-test.jar"/>
            </classpath>
			<jvmarg value="-Djava.library.path=dist"/>
		</java>
	</target>

	<target name="texttest">
		<antcall target="runtest">
			<param name="mainclass" value="net.java.games.input.test.ControllerTextTest"/>
		</antcall>
	</target>

    <target name="readtest" depends="init,all" description="Try running it.">
		<antcall target="runtest">
			<param name="mainclass" value="net.java.games.input.test.ControllerReadTest"/>
		</antcall>
	</target>

    <target name="eventtest" depends="init,all" description="Try running it.">
		<antcall target="runtest">
			<param name="mainclass" value="net.java.games.input.test.ControllerEventTest"/>
		</antcall>
	</target>

    <target name="rumbletest" depends="init,all" description="Try running it.">
		<antcall target="runtest">
			<param name="mainclass" value="net.java.games.input.test.RumbleTest"/>
		</antcall>
	</target>

    <target name="versiontest" depends="init,all" description="Try running it.">
		<antcall target="runtest">
			<param name="mainclass" value="net.java.games.input.test.VersionTest"/>
		</antcall>
	</target>

	<macrodef name="iterate">
		<attribute name="target"/>
		<sequential>
			<subant target="@{target}">
				<fileset file="coreAPI/build.xml"/>
				<fileset file="plugins/linux/build.xml"/>
				<fileset file="plugins/windows/build.xml"/>
				<fileset file="plugins/wintab/build.xml"/>
				<fileset file="plugins/OSX/build.xml"/>
				<fileset file="plugins/awt/build.xml"/>
			</subant>
		</sequential>
	</macrodef>

	<target name="clean">
		<iterate target="clean"/>
		<delete dir="dist" failonerror="no"/>
	</target>

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

	<target name="javadoc" depends="init">
		<iterate target="javadoc"/>
	</target>

	<target name="pluginTest" depends="dist" description="Build plugin version">
		<mkdir dir="dist"/>
		<mkdir dir="dist/controller"/>
		<copy todir="dist">
			<fileset file="coreAPI/bin/jinput-core.jar"/>
			<fileset file="coreAPI/bin/jinput-test.jar"/>
			<fileset file="plugins/DX8/lib/jutils.jar"/>
		</copy>
		<copy todir="dist/controller">
			<fileset dir="plugins/awt/bin"/>
			<fileset dir="plugins/linux/bin"/>
			<fileset dir="plugins/OSX/bin"/>
			<fileset dir="plugins/windows/bin"/>
			<fileset dir="plugins/wintab/bin"/>
		</copy>
	</target>
	
    <target name="dist" depends="init" description="Build the distribution file for this system">
		<iterate target="all"/>
        <mkdir dir="dist"/>
		<jar jarfile="dist/jinput.jar" compress="true" duplicate="preserve">
			<fileset dir="coreAPI/classes">
				<include name="**/*.class"/>
				<exclude name="**/test/*.class"/>
				<exclude name="**/example/*.class"/>
			</fileset>
			<fileset dir="plugins/linux/classes">
				<include name="**/*.class"/>
			</fileset>
			<fileset dir="plugins/OSX/classes">
				<include name="**/*.class"/>
			</fileset>
			<fileset dir="plugins/awt/classes">
				<include name="**/*.class"/>
			</fileset>
			<fileset dir="plugins/windows/classes">
				<include name="**/*.class"/>
			</fileset>
			<fileset dir="plugins/wintab/classes">
				<include name="**/*.class"/>
			</fileset>
			<zipfileset src="lib/jutils.jar" includes="**/*.class"/>
			<manifest>
				<attribute name="Sealed" value="true"/>
			</manifest>
		</jar>
		<copy todir="dist">
			<fileset file="coreAPI/bin/jinput-test.jar"/>
			<fileset dir="plugins/linux/bin" includes="*so"/>
			<fileset dir="plugins/OSX/bin" includes="*jnilib"/>
			<fileset dir="plugins/windows/bin" includes="*dll"/>
			<fileset dir="plugins/wintab/bin" includes="*dll"/>
		</copy>
    </target>
	
	<target name="simple_dist" depends="dist,simple_windows,simple_wintab,simple_linux,simple_osx" description="Build the simple jars for each platform"/>
	
	<target name="simple_linux" depends="dist" description="Build the simple linux jar" if="linux">
		<jar jarfile="dist/jinput-linux.jar" compress="true">
			<fileset dir="coreAPI/classes">
				<include name="**/*.class"/>
			</fileset>
			<fileset dir="plugins/linux/classes">
				<include name="**/*.class"/>
			</fileset>
			<zipfileset src="lib/jutils.jar" includes="**/*.class"/>
			<manifest>
				<attribute name="Sealed" value="true"/>
			</manifest>
		</jar>
		<tar destfile="dist/jinput_linux.tgz" compression="gzip">
			<tarfileset dir=".">
				<include name="dist/jinput-linux.jar"/>
			</tarfileset>
			<tarfileset dir="plugins/linux/bin">
				<include name="*.so"/>
			</tarfileset>
		</tar>
	</target>

	<target name="simple_windows" depends="dist" description="Build the simple windows jar" if="windows">
		<jar jarfile="dist/jinput-windows.jar" compress="true">
			<fileset dir="coreAPI/classes">
				<include name="**/*.class"/>
			</fileset>
			<fileset dir="plugins/windows/classes">
				<include name="**/*.class"/>
			</fileset>
			<zipfileset src="lib/jutils.jar" includes="**/*.class"/>
			<manifest>
				<attribute name="Sealed" value="true"/>
			</manifest>
		</jar>
		<zip destfile="dist/jinput_windows.zip">
			<zipfileset dir=".">
				<include name="dist/jinput-windows.jar"/>
			</zipfileset>
			<zipfileset dir="plugins/windows/bin">
				<include name="*.dll"/>
			</zipfileset>
		</zip>
	</target>

	<target name="simple_wintab" depends="dist" description="Build the simple windows jar" if="windows">
		<jar jarfile="dist/jinput-wintab.jar" compress="true">
			<fileset dir="coreAPI/classes">
				<include name="**/*.class"/>
			</fileset>
			<fileset dir="plugins/wintab/classes">
				<include name="**/*.class"/>
			</fileset>
			<zipfileset src="lib/jutils.jar" includes="**/*.class"/>
			<manifest>
				<attribute name="Sealed" value="true"/>
			</manifest>
		</jar>
		<zip destfile="dist/jinput_wintab.zip">
			<zipfileset dir=".">
				<include name="dist/jinput-wintab.jar"/>
			</zipfileset>
			<zipfileset dir="plugins/wintab/bin">
				<include name="*.dll"/>
			</zipfileset>
		</zip>
	</target>

	<target name="simple_osx" depends="dist" description="Build the simple osx jar" if="osx">
		<jar jarfile="dist/jinput-osx.jar" compress="true">
			<fileset dir="coreAPI/classes">
				<include name="**/*.class"/>
			</fileset>
			<fileset dir="plugins/OSX/classes">
				<include name="**/*.class"/>
			</fileset>
			<zipfileset src="lib/jutils.jar" includes="**/*.class"/>
			<manifest>
				<attribute name="Sealed" value="true"/>
			</manifest>
		</jar>
		<tar destfile="dist/jinput_osx.tgz" compression="gzip">
			<tarfileset dir=".">
				<include name="dist/jinput-osx.jar"/>
			</tarfileset>
			<tarfileset dir="plugins/OSX/bin">
				<include name="*.jnilib"/>
			</tarfileset>
		</tar>
	</target>
	
	<target name="install" description="Installs jinput in to local mvn repo">
		<path id="maven-ant-tasks.classpath" path="lib/maven-ant-tasks-2.1.0.jar" />
	    <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant"
	             classpathref="maven-ant-tasks.classpath"/>
		
		<artifact:pom id="jinputpom" groupId="net.java.games" artifactId="jinput" version="2.0.2-SNAPSHOT">
		</artifact:pom>
		<artifact:writepom pomRefId="jinputpom" file="dist/pom.xml"/>
		
		<artifact:pom id="jinputpomfile" file="dist/pom.xml" />
		
		<delete file="dist/jinput-windows-natives.zip"/>
		<zip destfile="dist/jinput-windows-natives.zip">
			<fileset dir="dist/">
				<include name="**/*.dll"/>
			</fileset>
		</zip>
		
		<delete file="dist/jinput-linux-natives.zip"/>
		<zip destfile="dist/jinput-linux-natives.zip">
			<fileset dir="dist/">
				<include name="**/*.so"/>
			</fileset>
		</zip>
		
		<delete file="dist/jinput-osx-natives.jar"/>
		<zip destfile="dist/jinput-osx-natives.zip" >
			<fileset dir="dist/">
				<include name="**/*.jnilib"/>
			</fileset>
		</zip>
		
		<artifact:install file="dist/jinput.jar">
			<pom refid="jinputpomfile" />
			<attach file="dist/jinput-windows-natives.zip" classifier="windows-natives" type="zip"/>
			<attach file="dist/jinput-linux-natives.zip" classifier="linux-natives" type="zip"/>
			<attach file="dist/jinput-osx-natives.zip" classifier="osx-natives" type="zip"/>
		</artifact:install>
	</target>
	
	<target name="mvndeploy" depends="install" description="Installs jinput in to local mvn repo">
		<artifact:install-provider artifactId="wagon-ssh" version="1.0-beta-2"/>
		
		<artifact:pom id="jinputpom" groupId="net.java.games" artifactId="jinput" version="2.0.1">
		</artifact:pom>
		<artifact:writepom pomRefId="jinputpom" file="dist/pom.xml"/>
		
		<artifact:pom id="jinputpomfile" file="dist/pom.xml" />
		
		<artifact:deploy file="dist/jinput.jar">
		    <remoteRepository url="${repository.url}">
				<authentication username="${repository.username}" password="${repository.password}"/>
		    </remoteRepository>
			<pom refid="jinputpomfile" />
			<attach file="dist/jinput-windows-natives.zip" classifier="windows-natives" type="zip"/>
			<attach file="dist/jinput-linux-natives.zip" classifier="linux-natives" type="zip"/>
			<attach file="dist/jinput-osx-natives.zip" classifier="osx-natives" type="zip"/>
		</artifact:deploy>
	</target>
</project>
