package com.mojang.launcher;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public enum OperatingSystem {
   LINUX("linux", new String[]{"linux", "unix"}),
   WINDOWS("windows", new String[]{"win"}),
   OSX("osx", new String[]{"mac"}),
   UNKNOWN("unknown", new String[0]);

   private static final Logger LOGGER = LogManager.getLogger();
   private final String name;
   private final String[] aliases;

   private OperatingSystem(String name, String... aliases) {
      this.name = name;
      this.aliases = aliases == null ? new String[0] : aliases;
   }

   public String getName() {
      return this.name;
   }

   public String[] getAliases() {
      return this.aliases;
   }

   public boolean isSupported() {
      return this != UNKNOWN;
   }

   public String getJavaDir() {
      String separator = System.getProperty("file.separator");
      String path = System.getProperty("java.home") + separator + "bin" + separator;
      return getCurrentPlatform() == WINDOWS && (new File(path + "javaw.exe")).isFile() ? path + "javaw.exe" : path + "java";
   }

   public static OperatingSystem getCurrentPlatform() {
      String osName = System.getProperty("os.name").toLowerCase();
      OperatingSystem[] var1 = values();
      int var2 = var1.length;

      for(int var3 = 0; var3 < var2; ++var3) {
         OperatingSystem os = var1[var3];
         String[] var5 = os.getAliases();
         int var6 = var5.length;

         for(int var7 = 0; var7 < var6; ++var7) {
            String alias = var5[var7];
            if (osName.contains(alias)) {
               return os;
            }
         }
      }

      return UNKNOWN;
   }

   public static void openLink(URI link) {
      try {
         Class<?> desktopClass = Class.forName("java.awt.Desktop");
         Object o = desktopClass.getMethod("getDesktop").invoke((Object)null);
         desktopClass.getMethod("browse", URI.class).invoke(o, link);
      } catch (Throwable var4) {
         if (getCurrentPlatform() == OSX) {
            try {
               Runtime.getRuntime().exec(new String[]{"/usr/bin/open", link.toString()});
            } catch (IOException var3) {
               LOGGER.error((String)("Failed to open link " + link.toString()), (Throwable)var3);
            }
         } else {
            LOGGER.error("Failed to open link " + link.toString(), var4);
         }
      }

   }

   public static void openFolder(File path) {
      String absolutePath = path.getAbsolutePath();
      OperatingSystem os = getCurrentPlatform();
      if (os == OSX) {
         try {
            Runtime.getRuntime().exec(new String[]{"/usr/bin/open", absolutePath});
            return;
         } catch (IOException var7) {
            LOGGER.error((String)("Couldn't open " + path + " through /usr/bin/open"), (Throwable)var7);
         }
      } else if (os == WINDOWS) {
         String cmd = String.format("cmd.exe /C start \"Open file\" \"%s\"", absolutePath);

         try {
            Runtime.getRuntime().exec(cmd);
            return;
         } catch (IOException var6) {
            LOGGER.error((String)("Couldn't open " + path + " through cmd.exe"), (Throwable)var6);
         }
      }

      try {
         Class<?> desktopClass = Class.forName("java.awt.Desktop");
         Object desktop = desktopClass.getMethod("getDesktop").invoke((Object)null);
         desktopClass.getMethod("browse", URI.class).invoke(desktop, path.toURI());
      } catch (Throwable var5) {
         LOGGER.error("Couldn't open " + path + " through Desktop.browse()", var5);
      }

   }
}
