/*
 * Copyright 2013-present Facebook, Inc.
 *
 * 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.facebook.buck.util.environment;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * Platform on which Buck is currently running. Limited to the OS kind, e.g. Windows or a kind of
 * Unix (MACOS, LINUX, ...).
 */
public enum Platform {
  LINUX("Linux", "linux", PlatformType.UNIX),
  MACOS("OS X", "darwin", PlatformType.UNIX),
  WINDOWS("Windows", "windows", PlatformType.WINDOWS),
  FREEBSD("FreeBSD", "freebsd", PlatformType.UNIX),
  UNKNOWN("Unknown", "unknown", PlatformType.UNKNOWN);

  private String autoconfName;
  private String platformName;
  private PlatformType platformType;

  Platform(String platformName, String autoconfName, PlatformType platformType) {
    this.platformName = platformName;
    this.autoconfName = autoconfName;
    this.platformType = platformType;
  }

  /** @return platform name as used in autoconf target tuples */
  public String getAutoconfName() {
    return autoconfName;
  }

  public String getPrintableName() {
    return platformName;
  }

  public PlatformType getType() {
    return platformType;
  }

  /**
   * Return a path to a special file which accepts reads (always returning EOF) and writes (accepts
   * and immediately discards written data). E.g {@code /dev/null} on Unix and {@code NUL} on
   * Windows.
   */
  public Path getNullDevicePath() {
    if (getType().isUnix()) {
      // This should be a valid path on any Unix/POSIX-conforming system.
      // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap10.html
      return Paths.get("/dev/null");
    } else if (getType().isWindows()) {
      // MS docs on special filenames.  The file named `NUL` or even `NUL.ext` is a blackhole.
      // https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
      return Paths.get("NUL");
    }
    throw new IllegalStateException(
        "don't know null special file for OS: " + System.getProperty("os.name"));
  }

  public static Platform detect() {
    String platformName = System.getProperty("os.name");
    if (platformName == null) {
      return UNKNOWN;
    } else if (platformName.startsWith("Linux")) {
      return LINUX;
    } else if (platformName.startsWith("Mac OS")) {
      return MACOS;
    } else if (platformName.startsWith("Windows")) {
      return WINDOWS;
    } else if (platformName.startsWith("FreeBSD")) {
      return FREEBSD;
    } else {
      return UNKNOWN;
    }
  }
}

@@ -698,16 +698,45 @@
 e.Paths;
+%0Aimport java.util.Properties;
 %0A%0A/**%0A *
@@ -903,16 +903,25 @@
 %22Linux%22,
+ %22Linux%22,
  %22linux%22
@@ -957,16 +957,28 @@
 (%22OS X%22,
+ %22Mac OS X%22,
  %22darwin
@@ -1020,16 +1020,27 @@
 indows%22,
+ %22Windows%22,
  %22window
@@ -1086,16 +1086,27 @@
 reeBSD%22,
+ %22FreeBSD%22,
  %22freebs
@@ -1149,16 +1149,27 @@
 nknown%22,
+ %22Unknown%22,
  %22unknow
@@ -1198,23 +1198,275 @@
 WN);%0A%0A  
-private
+/**%0A   * Name that is used for debugging purposes. This is different from %7B@link #platformName%7D due to%0A   * historical reasons.%0A   *%0A   * %3Cp%3ETODO: remove this field is favor of %7B@link #platformName%7D%0A   */%0A  private final String printableName;%0A%0A  private final
  String 
@@ -1489,16 +1489,22 @@
 private 
+final 
 String p
@@ -1526,16 +1526,22 @@
 private 
+final 
 Platform
@@ -1571,16 +1571,45 @@
 latform(
+%0A      String printableName, 
 String p
@@ -1667,24 +1667,64 @@
 formType) %7B%0A
+    this.printableName = printableName;%0A
     this.pla
@@ -2010,23 +2010,24 @@
 return p
-latform
+rintable
 Name;%0A  
@@ -2959,21 +2959,103 @@
   %7D%0A%0A  p
-ublic
+rivate static final Platform HOST_PLATFORM = detect(System.getProperties());%0A%0A  private
  static 
@@ -3070,16 +3070,37 @@
  detect(
+Properties properties
 ) %7B%0A    
@@ -3121,22 +3121,26 @@
 mName = 
-System
+properties
 .getProp
@@ -3219,84 +3219,56 @@
    %7D
- else if (platformName.startsWith(%22Linux%22)) %7B%0A      return LINUX;%0A    %7D else
+%0A%0A    for (Platform platform : values()) %7B%0A     
  if 
@@ -3296,16 +3296,29 @@
 ith(
-%22Mac OS%22
+platform.platformName
 )) %7B
@@ -3328,152 +3328,102 @@
     
+  
 return 
-MACOS;%0A    %7D else if (platformName.startsWith(%22Windows%22)) %7B
+platform;%0A      %7D%0A    %7D
 %0A    
-  
 return 
-WINDOWS
+UNKNOWN
 ;%0A
-  
   %7D
- else if (platformName.startsWith(%22FreeBSD%22)
+%0A%0A  public static Platform detect(
 ) %7B%0A
-  
     
@@ -3433,57 +3433,22 @@
 urn 
-FREEBSD;%0A    %7D else %7B%0A      return UNKNOWN;%0A    %7D
+HOST_PLATFORM;
 %0A  %7D

Refactor Platform to avoid repetitive work in detect