/*
 * Universal RCV Tabulator
 * Copyright (c) 2017-2020 Bright Spots Developers.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU Affero General Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with this
 * program.  If not, see <http://www.gnu.org/licenses/>.
 */

package network.brightspots.rcv;

import java.util.List;
import java.util.Map;

class Utils {

  static final boolean IS_OS_WINDOWS = osMatchesName("Windows");
  static final boolean IS_OS_MAC = osMatchesName("Mac");
  static final boolean IS_OS_LINUX = osMatchesName("Linux");

  private static final Map<String, String> envMap = System.getenv();

  static boolean isNullOrBlank(String s) {
    return s == null || s.isBlank();
  }

  static boolean isInt(String s) {
    boolean isInt = true;
    try {
      Integer.parseInt(s);
    } catch (NumberFormatException e) {
      isInt = false;
    }
    return isInt;
  }

  static String listToSentenceWithQuotes(List<String> list) {
    String sentence;

    if (list.size() == 1) {
      sentence = String.format("\"%s\"", list.get(0));
    } else if (list.size() == 2) {
      // if there are only 2 items, don't use a comma
      sentence = String.format("\"%s\" and \"%s\"", list.get(0), list.get(1));
    } else {
      StringBuilder stringBuilder = new StringBuilder();
      for (int i = 0; i < list.size() - 1; i++) {
        stringBuilder.append("\"").append(list.get(i)).append("\", ");
      }
      stringBuilder.append("and \"").append(list.get(list.size() - 1)).append("\"");
      sentence = stringBuilder.toString();
    }

    return sentence;
  }

  static String getComputerName() {
    String computerName = "[unknown]";
    if (envMap.containsKey("COMPUTERNAME")) {
      computerName = envMap.get("COMPUTERNAME");
    } else if (envMap.containsKey("HOSTNAME")) {
      computerName = envMap.get("HOSTNAME");
    }
    return computerName;
  }

  static String getUserName() {
    return envMap.getOrDefault("USERNAME", "[unknown]");
  }

  private static boolean osMatchesName(final String osNamePrefix) {
    return System.getProperty("os.name").toUpperCase().startsWith(osNamePrefix.toUpperCase());
  }
}

@@ -778,16 +778,54 @@
 s.rcv;%0A%0A
+import java.net.UnknownHostException;%0A
 import j
@@ -2174,16 +2174,195 @@
 nown%5D%22;%0A
+    try %7B%0A      java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();%0A      computerName = localMachine.getHostName();%0A    %7D catch (UnknownHostException e) %7B%0A  
     if (
@@ -2397,32 +2397,34 @@
 NAME%22)) %7B%0A      
+ 
+ 
 computerName = e
@@ -2446,24 +2446,26 @@
 UTERNAME%22);%0A
+  
     %7D else i
@@ -2503,24 +2503,26 @@
 %22)) %7B%0A      
+ 
+ 
 computerName
@@ -2544,24 +2544,32 @@
 HOSTNAME%22);%0A
+      %7D%0A
     %7D%0A    re
@@ -2628,22 +2628,99 @@
 ) %7B%0A    
-return
+String user = System.getProperty(%22user.name%22);%0A    if (user == null) %7B%0A      user =
  envMap.
@@ -2758,16 +2758,39 @@
 own%5D%22);%0A
+    %7D%0A    return user;%0A
   %7D%0A%0A  p

fix user and computer name logging (#525)