/*
 * Copyright (C) 2016 Satomichi Nishihara
 *
 * This file is distributed under the terms of the
 * GNU General Public License. See the file `LICENSE'
 * in the root directory of the present distribution,
 * or http://www.gnu.org/copyleft/gpl.txt .
 */

package burai.com.env;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MacCPUInfo extends CPUInfo {

    private static final String CHK_MAC_CPUCOM = "system_profiler SPHardwareDataType";

    private static final String CPU_WORD = "Cores";

    public MacCPUInfo() {
        super();
    }

    @Override
    protected int countNumCPUs() {

        /*
         *  TODO: get #cpus for MacOS.
         */

        int numCPUs = 1;

        try {
            Process process = Runtime.getRuntime().exec(CHK_MAC_CPUCOM);
            BufferedReader in = new BufferedReader(new InputStreamReader(process
                .getInputStream()));

            String line = null;
            while ((line = in.readLine()) != null) {
                line = line.trim();
                if (line.contains(CPU_WORD)){
                    String[] nCPULine = line.split(" ", 0);
                    if (nCPULine == null) {
                        continue;
                    }
                    if (nCPULine.length == 0){
                        numCPUs = 1;
                        break;
                    }
                    else {
                        numCPUs = Integer.parseInt(nCPULine[nCPULine.length -1]);
                        if (numCPUs <= 0){
                            numCPUs = 1;
                            break;
                        }
                    }
                }
            }

        } catch (IOException e) {
            // TODO
            numCPUs = 1;
            e.printStackTrace();
        }

        return numCPUs;
    }
}

@@ -683,75 +683,8 @@
 %0D%0A%0D%0A
-        /*%0D%0A         *  TODO: get #cpus for MacOS.%0D%0A         */%0D%0A%0D%0A
     
@@ -792,24 +792,295 @@
 C_CPUCOM);%0D%0A
+%0D%0A            int exitCode = 0;%0D%0A            try %7B%0D%0A                exitCode = process.waitFor();%0D%0A            %7D catch (InterruptedException e) %7B%0D%0A                exitCode = 1;%0D%0A            %7D%0D%0A            if (exitCode != 0) %7B%0D%0A                return 1;%0D%0A            %7D%0D%0A%0D%0A
             
@@ -1356,24 +1356,26 @@
 PU_WORD))%7B%0D%0A
+%0D%0A
             
@@ -1422,16 +1422,62 @@
  %22, 0);%0D
+%0A%0D%0A                    String strCPUs = null;%0D
 %0A       
@@ -1506,15 +1506,38 @@
 ine 
-=
+!
 = null
+ && nCPULine.length %3E 0
 ) %7B%0D
@@ -1565,16 +1565,47 @@
     
-continue
+strCPUs = nCPULine%5BnCPULine.length - 1%5D
 ;%0D%0A 
@@ -1618,32 +1618,34 @@
              %7D%0D%0A
+%0D%0A
                 
@@ -1656,32 +1656,63 @@
 if (
-nCPULine.length == 0)%7B%0D%0A
+strCPUs != null) %7B%0D%0A                        try %7B%0D%0A    
     
@@ -1733,33 +1733,57 @@
       numCPUs = 
-1
+Integer.parseInt(strCPUs)
 ;%0D%0A             
@@ -1785,38 +1785,67 @@
                 
-break;
+%7D catch (NumberFormatException e) %7B
 %0D%0A              
@@ -1846,35 +1846,32 @@
             
-%7D%0D%0A
         
             
@@ -1850,33 +1850,46 @@
                 
- 
+numCPUs = 1;%0D%0A
            else 
@@ -1887,16 +1887,8 @@
     
-else %7B%0D%0A
     
@@ -1888,32 +1888,35 @@
                 
+%7D%0D%0A
            numCP
@@ -1914,65 +1914,25 @@
     
-numCPUs = Integer.parseInt(nCPULine%5BnCPULine.length -1%5D);
+         %7D else %7B
 %0D%0A  
@@ -1957,20 +1957,16 @@
     
-if (
 numCPUs 
 %3C= 0
@@ -1965,14 +1965,12 @@
 PUs 
-%3C
 = 
-0)%7B
+1;
 %0D%0A  
@@ -1969,32 +1969,47 @@
 = 1;%0D%0A          
+          %7D%0D%0A%0D%0A
                 
@@ -2006,32 +2006,38 @@
             
+  if (
 numCPUs 
 = 1;%0D%0A      
@@ -2016,36 +2016,38 @@
     if (numCPUs 
+%3C
 = 
-1;
+0)%7B
 %0D%0A              
@@ -2060,23 +2060,22 @@
     
-    break;%0D%0A   
+numCPUs = 1;%0D%0A
     
@@ -2082,34 +2082,35 @@
                 
- 
 %7D
+%0D%0A
 %0D%0A              
@@ -2107,33 +2107,38 @@
                 
-%7D
+break;
 %0D%0A              
@@ -2198,29 +2198,8 @@
  %7B%0D%0A
-            // TODO%0D%0A
     

check exitCode and NumberFormatException in MacCPUInfo.