package com.orange.clara.cloud.servicedbdumper.task.boot.sequences;

import com.orange.clara.cloud.servicedbdumper.exception.BootSequenceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

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


@Component
@Order(3)
public class BootSequenceBinaries implements BootSequence {
    @Value("classpath:binaries")
    protected File binariesPath;
    private Logger logger = LoggerFactory.getLogger(BootSequenceBinaries.class);

    @Override
    public void runSequence() throws BootSequenceException {
        String[] commands = {"/bin/chmod", "-R", "+x", binariesPath.getAbsolutePath()};
        logger.debug("Running command: " + String.join(" ", commands) + " ...");
        ProcessBuilder pb = new ProcessBuilder(commands);
        Process p = null;
        try {
            p = pb.start();
            p.waitFor();
        } catch (IOException | InterruptedException e) {
            throw new BootSequenceException("Error when booting: " + e.getMessage(), e);
        }
        logger.debug("Finished Command: " + String.join(" ", commands) + ".");
    }
}
