001/*
002 * Syncany, www.syncany.org
003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package org.syncany.util;
019
020import java.io.File;
021import java.nio.file.Files;
022import java.nio.file.Paths;
023
024public class EnvironmentUtil {
025        public enum OperatingSystem {
026                WINDOWS(false), 
027                OSX(true), 
028                UNIX_LIKE(true);
029                
030                private boolean unixLike;
031                
032                OperatingSystem(boolean unixLike) {
033                        this.unixLike = unixLike;
034                }
035                
036                public boolean isUnixLike() {
037                        return unixLike;
038                }
039        };
040
041        private static OperatingSystem operatingSystem;
042        
043        static {
044                if (File.separatorChar == '\\') {
045                        operatingSystem = OperatingSystem.WINDOWS;
046                }
047                else if (System.getProperty("os.name").toUpperCase().contains("OS X")) {
048                        operatingSystem = OperatingSystem.OSX;
049                }
050                else {
051                        operatingSystem = OperatingSystem.UNIX_LIKE;
052                }
053        }               
054
055        public static void setOperatingSystem(OperatingSystem aOperatingSystem) {
056                operatingSystem = aOperatingSystem;
057        }
058        
059        public static OperatingSystem getOperatingSystem() {
060                return operatingSystem;
061        }
062        
063        public static boolean isUnixLikeOperatingSystem() {
064                return operatingSystem.isUnixLike();
065        }
066
067        public static boolean isWindows() {
068                return operatingSystem == OperatingSystem.WINDOWS;
069        }       
070        
071        public static boolean isMacOSX() {
072                return operatingSystem == OperatingSystem.OSX;
073        }
074
075        public static boolean symlinksSupported() {
076                return isUnixLikeOperatingSystem();
077        }
078        
079        public static boolean isDebianBased() {
080                return isUnixLikeOperatingSystem() && Files.exists(Paths.get("/usr/bin/dpkg"));
081        }
082
083        /**
084         * @see <a href="http://lopica.sourceforge.net/os.html">Possible return values</a>
085         * @return x86, x86_64, sparc, ppc, armv41, i686, ppc64, powerpc, par-risc, ia64n, pa_risk2.0, pa_risk, power, power_rs, mips, alpha
086         */
087        public static String getArchDescription() {
088                String realArchDescription = System.getProperty("os.arch").toLowerCase();
089                
090                switch (realArchDescription) {
091                case "i386":
092                        return "x86";
093                case "amd64":
094                        return "x86_64";
095                default:
096                        return realArchDescription;
097                }
098        }
099        
100        /**
101         * Returns the operating system short descripton.
102         * 
103         * @see <a href="http://www.prepareitonline.com/forums/prepare/24-developer-discussions/question/1615-what-are-the-possible-values-system-getproperty-os-name-can-return-on-different-systems">Possible output values</a> 
104         * @return aix, digital, freebsd, hp, irix, linux, mac, mpe/ix, netware, os/2, solaris, windows
105         */
106        public static String getOperatingSystemDescription() {
107                String realOperatingSystemDescription = System.getProperty("os.name").toLowerCase().split(" ")[0];
108                
109                switch (realOperatingSystemDescription) {
110                case "linux":
111                case "solaris":
112                case "freebsd":
113                case "aix":
114                        return "linux";
115                        
116                case "mac":
117                case "macosx":
118                        return "macosx";
119                        
120                case "windows":
121                        return "windows";
122                        
123                default:
124                        return realOperatingSystemDescription;
125                }
126        }
127}