<project name="maven" basedir="/dev/null">
	<property name="lwjgl.maven"			location="maven" />
	<property name="lwjgl.dstMaven"			location="${lwjgl.target.staging}/maven" />
	<property name="lwjgl.src.java" 		location="${lwjgl.src}/java" />
	<property name="jinput.version"			value="2.0.5" />

	<fileset id="lwjgl-sources.manual.fileset" dir="${lwjgl.src.java}">
		<patternset refid="lwjgl.package.pattern" />
		<patternset refid="lwjgl.package.dependencies.pattern"/>
	</fileset>

	<fileset id="lwjgl-sources.generated.fileset" dir="${lwjgl.target.gen.java}">
		<include name="**/*" />
	</fileset>

	<fileset id="lwjgl_util-sources.fileset" dir="${lwjgl.src.java}">
		<patternset refid="lwjgl_util.package.pattern" />
	</fileset>

	 <target name="-fixmavenversion">
		<script language="javascript">
			<![CDATA[
				importPackage(java.lang);
				var originalVersion = project.getProperty("lwjgl.version");
				System.out.println("LWJGL Version: " + originalVersion);
				var mavenVersion = originalVersion;
				if(originalVersion.match(/^[0-9]+\.[0-9]+$/)){
					System.out.println("Fixing LWJGL Maven version (Maven version should be x.y.z)");
					mavenVersion = originalVersion + ".0";
				}
				if(!mavenVersion.match(/^.*-SNAPSHOT$/)){
					var forceSnapshot = project.getProperty("snapshot")
					if(forceSnapshot!=null && forceSnapshot!="false"){
						System.out.println("Forcing Maven Version to Snapshot");
						mavenVersion = mavenVersion + "-SNAPSHOT";
					}
			    }
			
				project.setNewProperty("lwjgl-maven-version",mavenVersion);
				System.out.println("LWJGL Maven Version: " + project.getProperty("lwjgl-maven-version"));
			]]>
		</script>
	</target>
	
	<target name="-checkjinputversion">
			<script language="javascript" classpath="${lwjgl.lib}/jinput.jar">
			<![CDATA[
			importPackage(java.lang);
			var version =  net.java.games.input.Version.getVersion()
			project.setNewProperty("jinputversion",version);
			var declaredJinputVersion = project.getProperty("jinput.version");
			System.out.println("JINPUT Version: " + version + " - DeclaredVersion: " + declaredJinputVersion);
				
			if(declaredJinputVersion.equals(version)){
				System.out.println("JINPUT Version Matches");
				project.setNewProperty("jinputversionmatches", true);
			} else {
				System.out.println("JINPUT Version don't match");
			}
			]]>
			</script>
			<condition property="failjinputcheck">
				<and>
					<not><isset property="jinputversionmatches" /></not>
					<not><isset property="overridejinput" /></not>
				</and>
			</condition>		
			<fail  if="failjinputcheck" message="Jinput version in project (${jinputversion}) is different from the declared jinput version for maven (${jinput.version}) add -Doverridejinput=true as a command line option to avoid this check" />	
	</target>

	<target name="maven-full">
		<antcall target="clean-java" />
		<antcall target="-initialize" />
		
		<antcall target="generate-all" />
		<antcall target="compile" />
        <antcall target="-createjars" />
        <antcall target="maven"/>
	</target>
	
	<target name="maven" depends="-fixmavenversion, -checkjinputversion"> <!-- Added as dependency because using antcall creates a new project scope -->
		<delete dir="${lwjgl.dstMaven}" quiet="true" failonerror="false" taskname="cleaning maven dist" />
		<mkdir dir="${lwjgl.dstMaven}"   taskname="initialiazing temp maven folder" />
		<antcall target="-copylwjgljars" />
		<antcall target="-createmavensourcejars" />
		<antcall target="-createmavenjavadocs" />
		<antcall target="-createmavennativejars" />
		<antcall target="-copymavenpoms"/>
		<antcall target="-copymavendeploybuild"/>
		<antcall target="-copymaventdist"/>
	</target>
	
	
		
	
	
	


	<target name="-copylwjgljars">
		<copy todir="${lwjgl.dstMaven}">
			<fileset dir="${lwjgl.target.staging}/jar/">
				<patternset>
					<include name="lwjgl.jar" />
					<include name="lwjgl_util.jar" />
				</patternset>
			</fileset>
		</copy>
	</target>

	<!-- Packages the java files -->
	<target name="-createmavensourcejars">

		<jar destfile="${lwjgl.dstMaven}/lwjgl-sources.jar" taskname="lwjgl-sources.jar">
			<fileset refid="lwjgl-sources.manual.fileset" />
			<fileset refid="lwjgl-sources.generated.fileset" />
		</jar>
	
		<jar destfile="${lwjgl.dstMaven}/lwjgl_util-sources.jar" taskname="lwjgl_util-sources.jar">
			<fileset refid="lwjgl_util-sources.fileset" />
		</jar>
	</target>
	
	<target name="-createmavenjavadocs">
	<!-- Creates the Javadoc -->
	
		<javadoc destdir="${lwjgl.dstMaven}/lwjgl-javadoc" classpath="${lwjgl.lib}/jinput.jar" author="true" version="true" use="true" source="1.8" windowtitle="LWJGL API" useexternalfile="true">
			<fileset refid="lwjgl-sources.manual.fileset"/>
			<fileset refid="lwjgl-sources.generated.fileset"/>
			<doctitle><![CDATA[<h1>Lightweight Java Game Toolkit</h1>]]></doctitle>
			<bottom><![CDATA[<i>Copyright &#169; 2002-2010 lwjgl.org. All Rights Reserved.</i>]]></bottom>
		</javadoc>
		<jar destfile="${lwjgl.dstMaven}/lwjgl-javadoc.jar" taskname="lwjgl-javadoc.jar">
			<fileset dir="${lwjgl.dstMaven}/lwjgl-javadoc" />
		</jar>
		
		<javadoc destdir="${lwjgl.dstMaven}/lwjgl_util-javadoc" classpath="${lwjgl.lib}/jinput.jar:${lwjgl.lib}/lwjgl.jar" author="true" version="true" use="true" source="1.8" windowtitle="LWJGL UTIL API" useexternalfile="true">
			<fileset refid="lwjgl_util-sources.fileset"/>
			<doctitle><![CDATA[<h1>Lightweight Java Game Toolkit</h1>]]></doctitle>
			<bottom><![CDATA[<i>Copyright &#169; 2002-2010 lwjgl.org. All Rights Reserved.</i>]]></bottom>
		</javadoc>
		<jar destfile="${lwjgl.dstMaven}/lwjgl_util-javadoc.jar" taskname="lwjgl_util-javadoc.jar">
			<fileset dir="${lwjgl.dstMaven}/lwjgl_util-javadoc" />
		</jar>
		
		<delete dir="${lwjgl.dstMaven}/lwjgl-javadoc" quiet="true" failonerror="false" taskname="cleaning maven javadoc temps lwjgl" />
		<delete dir="${lwjgl.dstMaven}/lwjgl_util-javadoc" quiet="true" failonerror="false" taskname="cleaning maven javadoc temps lwjgl_util" />
	</target>
	
	<target name="-createmavennativejars">
		<jar destfile="${lwjgl.dstMaven}/lwjgl-platform-natives-windows.jar" taskname="lwjgl-platform-natives-windows.jar">
			<fileset dir="${lwjgl.lib}/windows">
				<patternset refid="lwjgl-windows-lwjgl.fileset"/>
			</fileset>
		</jar>
		<jar destfile="${lwjgl.dstMaven}/lwjgl-platform-natives-linux.jar" taskname="lwjgl-platform-natives-linux.jar">
			<fileset dir="${lwjgl.lib}/linux">
				<patternset refid="lwjgl-linux-lwjgl.fileset"/>
			</fileset>
		</jar>
		<jar destfile="${lwjgl.dstMaven}/lwjgl-platform-natives-osx.jar" taskname="lwjgl-platform-natives-osx.jar">
			<fileset dir="${lwjgl.lib}/macosx">
				<patternset refid="lwjgl-macosx-lwjgl.fileset"/>
			</fileset>
		</jar>
	</target>

	<target name="-copymavenpoms">
		<copy todir="${lwjgl.dstMaven}">
			<fileset dir="${lwjgl.maven}">
				<include name="*.pom" />
			</fileset>
			<filterset>
      			<filter token="VERSION" value="${lwjgl-maven-version}"/>
				<filter token="JINPUTVERSION" value="${jinput.version}"/>
    		</filterset>	
		</copy>
	</target>
	
	<target name="-copymavendeploybuild">
		<copy todir="${lwjgl.dstMaven}">
			<fileset dir="${lwjgl.maven}">
				<include name="build.xml" />
			</fileset>
		</copy>
	</target>
	
	<target name="-copymaventdist">
		<zip destfile="${lwjgl.target.bundle}/lwjgl-maven-${lwjgl.version}.zip" basedir="${lwjgl.target.staging}" includes="maven/**" />
	</target>
</project>
