/*
 * Copyright 2017 flow.ci
 *
 * 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 com.flow.platform.util;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;

/**
 * @author yang
 */
public class SystemUtil {

    private final static char ENV_VAR_START_CHAR = '$';
    private final static char ENV_VAR_LEFT_BRACKET = '{';
    private final static char ENV_VAR_RIGHT_BRACKET = '}';

    public static boolean isWindows() {
        String os = System.getProperty("os.name").toLowerCase();
        return os.startsWith("win");
    }

    /**
     * Parse path with ${xxx} variable to absolute path
     *
     * @param pathWithEnv path with folder/${xxx}/folder
     * @return absolute path
     */
    public static Path replacePathWithEnv(String pathWithEnv) {
        String[] paths = pathWithEnv.split(Pattern.quote(File.separator));
        Path path = Paths.get("/");

        for (String pathItem : paths) {
            int index = pathItem.indexOf("$", 0);

            if (index < 0) {
                path = Paths.get(path.toString(), pathItem);
                continue;
            }

            path = Paths.get(path.toString(), parseEnv(pathItem));
        }

        return path;
    }

    /**
     * Parse ${xx} variable to exact value
     */
    public static String parseEnv(String env) {
        if (env == null) {
            throw new IllegalArgumentException();
        }

        if (env.charAt(0) != ENV_VAR_START_CHAR) {
            throw new IllegalArgumentException();
        }

        boolean hasBracket = env.charAt(1) == ENV_VAR_LEFT_BRACKET;
        env = env.substring(1);

        if (!hasBracket) {
            return System.getenv(env);
        }

        env = env.substring(1, env.length() - 1);
        return System.getenv(env);
    }
}

@@ -617,16 +617,55 @@
 .util;%0A%0A
+import com.google.common.base.Strings;%0A
 import j
@@ -732,16 +732,42 @@
 .Paths;%0A
+import java.util.Objects;%0A
 import j
@@ -1957,19 +1957,27 @@
 if (
-env == null
+Objects.isNull(env)
 ) %7B%0A
@@ -2292,37 +2292,40 @@
      return 
-System.
 get
-e
+E
 nv
+OrProperty
 (env);%0A     
@@ -2399,26 +2399,261 @@
 urn 
-System.getenv(env)
+getEnvOrProperty(env);%0A    %7D%0A%0A    private static String getEnvOrProperty(String name) %7B%0A        String value = System.getenv(name);%0A%0A        if (Strings.isNullOrEmpty(value)) %7B%0A            return System.getProperty(name);%0A        %7D%0A%0A        return value
 ;%0A  

support load java property