// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.internal.statistic.collectors.fus.os;

import com.intellij.internal.statistic.beans.UsageDescriptor;
import com.intellij.internal.statistic.service.fus.collectors.ApplicationUsagesCollector;
import com.intellij.internal.statistic.service.fus.collectors.UsageDescriptorKeyValidator;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.Version;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class OsVersionUsageCollector extends ApplicationUsagesCollector {
  @NotNull
  @Override
  public Set<UsageDescriptor> getUsages() {
    return getDescriptors();
  }

  @NotNull
  public static Set<UsageDescriptor> getDescriptors() {
    UsageDescriptor descriptor;

    if (SystemInfo.isLinux) {
      final String version = getLinuxOSVersion();
      descriptor = new UsageDescriptor("Linux/" + version, 1);
    }
    else {
      descriptor = new UsageDescriptor(UsageDescriptorKeyValidator.ensureProperKey(SystemInfo.OS_NAME + "." + SystemInfo.OS_VERSION), 1);
    }

    return Collections.singleton(descriptor);
  }

  @NotNull
  public static String getLinuxOSVersion() {
    String releaseId = null, releaseVersion = null;

    try {
      Map<String, String> values = Files.lines(Paths.get("/etc/os-release"))
                                        .map(line -> StringUtil.split(line, "="))
                                        .filter(parts -> parts.size() == 2)
                                        .collect(
                                          Collectors.toMap(parts -> parts.get(0), parts -> StringUtil.unquoteString(parts.get(1))));
      releaseId = values.get("ID");
      releaseVersion = values.get("VERSION_ID");
    }
    catch (IOException ignored) {
    }

    if (releaseId == null) releaseId = "unknown";

    if (releaseVersion == null) {
      releaseVersion = SystemInfo.OS_VERSION;
      Version version = Version.parseVersion(releaseVersion);
      if (version != null) {
        releaseVersion = version.toCompactString();
      }
    }
    return releaseId + " " + releaseVersion;
  }

  @NotNull
  @Override
  public String getGroupId() { return "statistics.os.version"; }
}
@@ -810,16 +810,48 @@
 lectors;
+%0Aimport java.util.stream.Stream;
 %0A%0Apublic
@@ -1596,36 +1596,24 @@
 try 
-%7B%0A      Map%3CString, 
+(Stream%3C
 String%3E 
 valu
@@ -1608,20 +1608,19 @@
 String%3E 
-valu
+lin
 es = Fil
@@ -1661,41 +1661,55 @@
 e%22))
+) %7B
 %0A      
-                          
+Map%3CString, String%3E releases = lines%0A
     
@@ -1758,40 +1758,8 @@
 %22))%0A
-                                
     
@@ -1810,92 +1810,17 @@
     
-                                .collect(%0A                                          
+.collect(
 Coll
@@ -1906,16 +1906,17 @@
 (1))));%0A
+%0A
       re
@@ -1925,20 +1925,22 @@
 aseId = 
-valu
+releas
 es.get(%22
@@ -1972,12 +1972,14 @@
 n = 
-valu
+releas
 es.g

fix leak on reading os-release file