package tasks.bench;

import java.io.File;

import main.Config;

/**
 * Prepares the directories before cloning the repositories
 * 
 * @author Lisa Nguyen Quang Do
 */

public class Setup {

	private final String id;

	public Setup(String id) {
		this.id = id;
	}

	public File createSetup() {
		File parentDir = new File(Config.workspace);
		File projectDir = new File(Config.workspace + "\\" + id);
		File tmpDir = new File(Config.tmpDir);
		emptyDirectory(tmpDir);
		emptyDirectory(parentDir);
		try {
			tmpDir.mkdir();
			parentDir.mkdir();
			projectDir.mkdir();
		} catch (SecurityException e) {
			e.printStackTrace();
			return null;
		}
		return projectDir;
	}

	private void emptyDirectory(File projectDir) {
		if (projectDir.isDirectory()) {
			File[] children = projectDir.listFiles();
			for (int i = 0; i < children.length; i++) {
				emptyDirectory(children[i]);
			}
		}
		projectDir.delete();
	}
}
