package com.atlassian.bitbucket.scm.support;

import com.atlassian.stash.internal.ApplicationSettings;
import com.atlassian.stash.internal.home.HomeDirectoryDetails;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * This subclass exists solely delete the home directory when the {@code ApplicationContext} is shutdown.
 */
public class PlaceableApplicationSettings extends ApplicationSettings {
    
    private final File home;
    private final File sharedHome;

    public PlaceableApplicationSettings(File home, File sharedHome) {
        super(new HomeDirectoryDetails(home, sharedHome), null, null, "1");

        this.home = home;
        this.sharedHome = sharedHome;
    }

    /**
     * Recursively deletes the home directory, effectively deleting all of the repositories that were unpacked during
     * testing (as well as any other stray data that might have been created).
     *
     * @throws IOException Thrown if the recursive deletion of all files and directories in the home fails.
     */
    public void destroy() throws IOException {
        FileUtils.deleteDirectory(sharedHome);
        FileUtils.deleteDirectory(home);
    }
}
