/*
 * CheckConfigFile.java
 *
 * Created on 6 novembre 2007, 11:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


package fr.gouv.finances.cp.buildtools.chkconfig;

import fr.gouv.finances.cp.xemelios.common.config.DocumentModel;
import fr.gouv.finances.cp.xemelios.common.config.DocumentsModel;
import fr.gouv.finances.cp.xemelios.common.config.DocumentsParser;
import java.io.File;

/**
 * Effectue tous les controles necessaires sur le fichier de configuration pass� en param�tre.
 * Si il y a une erreur, le code de sortie est different de 0, ce qui peut �tre intercept� par le script de build.
 * @author chm
 */
public class CheckConfigFile {
    private String fileName;
    
    /** Creates a new instance of CheckConfigFile */
    public CheckConfigFile(String fileName) {
        super();
        this.fileName=fileName;
    }
    
    public static void main(String[] args) {
        if(args.length==0) {
            showSyntax();
            System.exit(1);
        }
        CheckConfigFile ccf = new CheckConfigFile(args[0]);
        ccf.runChecks();
    }
    
    protected void runChecks() {
        try {
            DocumentsParser dp = new DocumentsParser();
            File f = new File(fileName);
            File dir = f.getParentFile();
            String repertoire = dir.getAbsolutePath();
            dp.parse(fileName);
            DocumentsModel dm = (DocumentsModel)dp.getMarshallable();
            for(DocumentModel dom:dm.getDocuments()) {
                dom.setBaseDirectory(dir.getAbsolutePath());
                dom.validate();
            }
        } catch(Exception ex) {
            System.err.println(ex.getMessage());
            System.exit(2);
        }
    }
    
    public static void showSyntax() {
        StringBuilder sb = new StringBuilder();
        sb.append("java ").append(CheckConfigFile.class.getName()).append(" <configFile>\n");
        sb.append("\twhere <configFile> is an absolute path name to a config file to test.\n");
    }
}
