/*
 * Copyright 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.process.internal;

import org.gradle.internal.Factory;
import org.gradle.internal.file.PathToFileResolver;
import org.gradle.internal.jvm.Jvm;
import org.gradle.process.ProcessForkOptions;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class DefaultProcessForkOptions implements ProcessForkOptions {
    private final PathToFileResolver resolver;
    private Object executable;
    private Factory<File> workingDir;
    private final Map<String, Object> environment = new HashMap<String, Object>(Jvm.current().getInheritableEnvironmentVariables(System.getenv()));

    public DefaultProcessForkOptions(PathToFileResolver resolver) {
        this.resolver = resolver;
        workingDir = resolver.resolveLater(".");
    }

    public String getExecutable() {
        return executable == null ? null : executable.toString();
    }

    public void setExecutable(Object executable) {
        this.executable = executable;
    }

    public ProcessForkOptions executable(Object executable) {
        setExecutable(executable);
        return this;
    }

    public File getWorkingDir() {
        return workingDir.create();
    }

    public void setWorkingDir(Object dir) {
        this.workingDir = resolver.resolveLater(dir);
    }

    public ProcessForkOptions workingDir(Object dir) {
        setWorkingDir(dir);
        return this;
    }

    public Map<String, Object> getEnvironment() {
        return environment;
    }

    public Map<String, String> getActualEnvironment() {
        Map<String, String> actual = new HashMap<String, String>();
        for (Map.Entry<String, Object> entry : environment.entrySet()) {
            actual.put(entry.getKey(), String.valueOf(entry.getValue()));
        }
        return actual;
    }

    public void setEnvironment(Map<String, ?> environmentVariables) {
        environment.clear();
        environment.putAll(environmentVariables);
    }

    public ProcessForkOptions environment(String name, Object value) {
        environment.put(name, value);
        return this;
    }

    public ProcessForkOptions environment(Map<String, ?> environmentVariables) {
        environment.putAll(environmentVariables);
        return this;
    }

    public ProcessForkOptions copyTo(ProcessForkOptions target) {
        target.setExecutable(executable);
        target.setWorkingDir(workingDir);
        target.setEnvironment(environment);
        return this;
    }
}

@@ -647,16 +647,55 @@
 ernal;%0A%0A
+import com.google.common.collect.Maps;%0A
 import o
@@ -722,16 +722,16 @@
 actory;%0A
-
 import o
@@ -1126,22 +1126,16 @@
 private 
-final 
 Map%3CStri
@@ -1161,105 +1161,8 @@
 ment
- = new HashMap%3CString, Object%3E(Jvm.current().getInheritableEnvironmentVariables(System.getenv()))
 ;%0A%0A 
@@ -1983,32 +1983,172 @@
 Environment() %7B%0A
+        if (environment == null) %7B%0A            setEnvironment(Jvm.current().getInheritableEnvironmentVariables(System.getenv()));%0A        %7D%0A
         return e
@@ -2337,17 +2337,20 @@
 entry : 
-e
+getE
 nvironme
@@ -2351,16 +2351,18 @@
 ironment
+()
 .entrySe
@@ -2575,14 +2575,26 @@
 ment
-.clear
+ = Maps.newHashMap
 ();%0A
@@ -2721,33 +2721,36 @@
 alue) %7B%0A        
-e
+getE
 nvironment.put(n
@@ -2739,24 +2739,26 @@
 tEnvironment
+()
 .put(name, v
@@ -2873,33 +2873,36 @@
 bles) %7B%0A        
-e
+getE
 nvironment.putAl
@@ -2887,32 +2887,34 @@
   getEnvironment
+()
 .putAll(environm
@@ -3102,24 +3102,63 @@
 orkingDir);%0A
+        if (environment != null) %7B%0A    
         targ
@@ -3189,16 +3189,26 @@
 nment);%0A
+        %7D%0A
         

Copy environment variables lazily