/*
 * Copyright 2021, TeamDev. All rights reserved.
 *
 * 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
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.tools;

import java.util.Locale;

/**
 * Detects current operating system properties.
 *
 * <p>Based on {@code org.apache.tools.ant.taskdefs.condition.Os}.
 */
@SuppressWarnings("AccessOfSystemProperties") // to get current OS props.
public enum OsFamily {

    Windows,

    Win9x {
        @Override
        public boolean isCurrent() {
            boolean hasSuffix = OS_NAME.contains("95")
                    || OS_NAME.contains("98")
                    || OS_NAME.contains("me");
            return Windows.isCurrent() && hasSuffix;
        }
    },

    WinNT {
        @Override
        public boolean isCurrent() {
            return Windows.isCurrent() && !Win9x.isCurrent();
        }
    },

    Os2("os/2"),

    macOS("mac") {
        @Override
        public boolean isCurrent() {
            return super.isCurrent() || OS_NAME.contains(DARWIN);
        }
    },

    OpenVms("openvms"),

    Unix {
        @Override
        public boolean isCurrent() {
            boolean separatorMatches = ":".equals(PATH_SEP);
            boolean notMac = !macOS.isCurrent()
                    || OS_NAME.endsWith("x")
                    || OS_NAME.contains(DARWIN);
            boolean notVms = !OpenVms.isCurrent();
            return separatorMatches && notVms && notMac;
        }
    };

    private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
    private static final String PATH_SEP = System.getProperty("path.separator");

    /**
     * OpenJDK is reported to call MacOS X "Darwin".
     *
     * @see <a href="https://issues.apache.org/bugzilla/show_bug.cgi?id=44889">Bug 1</a>
     * @see <a href="https://issues.apache.org/jira/browse/HADOOP-3318">Bug 2</a>
     */
    private static final String DARWIN = "darwin";

    /** A lower-cased name of the OS family. */
    private final String signature;

    /** Creates an instance with the signature taken as a lower-cased enum item name. */
    OsFamily() {
        this.signature = name().toLowerCase(Locale.ENGLISH);
    }

    /** Creates an instance with the passed signature value. */
    OsFamily(String signature) {
        this.signature = signature;
    }

    /**
     * Tells if the operating system under which the code is executed belongs
     * to this OS family.
     */
    public boolean isCurrent() {
        boolean result = OS_NAME.contains(signature);
        return result;
    }
}

@@ -1529,459 +1529,8 @@
 s,%0A%0A
-    Win9x %7B%0A        @Override%0A        public boolean isCurrent() %7B%0A            boolean hasSuffix = OS_NAME.contains(%2295%22)%0A                    %7C%7C OS_NAME.contains(%2298%22)%0A                    %7C%7C OS_NAME.contains(%22me%22);%0A            return Windows.isCurrent() && hasSuffix;%0A        %7D%0A    %7D,%0A%0A    WinNT %7B%0A        @Override%0A        public boolean isCurrent() %7B%0A            return Windows.isCurrent() && !Win9x.isCurrent();%0A        %7D%0A    %7D,%0A%0A    Os2(%22os/2%22),%0A%0A
     
@@ -1540,24 +1540,24 @@
 OS(%22mac%22) %7B%0A
+
         @Ove
@@ -1687,33 +1687,8 @@
 %7D,%0A%0A
-    OpenVms(%22openvms%22),%0A%0A
     
@@ -1987,25 +1987,33 @@
 = !O
-penVms.isCurrent(
+S_NAME.contains(%22openvms%22
 );%0A 

Reduce the list of detected OS