/*
 * Copyright 2011 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.launcher.daemon.server.exec;

import org.gradle.launcher.daemon.protocol.Build;
import org.gradle.launcher.env.LenientEnvHacker;

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

/**
 * Aims to make the local environment the same as the client's environment.
 */
public class EstablishBuildEnvironment extends BuildCommandOnly {
    private final LenientEnvHacker processEnvironment = new LenientEnvHacker();

    protected void doBuild(DaemonCommandExecution execution, Build build) {
        Properties originalSystemProperties = new Properties();
        originalSystemProperties.putAll(System.getProperties());
        File currentDir = new File(".").getAbsoluteFile();

        Properties clientSystemProperties = new Properties();
        clientSystemProperties.putAll(build.getParameters().getSystemProperties());
        System.setProperties(clientSystemProperties);

        Map<String, String> originalEnv = System.getenv();
        processEnvironment.setenv(build.getParameters().getEnvVariables());

        processEnvironment.setProcessDir(build.getParameters().getCurrentDir());

        try {
            execution.proceed();
        } finally {
            System.setProperties(originalSystemProperties);
            processEnvironment.setenv(originalEnv);
            processEnvironment.setProcessDir(currentDir);
        }
    }

}

@@ -756,16 +756,51 @@
 vHacker;
+%0Aimport org.gradle.util.GFileUtils;
 %0A%0Aimport
@@ -831,26 +831,19 @@
 va.util.
-Properties
+Map
 ;%0Aimport
@@ -853,19 +853,26 @@
 va.util.
-Map
+Properties
 ;%0A%0A/**%0A 
@@ -1332,38 +1332,45 @@
 r = 
-new File(%22.%22).getAbsolute
+GFileUtils.canonicalise(new 
 File(
+%22.%22)
 );%0A%0A

Fix to stop adding '\.' to the end of the daemon working directory each time the daemon runs a build.