package org.magetech.paq.installer;

import org.magetech.paq.Launch;
import org.magetech.paq.NetUtils;
import org.magetech.paq.StreamUtils;
import org.magetech.paq.UrlUtils;
import org.magetech.paq.configuration.Property;
import org.magetech.paq.configuration.PropertyLoader;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;

/**
 * Created by Aleksander on 13.12.13.
 */
public class ForgeInstaller {
    public static String install(String forgeUrl, boolean isServer) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        String forgeFileName = UrlUtils.fileName(forgeUrl);

        String forgeVersionId = null;
        if(!isServer) {
            forgeVersionId = Minecraft.findForgeProfile();
            if(forgeVersionId != null) {
                // sample 1.6.4-Forge9.11.1.953
                String forgeVersion = forgeVersionId.replace("-Forge", "-");
                if(forgeFileName.contains(forgeVersion))
                    return forgeVersionId;
            }
        }

        File tmp = File.createTempFile("forge", ".jar");
        tmp.deleteOnExit();
        try(InputStream is = new URL(forgeUrl).openStream()) {
            StreamUtils.saveTo(is, tmp);
        }

        String[] args;
        if(isServer)
            args = new String[] { "installServer" };
        else
            args = new String[0];

        Launch.jar(tmp, args);
        if(!isServer) {
            forgeVersionId = Minecraft.findForgeProfile();
        }

        return forgeVersionId;
    }
}

@@ -266,24 +266,55 @@
 rtyLoader;%0A%0A
+import java.io.BufferedReader;%0A
 import java.
@@ -378,16 +378,50 @@
 Stream;%0A
+import java.io.InputStreamReader;%0A
 import j
@@ -1575,16 +1575,18 @@
         
+//
 Launch.j
@@ -1600,16 +1600,378 @@
  args);%0A
+        %0A        Process p = Runtime.getRuntime().exec(%22java -jar %22 + %22%5C%22%22 + tmp.getAbsolutePath() + %22%5C%22 %22);%0A%09%09BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));%0A%09%09%0A%09%09String line=null;%0A%09%09%0A%09%09while((line=input.readLine()) !=null )%7B%0A%09%09%09System.out.println(line);%0A%09%09%7D%0A%09%0A%09p.waitFor();%0A%09if (p.exitValue() != 0) %7B%0A%09%7D%0A        %0A        %0A
         

Take 2