package jetbrains.mps.baseLanguage.unitTest.execution.settings;

/*Generated by MPS */

import com.intellij.openapi.application.PathManager;
import java.io.File;
import jetbrains.mps.util.FileUtil;
import jetbrains.mps.baseLanguage.unitTest.execution.client.RunCachesManager;

public class DefaultSettingsPathChooser {
  private static final String BASE_NAME = "MPSJUnitRC";

  public String chooseDir() {
    for (int i = 0; i < 1000; ++i) {
      String defaultConfigPathFor = PathManager.getDefaultConfigPathFor(BASE_NAME);
      File newTestDir = new File(defaultConfigPathFor + i);
      if (checkDir(newTestDir)) {
        return newTestDir.getAbsolutePath();
      }
    }
    throw new IllegalStateException("Cannot find directory to write in");
  }

  private boolean checkDir(File dir) {
    return FileUtil.canWrite(dir) && !(RunCachesManager.isLocked(dir.getAbsolutePath()));
  }
}
