/*
 * 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.addthis.hydra.job.minion;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class MacUtils {
    private static final Logger log = LoggerFactory.getLogger(MacUtils.class);

    private MacUtils() {}

    // detect fl-cow in sys env and apple for copy command
    static {
        for (String v : System.getenv().values()) {
            if (v.toLowerCase().contains("libflcow")) {
                log.info("detected support for copy-on-write hard-links");
                linkBackup = true;
                break;
            }
        }
        for (Object v : System.getProperties().values()) {
            if (v.toString().toLowerCase().contains("apple") || v.toString().toLowerCase().contains("mac os x")) {
                log.info("detected darwin-based system. switching to gnu commands");
                MacUtils.cpcmd = "gcp";
                MacUtils.lncmd = "gln";
                MacUtils.lscmd = "gls";
                MacUtils.rmcmd = "grm";
                MacUtils.mvcmd = "gmv";
                MacUtils.ducmd = "gdu";
                MacUtils.useMacFriendlyPSCommands = true;
                break;
            }
        }
    }

    static String cpcmd = "cp";
    static String lncmd = "ln";
    static String lscmd = "ls";
    static String rmcmd = "rm";
    static String mvcmd = "mv";
    static String ducmd = "du";
    static boolean useMacFriendlyPSCommands = false;
    static boolean linkBackup = !System.getProperty("minion.backup.hardlink", "0").equals("0");
}

@@ -778,16 +778,291 @@
 s() %7B%7D%0A%0A
+    static final String cpcmd;%0A    static final String lncmd;%0A    static final String lscmd;%0A    static final String rmcmd;%0A    static final String mvcmd;%0A    static final String ducmd;%0A    static final boolean useMacFriendlyPSCommands;%0A    static final boolean linkBackup;%0A%0A
     // d
@@ -1125,16 +1125,62 @@
 tatic %7B%0A
+        boolean copyOnWriteSupported = false;%0A
         
@@ -1370,26 +1370,36 @@
         
-linkBackup
+copyOnWriteSupported
  = true;
@@ -1438,32 +1438,182 @@
     %7D%0A        %7D%0A
+        linkBackup = !System.getProperty(%22minion.backup.hardlink%22, %220%22).equals(%220%22) %7C%7C copyOnWriteSupported;%0A%0A        boolean useGnuCommands = false;%0A
         for (Obj
@@ -1871,25 +1871,128 @@
         
-MacUtils.
+useGnuCommands = true;%0A                break;%0A            %7D%0A        %7D%0A        if (useGnuCommands) %7B%0A            
 cpcmd = 
@@ -2010,29 +2010,16 @@
         
-    MacUtils.
 lncmd = 
@@ -2037,29 +2037,16 @@
         
-    MacUtils.
 lscmd = 
@@ -2064,29 +2064,16 @@
         
- 
-   MacUtils.
 rmcmd = 
@@ -2091,29 +2091,16 @@
         
-  
-  MacUtils.
 mvcmd = 
@@ -2118,29 +2118,16 @@
         
-   
- MacUtils.
 ducmd = 
@@ -2149,21 +2149,8 @@
     
-    
-MacUtils.
 useM
@@ -2190,36 +2190,16 @@
     
-        break;%0A            %7D
+%7D else %7B
 %0A   
@@ -2207,35 +2207,12 @@
     
-%7D%0A
     
-%7D%0A%0A    static String 
 cpcm
@@ -2225,29 +2225,23 @@
 p%22;%0A    
-static String
+       
  lncmd =
@@ -2251,29 +2251,23 @@
 n%22;%0A    
-static String
+       
  lscmd =
@@ -2277,29 +2277,23 @@
 s%22;%0A    
-static String
+       
  rmcmd =
@@ -2303,29 +2303,23 @@
 m%22;%0A    
-static String
+       
  mvcmd =
@@ -2333,21 +2333,15 @@
     
-static String
+       
  duc
@@ -2355,30 +2355,23 @@
 u%22;%0A    
-static boolean
+       
  useMacF
@@ -2405,98 +2405,18 @@
     
-static boolean linkBackup = !System.getProperty(%22minion.backup.hardlink%22, %220%22).equals(%220%22);
+    %7D%0A    %7D
 %0A%7D%0A

fix crazy ordering of static initialization