package com.atlassian.bitbucket.mesh.app;

import com.atlassian.bitbucket.mesh.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.nio.file.Path;

import static com.atlassian.bitbucket.mesh.util.ResourceUtils.unpackTar;
import static java.util.Objects.requireNonNull;

public class HomeInitializer {

    private static final Logger log = LoggerFactory.getLogger(HomeInitializer.class);

    public HomeInitializer(@Nonnull Path homeDir) {
        requireNonNull(homeDir, "homeDir");

        log.debug("Initializing home directory {} with bundled scripts", homeDir);
        try {
            unpackTar("/templates/home.tar", homeDir);
        } catch (IOException e) {
            throw new HomeInitializationFailedException(
                    "The home directory cannot be initialized for use by " + Product.NAME,
                    "Please check the permissions on " + homeDir,
                    e);
        }
    }
}
