package org.implab.gradle.common.core.os;

import org.implab.gradle.common.core.lang.LazyValue;

class SystemResolver {

    private final static LazyValue<OperatingSystem> current = new LazyValue<>(SystemResolver::resolveCurrent);

    private static OperatingSystem resolveCurrent() {
        var osName = System.getProperty("os.name");
        var osVersion = System.getProperty("os.version");

        return forName(osName, osVersion);
    }

    public static OperatingSystem current() {
        return current.get();
    }

    public static OperatingSystem forName(String os, String version) {
        var osName = os.toLowerCase();

        if (osName.contains("windows")) {
            return new Windows(osName, version);
        } else if (osName.contains("mac os x") || osName.contains("darwin") || osName.contains("osx")) {
            return new MacOs(osName, version);
        } else if (osName.contains("linux")) {
            return new Linux(osName, version);
        } else if (osName.contains("freebsd")) {
            return new FreeBsd(osName, version);
        } else {
            // Not strictly true
            return new GenericSystem(osName, version);
        }
    }
}
