<project name="DimensionalDoors" default="install" basedir=".">
	<property environment="env" />

	<property name="project.name" value="DimDoors" />
	<property name="src.dir" value="StevenDimDoors" />
	<property name="resources.dir" value="resources" />
	<property name="schematics.dir" value="schematics" />
	<property name="versionclass.dir" value="mod_pocketDim" />
	<property name="versionclass.file" value="mod_pocketDim.java"/>

	<property name="minecraft.version" value="1.6.4" />
	<property name="forge.forgeversion" value="9.11.1" />
	<property name="forge.buildnum" value="953" />

	<property name="build.dir" value="build" />
	<property name="classes.dir" value="${build.dir}/packaging" />
	<property name="resourcePack.dir" value="${classes.dir}" />
	<property name="schematicPack.dir" value="${classes.dir}/schematics/" />
	<property name="apiclasses.dir" value="${build.dir}/api-packaging" />
	<property name="forge.version" value="${minecraft.version}-${forge.forgeversion}.${forge.buildnum}" />
	<property name="forge.url" value="http://files.minecraftforge.net/minecraftforge/minecraftforge-src-${forge.version}.zip" />
	<property name="forge.dir" value="${build.dir}/forge" />
	<property name="mcp.dir" value="${forge.dir}/mcp" />
	<property name="mcpsrc.dir" value="${mcp.dir}/src/minecraft/StevenDimDoors" />

	<property name="package.meta-inf" value="META-INF" />
	<property name="dist.dir" value="${build.dir}/dist" />

	<property name="download.dir" value="${build.dir}/downloads" />

	<property name="lib.dir" value="${mcp.dir}/lib" />

	<property file="${forge.dir}/forgeversion.properties" />
	<condition property="forge.already.installed">
		<equals arg1="${forge.build.number}" arg2="${forge.buildnum}" />
	</condition>

	<mkdir dir="${download.dir}"/>
	<mkdir dir="${build.dir}" />
	
	<target name="get-version" depends="setup-forge">
		<!-- Copy source -->
		<copy todir="${mcpsrc.dir}" overwrite="true">
			<fileset dir="${src.dir}"/>
		</copy>

		<!-- Get the version from the mcmod.info -->
		<script language="javascript">
		    importClass(java.io.File);
		    importClass(java.nio.file.Files)
		    importClass(java.io.FileReader);
		    importClass(java.io.BufferedReader);
		    importClass(java.io.FileWriter);
		    importClass(java.io.BufferedWriter);

			echo = project.createTask("echo");
			echo.setMessage("Parsing mcmod.info...");
			echo.perform();
			
			var file = new File("./mcmod.info");

			fr = new FileReader(file);
			br = new BufferedReader(fr);

			// Read the file.
			// This assumes the file has no line
			var line;
			var json = "";
			while ((line = br.readLine()) != null) {
				json += line;
			}

			var struct = JSON.parse(json);
			var version = struct["modlist"][0].version;

			echo = project.createTask("echo");
			echo.setMessage("Parsed version: " + version);
			echo.perform();

			project.setProperty("mod.version", version);
		</script>

		<!-- Replace the version information in the mod class -->
		<replace file="${mcpsrc.dir}/${versionclass.dir}/${versionclass.file}">
		    <replacefilter token="$VERSION$" value="${mod.version}"/>
		</replace>
	</target>

	<available property="forge-exists" file="${download.dir}/minecraftforge-src-${forge.version}.zip" />
	<available property="already-compiled" file="${classes.dir}/${src.dir}" />
	<condition property="should-download-ant-contrib">
		<or>
			<available file="${download.dir}/ant-contrib/ant-contrib-1.0b3.jar"/>
			<available file="${download.dir}/minecraftforge-src-${forge.version}.zip"/>
		</or>
	</condition>

	<target name="install" depends="build">
		<delete dir="${mcpsrc.dir}/${src.dir}" />
		<delete dir="${dist.dir}" />
		<jar destfile="${dist.dir}/${project.name}-${mod.version}-${build.number}.jar" basedir="${classes.dir}"/>
		<delete dir="${classes.dir}" />
		<delete dir="${mcp.dir}/reobf"/>
		<delete dir="{$apiclasses.dir}" />
		<delete dir="${mcpsrc.dir}/${src.dir}" />
	</target>

	<target name="build" depends="get-version" unless="already-compiled">
		<!-- Recompile -->
		<exec dir="${mcp.dir}" executable="cmd" osfamily="windows" failonerror="true">
			<arg line="/c recompile.bat"/>
		</exec>

		<exec dir="${mcp.dir}" executable="sh" osfamily="unix" failonerror="true">
			<arg value="recompile.sh" />
		</exec>

		<!-- Reobf -->
		<exec dir="${mcp.dir}" executable="cmd" osfamily="windows">
			<arg line="/c reobfuscate.bat --srgnames"/>
		</exec>

		<exec dir="${mcp.dir}" executable="sh" osfamily="unix">
			<arg value="reobfuscate.sh" />
		</exec>

		<copy todir="${classes.dir}">
			<fileset dir="${mcp.dir}/reobf/minecraft"/>
		</copy>

		<copy todir="${resourcePack.dir}">
			<fileset dir="${resources.dir}" />
		</copy>
		
		<copy todir="${schematicPack.dir}">
			<fileset dir="${schematics.dir}" />
		</copy>
		
		<copy todir="${classes.dir}">
			<fileset file="mcmod.info" />
		</copy>
		
		<copy todir="${classes.dir}">
			<fileset file="${resources.dir}/pack.mcmeta" />
		</copy>
		
		<delete file="${resourcePack.dir}/mcmod.info" />
		
		<delete file="${resourcePack.dir}/pack.mcmeta" />
	</target>

	<target name="build-number-there" if="env.BUILD_NUMBER" >
		<echo message="Using build number ${env.BUILD_NUMBER}..." />
		<property name="build.number" value="${env.BUILD_NUMBER}" />
	</target>

	<target name="build-number-not-there" unless="env.BUILD_NUMBER" >
		<echo message="!! No build number set !!" />
		<property name="build.number" value="0" />
	</target>

	<target name="setup-forge" depends="download-forge,build-number-there,build-number-not-there" unless="forge.already.installed">

		<property name="build.full" value="${build.number}" />
		<unzip dest="${build.dir}">
			<fileset dir="${download.dir}">
				<include name="minecraftforge-src-${forge.version}.zip"/>
			</fileset>
		</unzip>

		<!-- Change executables' permitions -->
		<chmod file="${forge.dir}/install.sh" perm="+x"/>

		<!-- Install forge -->
		<delete dir="${mcp.dir}" failonerror="no"/>
		<exec dir="${forge.dir}" executable="cmd" osfamily="windows" inputstring="Yes\n">
			<arg line="/c install.cmd"/>
		</exec>

		<exec dir="${forge.dir}" executable="sh" osfamily="unix" inputstring="Yes\n">
			<arg value="install.sh" />
		</exec>

		<chmod file="${mcp.dir}/updatemd5.sh" perm="+x"/>
		<chmod file="${mcp.dir}/recompile.sh" perm="+x"/>
		<chmod file="${mcp.dir}/reobfuscate.sh" perm="+x"/>
		<chmod file="${mcp.dir}/runtime/bin/astyle-osx" perm="+x" />
		<chmod file="${mcp.dir}/runtime/bin/jad-osx" perm="+x" />
	</target>

	<target name="download-forge" depends="download-ant-contrib" unless="forge-exists">
		<taskdef resource="net/sf/antcontrib/antlib.xml">
			<classpath>
				<pathelement location="${download.dir}/ant-contrib/ant-contrib-1.0b3.jar"/>
				<fileset dir="${download.dir}/ant-contrib/lib">
					<include name="*.jar"/>
				</fileset>
			</classpath>
		</taskdef>
		<echo message="Downloading forge... " />
		<get src="http://files.minecraftforge.net/minecraftforge-src-${forge.version}.zip"
							dest="${download.dir}/minecraftforge-src-${forge.version}.zip" />
	</target>

	<target name="download-ant-contrib" unless="should-download-ant-contrib">
		<echo message="Getting: ant-contrib"/>
		<mkdir dir="${download.dir}/tmp"/>
		<mkdir dir="${download.dir}/ant-contrib/lib" />
		<get src="http://sourceforge.net/projects/ant-contrib/files/ant-contrib/1.0b3/ant-contrib-1.0b3-bin.zip/download" dest="${download.dir}/tmp/ant-contrib-1.0b3-bin.zip"/>
		<get src="http://archive.apache.org/dist/commons/codec/binaries/commons-codec-1.6-bin.zip" dest="${download.dir}/tmp/commons-codec-1.6-bin.zip"/>

		<unzip src="${download.dir}/tmp/ant-contrib-1.0b3-bin.zip" dest="${download.dir}"/>
		<unzip src="${download.dir}/tmp/commons-codec-1.6-bin.zip" dest="${download.dir}/tmp"/>

		<move todir="${download.dir}/ant-contrib/lib">
			<fileset file="${download.dir}/tmp/commons-codec-1.6/commons-codec-1.6.jar"/>
		</move>

		<!-- Delete useless files -->
		<delete dir="${download.dir}/ant-contrib/docs"/>
		<delete dir="${download.dir}/tmp"/>

	</target>

	<target name="clean" >
		<delete dir="${build.dir}" />
	</target>


</project>