package net.minecraftforge.gradle.tasks.dev;

import groovy.lang.Closure;

import java.io.File;
import java.io.IOException;

import net.minecraftforge.gradle.delayed.DelayedFile;
import net.minecraftforge.gradle.delayed.DelayedString;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.OutputFile;
import org.gradle.api.tasks.TaskAction;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class FMLVersionPropTask extends DefaultTask
{
    @OutputFile
    DelayedFile outputFile;
    
    private Closure<String> version;

    @TaskAction
    public void doTask() throws IOException
    {
        String[] v;
        if (this.version == null)
            v = ((String)getProject().getVersion()).split("-")[1].split("\\.");
        else
            v = this.version.call().split("-")[1].split("\\.");
        String data =
        "fmlbuild.major.number="    + v[0] + "\n" +
        "fmlbuild.minor.number="    + v[1] + "\n" +
        "fmlbuild.revision.number=" + v[2] + "\n" +
        "fmlbuild.build.number="    + v[3] + "\n" +
        "fmlbuild.mcversion=" + new DelayedString(getProject(), "{MC_VERSION}").call() + "\n" +
        "fmlbuild.mcpversion=" + new DelayedString(getProject(), "{MCP_VERSION}").call() + "\n";
        //fmlbuild.deobfuscation.hash -- Not actually used anywhere
        Files.write(data.getBytes(Charsets.UTF_8), getOutputFile());
    }

    public void setOutputFile(DelayedFile output)
    {
        this.outputFile = output;
    }

    public File getOutputFile()
    {
        return outputFile.call();
    }

    public void setVersion(Closure<String> value)
    {
        this.version = value;
    }
}

@@ -646,12 +646,20 @@
 ring
-%5B%5D v
+ fullVersion
 ;%0A  
@@ -706,13 +706,22 @@
     
-v
+fullVersion
  = 
-(
 (Str
@@ -753,36 +753,8 @@
 on()
-).split(%22-%22)%5B1%5D.split(%22%5C%5C.%22)
 ;%0A  
@@ -776,17 +776,27 @@
         
-v
+fullVersion
  = this.
@@ -809,16 +809,198 @@
 n.call()
+;%0A%0A        String mcVersion = new DelayedString(getProject(), %22%7BMC_VERSION%7D%22).call();%0A        fullVersion = fullVersion.substring(mcVersion.length());%0A        String%5B%5D v =fullVersion
 .split(%22

Fix dashes problem in version