/*
 * 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 org.jutils.jhardware.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

/**
 *
 * @author Javier Garcia Alonso
 */
public class HardwareInfoUtils {

    private static final String CRLF = "\r\n";
    
    //Hide constructor
    private HardwareInfoUtils() {
        
    }

    public static Stream<String> readFile(String filePath) {
        Path path = Paths.get(filePath);

        Stream<String> fileLines = null;
        try {
            fileLines = Files.lines(path);
        } catch (IOException ex) {
            Logger.getLogger(HardwareInfoUtils.class.getName()).log(Level.SEVERE, null, ex);
        }

        return fileLines;
    }

    public static String executeCommand(String... command) {
        StringBuilder commandOutput = new StringBuilder();
        BufferedReader processOutput = null;

        try {
            Process process = Runtime.getRuntime().exec(command);
            
            readData(process, commandOutput, processOutput);
        } catch (IOException ex) {
            Logger.getLogger(HardwareInfoUtils.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (processOutput != null) {
                try {
                    processOutput.close();
                } catch (IOException ex) {
                    Logger.getLogger(HardwareInfoUtils.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        return commandOutput.toString();
    }
    
    private static void readData(Process process, StringBuilder commandOutput, BufferedReader processOutput) throws IOException {
        try {
                process.waitFor();
            } catch (InterruptedException ex) {
                Logger.getLogger(HardwareInfoUtils.class.getName()).log(Level.SEVERE, null, ex);
            }            

            if (process.exitValue() == 0) {
                processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()));
            } else {
                processOutput = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            }

            String line;
            while ((line = processOutput.readLine()) != null) {
                if (!line.isEmpty()) {
                    commandOutput.append(line).append(CRLF);
                }
            }
    }

    public static boolean isSudo() {
        return !HardwareInfoUtils.executeCommand("sudo", "-n", "true").contains("a password is required");
    }

    public static String toCamelCase(String s) {
        String[] parts = s.split("_");
        String camelCaseString = "";
        for (String part : parts) {
            camelCaseString = camelCaseString + toProperCase(part);
        }
        return camelCaseString;
    }

    private static String toProperCase(String s) {
        return s.substring(0, 1).toUpperCase()
                + s.substring(1).toLowerCase();
    }
}

@@ -985,20 +985,16 @@
 %22%5Cr%5Cn%22;%0A
-    
 %0A    //H
@@ -1043,24 +1043,16 @@
 ils() %7B%0A
-        
 %0A    %7D%0A%0A
@@ -1670,28 +1670,16 @@
 mmand);%0A
-            
 %0A       
@@ -1874,309 +1874,8 @@
   %7D 
-finally %7B%0A            if (processOutput != null) %7B%0A                try %7B%0A                    processOutput.close();%0A                %7D catch (IOException ex) %7B%0A                    Logger.getLogger(HardwareInfoUtils.class.getName()).log(Level.SEVERE, null, ex);%0A                %7D%0A            %7D%0A        %7D
 %0A%0A  
@@ -1919,20 +1919,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri
@@ -2072,28 +2072,24 @@
             
-    
 process.wait
@@ -2091,28 +2091,24 @@
 .waitFor();%0A
-    
         %7D ca
@@ -2131,36 +2131,32 @@
 Exception ex) %7B%0A
-    
             Logg
@@ -2244,24 +2244,19 @@
     
-    %7D   
+%7D%0A%0A
         
  %0A%0A 
@@ -2251,18 +2251,21 @@
         
- %0A
+try %7B
 %0A       
@@ -2756,24 +2756,337 @@
           %7D%0A
+        %7D finally %7B%0A             if (processOutput != null) %7B%0A                try %7B%0A                    processOutput.close();%0A                %7D catch (IOException ex) %7B%0A                    Logger.getLogger(HardwareInfoUtils.class.getName()).log(Level.SEVERE, null, ex);%0A                %7D%0A            %7D%0A        %7D%0A
     %7D%0A%0A    p

Fix bufferedreader not correctly closed