public static void importConfigsTo(boolean veryFirstStartOnThisComputer,
                                     @NotNull Path newConfigDir,
                                     @NotNull List<String> args,
                                     @NotNull Logger log) {
    log.info("Importing configs to " + newConfigDir);
    System.setProperty(FIRST_SESSION_KEY, Boolean.TRUE.toString());

    var customMigrationOption = CustomConfigMigrationOption.readCustomConfigMigrationOptionAndRemoveMarkerFile(newConfigDir);
    log.info("Custom migration option: " + customMigrationOption);

    if (customMigrationOption instanceof CustomConfigMigrationOption.SetProperties sp) {
      List<String> properties = sp.getProperties();
      log.info("Enabling system properties after restart: " + properties);
      for (String property : properties) System.setProperty(property, Boolean.TRUE.toString());
      return;
    }

    ConfigImportSettings settings = findCustomConfigImportSettings();
    log.info("Custom ConfigImportSettings instance: " + settings);

    List<String> otherProductPrefixes = settings != null ? settings.getProductsToImportFrom(args) : List.of();
    ConfigDirsSearchResult guessedOldConfigDirs = findConfigDirectories(newConfigDir, settings, otherProductPrefixes);
    Path tempBackup = null;
    boolean vmOptionFileChanged = false;
    @Nullable List<String> vmOptionsLines = null;
    ImportOldConfigsState.InitialImportScenario importScenarioStatistics = null;

    try {
      Pair<Path, Path> oldConfigDirAndOldIdePath = null;
      if (customMigrationOption instanceof CustomConfigMigrationOption.MigrateFromCustomPlace ||
          customMigrationOption instanceof CustomConfigMigrationOption.StartWithCleanConfig) {
        vmOptionFileChanged = doesVmOptionsFileExist(newConfigDir);
        try {
          if (customMigrationOption instanceof CustomConfigMigrationOption.MigrateFromCustomPlace mcp) {
            oldConfigDirAndOldIdePath = findConfigDirectoryByPath(mcp.getLocation());
            if (oldConfigDirAndOldIdePath == null) {
              logInfoAboutNotAcceptedConfigDirectory(log, "Custom location", mcp.getLocation());
            }
            if (oldConfigDirAndOldIdePath != null && oldConfigDirAndOldIdePath.first != null) {
              if (doesVmOptionsFileExist(newConfigDir) && !vmOptionsRequiresMerge(oldConfigDirAndOldIdePath.first, newConfigDir, log)) {
                //save old lines for the new file
                vmOptionsLines = Files.readAllLines(newConfigDir.resolve(VMOptions.getFileName()), VMOptions.getFileCharset());
                vmOptionFileChanged = false;
              }
            }
            tempBackup = backupCurrentConfigToTempAndDelete(newConfigDir, log, false, settings);
            importScenarioStatistics = IMPORT_SETTINGS_ACTION;
          }
          else {
            tempBackup = backupCurrentConfigToTempAndDelete(newConfigDir, log, true, settings);
            importScenarioStatistics = RESTORE_DEFAULT_ACTION;
          }
        }
        catch (IOException e) {
          log.error("Couldn't backup current config or delete current config directory", e);
        }
      }
      else if (IdeStartupWizardKt.isIdeStartupWizardEnabled()) {
        if (!guessedOldConfigDirs.isEmpty() && !shouldAskForConfig()) {
          Pair<Path, FileTime> bestConfigGuess = guessedOldConfigDirs.getFirstItem();
          if (!isConfigOld(bestConfigGuess.second)) {
            oldConfigDirAndOldIdePath = findConfigDirectoryByPath(bestConfigGuess.first);
            if (oldConfigDirAndOldIdePath == null) {
              logInfoAboutNotAcceptedConfigDirectory(log, "Previous config directory", bestConfigGuess.first);
              importScenarioStatistics = CONFIG_DIRECTORY_NOT_FOUND;
            }
          }
        }
      }
      else {
        boolean askForConfig = shouldAskForConfig();
        if (askForConfig) {
          oldConfigDirAndOldIdePath = showDialogAndGetOldConfigPath(guessedOldConfigDirs.getPaths());
          importScenarioStatistics = SHOW_DIALOG_REQUESTED_BY_PROPERTY;
        }
        else if (guessedOldConfigDirs.isEmpty()) {
          boolean importedFromCloud = false;
          CloudConfigProvider configProvider = CloudConfigProvider.getProvider();
          if (configProvider != null) {
            importedFromCloud = configProvider.importSettingsSilently(newConfigDir);

            if (importedFromCloud) {
              importScenarioStatistics = IMPORTED_FROM_CLOUD;
            }
          }
          if (!importedFromCloud && !veryFirstStartOnThisComputer) {
            oldConfigDirAndOldIdePath = showDialogAndGetOldConfigPath(guessedOldConfigDirs.getPaths());
            importScenarioStatistics = SHOW_DIALOG_NO_CONFIGS_FOUND;
          }
        }
        else {
          Pair<Path, FileTime> bestConfigGuess = guessedOldConfigDirs.getFirstItem();
          if (isConfigOld(bestConfigGuess.second)) {
            log.info("The best config guess [" + bestConfigGuess.first + "] is too old, it won't be used for importing.");
            oldConfigDirAndOldIdePath = showDialogAndGetOldConfigPath(guessedOldConfigDirs.getPaths());
            importScenarioStatistics = SHOW_DIALOG_CONFIGS_ARE_TOO_OLD;
          }
          else {
            oldConfigDirAndOldIdePath = findConfigDirectoryByPath(bestConfigGuess.first);
            if (oldConfigDirAndOldIdePath == null) {
              logInfoAboutNotAcceptedConfigDirectory(log, "Previous config directory", bestConfigGuess.first);
              importScenarioStatistics = CONFIG_DIRECTORY_NOT_FOUND;
            }
          }
        }
      }
      Path oldConfigDir;
      if (oldConfigDirAndOldIdePath != null) {
        oldConfigDir = oldConfigDirAndOldIdePath.first;
        Path oldIdeHome = oldConfigDirAndOldIdePath.second;

        var configImportOptions = new ConfigImportOptions(log);
        configImportOptions.importSettings = settings;
        configImportOptions.mergeVmOptions = customMigrationOption instanceof CustomConfigMigrationOption.MergeConfigs;
        if (!guessedOldConfigDirs.fromSameProduct) {
          importScenarioStatistics = IMPORTED_FROM_OTHER_PRODUCT;
        }
        else if (importScenarioStatistics == null) {
          importScenarioStatistics = IMPORTED_FROM_PREVIOUS_VERSION;
        }

        System.setProperty(
          guessedOldConfigDirs.fromSameProduct ? CONFIG_IMPORTED_FROM_PREVIOUS_VERSION_KEY : CONFIG_IMPORTED_FROM_OTHER_PRODUCT_KEY,
          oldConfigDir.toString());

        doImport(oldConfigDir, newConfigDir, oldIdeHome, log, configImportOptions);

        setConfigImportedInThisSession();
      }
      else {
        oldConfigDir = null;
        log.info("No configs imported, starting with clean configs at " + newConfigDir);
        if (importScenarioStatistics == null) {
          importScenarioStatistics = CLEAN_CONFIGS;
        }
      }

      if (settings != null) {
        settings.importFinished(newConfigDir, otherProductPrefixes);
      }

      ImportOldConfigsState.Companion.getInstance().reportImportScenario(importScenarioStatistics);
      if (importScenarioStatistics == IMPORT_SETTINGS_ACTION && vmOptionsLines != null) {
        Path vmOptionsFile = newConfigDir.resolve(VMOptions.getFileName());
        try {
          Files.write(vmOptionsFile, vmOptionsLines, VMOptions.getFileCharset());
          vmOptionFileChanged = false;
        }
        catch (IOException e) {
          throw new RuntimeException(e);
        }
      } else {
        vmOptionFileChanged |= doesVmOptionsFileExist(newConfigDir);
      }
    }
    finally {
      if (tempBackup != null) {
        try {
          moveTempBackupToStandardBackup(newConfigDir, tempBackup);
        }
        catch (IOException e) {
          log.warn(String.format("Couldn't move the backup of current config from temp dir [%s] to backup dir", tempBackup), e);
        }
      }
    }

    // TODO remove hack, should we support vmoptions import in per project?
    // TODO If so, we need to patch restarter.
    if (vmOptionFileChanged && !ProjectManagerEx.IS_PER_PROJECT_INSTANCE_ENABLED) {
      log.info("The vmoptions file has changed, restarting...");
      if (settings == null || settings.shouldRestartAfterVmOptionsChange()) {
        writeOptionsForRestart(newConfigDir, log);
        restart(args);
      }
    }
  }
public static void importConfigsTo(boolean veryFirstStartOnThisComputer,
                                     @NotNull Path newConfigDir,
                                     @NotNull List<String> args,
                                     @NotNull Logger log)