private static int getBuildNumber(File installDirectory) {
    installDirectory = installDirectory.getAbsoluteFile();

    File buildTxt = new File(installDirectory, BUILD_NUMBER_FILE);
    if ((!buildTxt.exists()) || (buildTxt.isDirectory())) {
      buildTxt = new File(new File(installDirectory, BIN_FOLDER), BUILD_NUMBER_FILE);
    }

    if (buildTxt.exists() && !buildTxt.isDirectory()) {
      int buildNumber = -1;
      String buildNumberText = getContent(buildTxt);
      if (buildNumberText != null) {
        try {
          if (buildNumberText.length() > 1) {
            buildNumberText = buildNumberText.trim();
            buildNumber = Integer.parseInt(buildNumberText);
          }
        } catch (Exception e) {
          // OK
        }
      }
      return buildNumber;
    }

    return -1;
  }
  public static void rename(@NotNull File source, @NotNull File target) throws IOException {
    if (source.renameTo(target)) return;
    if (!source.exists()) return;

    copy(source, target);
    delete(source);
  }
  private static boolean validateOldConfigDir(
      @Nullable File installationHome,
      @Nullable File oldConfigDir,
      @NotNull ConfigImportSettings settings) {
    if (oldConfigDir == null) {
      if (installationHome != null) {
        JOptionPane.showMessageDialog(
            JOptionPane.getRootFrame(),
            ApplicationBundle.message(
                "error.invalid.installation.home",
                installationHome.getAbsolutePath(),
                settings.getProductName(ThreeState.YES)));
      }
      return false;
    }

    if (!oldConfigDir.exists()) {
      JOptionPane.showMessageDialog(
          JOptionPane.getRootFrame(),
          ApplicationBundle.message("error.no.settings.path", oldConfigDir.getAbsolutePath()),
          ApplicationBundle.message("title.settings.import.failed"),
          JOptionPane.WARNING_MESSAGE);
      return false;
    }
    return true;
  }
  public static boolean isInstallationHomeOrConfig(
      @NotNull final String installationHome, @NotNull final ConfigImportSettings settings) {
    if (new File(installationHome, OPTIONS_XML).exists()) return true;
    if (new File(installationHome, CONFIG_RELATED_PATH + OPTIONS_XML).exists()) return true;

    if (!new File(installationHome, BIN_FOLDER).exists()) {
      return false;
    }

    File libFolder = new File(installationHome, "lib");
    boolean quickTest = false;
    String[] mainJarNames = settings.getMainJarNames();
    for (String name : mainJarNames) {
      String mainJarName = StringUtil.toLowerCase(name) + ".jar";
      //noinspection HardCodedStringLiteral
      if (new File(libFolder, mainJarName).exists()) {
        quickTest = true;
        break;
      }
    }
    if (!quickTest) return false;

    File[] files = getLaunchFilesCandidates(new File(installationHome), settings);
    for (File file : files) {
      if (file.exists()) return true;
    }

    return false;
  }
 private static File getTempFile(@NotNull String originalFileName, @NotNull File parent) {
   int randomSuffix = (int) (System.currentTimeMillis() % 1000);
   for (int i = randomSuffix; ; i++) {
     @NonNls String name = "___" + originalFileName + i + ASYNC_DELETE_EXTENSION;
     File tempFile = new File(parent, name);
     if (!tempFile.exists()) return tempFile;
   }
 }
 public static File findSequentNonexistentFile(
     @NotNull File parentFolder, @NotNull String filePrefix, @NotNull String extension) {
   int postfix = 0;
   String ext = extension.isEmpty() ? "" : '.' + extension;
   File candidate = new File(parentFolder, filePrefix + ext);
   while (candidate.exists()) {
     postfix++;
     candidate = new File(parentFolder, filePrefix + Integer.toString(postfix) + ext);
   }
   return candidate;
 }
  @Nullable
  public static File findFirstThatExist(@NotNull String... paths) {
    for (String path : paths) {
      if (!StringUtil.isEmptyOrSpaces(path)) {
        File file = new File(toSystemDependentName(path));
        if (file.exists()) return file;
      }
    }

    return null;
  }
  @Nullable
  private static File findOldConfigDir(
      @NotNull File configDir, @Nullable String customPathSelector) {
    final File selectorDir = CONFIG_RELATED_PATH.isEmpty() ? configDir : configDir.getParentFile();
    final File parent = selectorDir.getParentFile();
    if (parent == null || !parent.exists()) {
      return null;
    }
    File maxFile = null;
    long lastModified = 0;
    final String selector =
        PathManager.getPathsSelector() != null
            ? PathManager.getPathsSelector()
            : selectorDir.getName();

    final String prefix = getPrefixFromSelector(selector);
    final String customPrefix =
        customPathSelector != null ? getPrefixFromSelector(customPathSelector) : null;
    for (File file :
        parent.listFiles(
            new FilenameFilter() {
              @Override
              public boolean accept(@NotNull File file, @NotNull String name) {
                return StringUtil.startsWithIgnoreCase(name, prefix)
                    || customPrefix != null && StringUtil.startsWithIgnoreCase(name, customPrefix);
              }
            })) {
      File options = new File(file, CONFIG_RELATED_PATH + OPTIONS_XML);
      if (!options.exists()) {
        continue;
      }

      long modified = options.lastModified();
      if (modified > lastModified) {
        lastModified = modified;
        maxFile = file;
      }
    }
    return maxFile != null ? new File(maxFile, CONFIG_RELATED_PATH) : null;
  }
  public static File findSequentNonexistentFile(
      @NotNull File aParentFolder,
      @NotNull @NonNls final String aFilePrefix,
      @NotNull String aExtension) {
    int postfix = 0;
    String ext = aExtension.isEmpty() ? "" : "." + aExtension;

    File candidate = new File(aParentFolder, aFilePrefix + ext);
    while (candidate.exists()) {
      postfix++;
      candidate = new File(aParentFolder, aFilePrefix + Integer.toString(postfix) + ext);
    }
    return candidate;
  }
  /**
   * Returns empty string for empty path. First checks whether provided path is a path of a file
   * with sought-for name. Unless found, checks if provided file was a directory. In this case
   * checks existence of child files with given names in order "as provided". Finally checks
   * filename among brother-files of provided. Returns null if nothing found.
   *
   * @return path of the first of found files or empty string or null.
   */
  @Nullable
  public static String findFileInProvidedPath(String providedPath, String... fileNames) {
    if (StringUtil.isEmpty(providedPath)) {
      return "";
    }

    File providedFile = new File(providedPath);
    if (providedFile.exists()) {
      String name = providedFile.getName();
      for (String fileName : fileNames) {
        if (name.equals(fileName)) {
          return toSystemDependentName(providedFile.getPath());
        }
      }
    }

    if (providedFile.isDirectory()) { // user chose folder with file
      for (String fileName : fileNames) {
        File file = new File(providedFile, fileName);
        if (fileName.equals(file.getName()) && file.exists()) {
          return toSystemDependentName(file.getPath());
        }
      }
    }

    providedFile = providedFile.getParentFile(); // users chose wrong file in same directory
    if (providedFile != null && providedFile.exists()) {
      for (String fileName : fileNames) {
        File file = new File(providedFile, fileName);
        if (fileName.equals(file.getName()) && file.exists()) {
          return toSystemDependentName(file.getPath());
        }
      }
    }

    return null;
  }
  private static File getOldPath(
      final File oldInstallHome,
      final ConfigImportSettings settings,
      final String propertyName,
      final Function<String, String> fromPathSelector) {
    final File[] launchFileCandidates = getLaunchFilesCandidates(oldInstallHome, settings);

    // custom config folder
    for (File candidate : launchFileCandidates) {
      if (candidate.exists()) {
        String configDir =
            PathManager.substituteVars(
                getPropertyFromLaxFile(candidate, propertyName), oldInstallHome.getPath());
        if (configDir != null) {
          File probableConfig = new File(configDir);
          if (probableConfig.exists()) return probableConfig;
        }
      }
    }

    // custom config folder not found - use paths selector
    for (File candidate : launchFileCandidates) {
      if (candidate.exists()) {
        final String pathsSelector =
            getPropertyFromLaxFile(candidate, PathManager.PROPERTY_PATHS_SELECTOR);
        if (pathsSelector != null) {
          final String configDir = fromPathSelector.fun(pathsSelector);
          final File probableConfig = new File(configDir);
          if (probableConfig.exists()) {
            return probableConfig;
          }
        }
      }
    }

    return null;
  }
  public static boolean moveDirWithContent(@NotNull File fromDir, @NotNull File toDir) {
    if (!toDir.exists()) return fromDir.renameTo(toDir);

    File[] files = fromDir.listFiles();
    if (files == null) return false;

    boolean success = true;

    for (File fromFile : files) {
      File toFile = new File(toDir, fromFile.getName());
      success = success && fromFile.renameTo(toFile);
    }
    fromDir.delete();

    return success;
  }
 private static boolean loadOldPlugins(File plugins, File dest) throws IOException {
   if (plugins.exists()) {
     List<IdeaPluginDescriptorImpl> descriptors = new SmartList<IdeaPluginDescriptorImpl>();
     PluginManagerCore.loadDescriptors(plugins, descriptors, null, 0);
     List<String> oldPlugins = new SmartList<String>();
     for (IdeaPluginDescriptorImpl descriptor : descriptors) {
       // check isBundled also - probably plugin is bundled in new IDE version
       if (descriptor.isEnabled() && !descriptor.isBundled()) {
         oldPlugins.add(descriptor.getPluginId().getIdString());
       }
     }
     if (!oldPlugins.isEmpty()) {
       PluginManagerCore.savePluginsList(
           oldPlugins, false, new File(dest, PluginManager.INSTALLED_TXT));
     }
     return true;
   }
   return false;
 }
 public static void ensureExists(@NotNull File dir) throws IOException {
   if (!dir.exists() && !dir.mkdirs()) {
     throw new IOException(
         CommonBundle.message("exception.directory.can.not.create", dir.getPath()));
   }
 }