package org.josast.ApplicationSatelliteDatabaseCli.cmd;

import java.io.File;
import java.util.logging.Logger;

import org.josast.ModuleSatelliteDatabase.UpdateAmsatListFile;

public class UpdateCmd implements Cmd {

    /**
     * logger.
     */
	private Logger log = Logger.getLogger(getClass().getName());

    /**
     *
     */
    @Override
    public void excute() {
        ParameterSingleton parameter = ParameterSingleton.getInstance();
        String inputRepository = parameter.getInputDirectory();
        String outputRepository = parameter.getOutputDirectory();
        File inputRepertoire = new File(inputRepository);
        File outputRepertoire = new File(outputRepository);

        int i = 0;
        if (!inputRepertoire.isDirectory()) {
            log.severe(inputRepository + "is not a repository");
        }
        if (!outputRepertoire.exists()) {
            log.info("create " + inputRepository);
            if (!outputRepertoire.mkdir()) {
                log.severe(outputRepertoire + "is not created");
            }
        } else if (!outputRepertoire.isDirectory()) {
            log.severe(outputRepertoire + "is not a repository");
        }

        File[] inputsfiles = inputRepertoire.listFiles();
        if (inputsfiles == null) {
            log.severe(inputRepository + "Repository empty");
        } else {
            for (i = 0; i < inputsfiles.length; i++) {

                UpdateAmsatListFile up = new UpdateAmsatListFile(
                        inputsfiles[i].getAbsolutePath());
                up.updateStatus();
                up.saveUpdateFile(
                        outputRepository + "\\" + inputsfiles[i].getName());
            }
        }

    }

}
