/*
 * Copyright
 *   2009 axYus - www.axyus.com
 *   2009 Christophe Marchand
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package fr.gouv.finances.cp.buildtools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import jonelo.jacksum.algorithm.AbstractChecksum;
import jonelo.jacksum.algorithm.MD;

/**
 * This class inspects a directory tree and put in each directory a file "1signer.key"
 * that contains a SHA1 of all files contained in the directory and all its sub-directories.
 * @author chm
 */
public class SetupSigner {
    private File initialDir = null;

    public static void main(String[] args) {
        if(args.length==0) throw new RuntimeException("Directory to sign should be given as argument");
        File dir = new File(args[0]);
        if(!dir.exists() || !dir.isDirectory()) throw new RuntimeException(dir.getAbsolutePath()+" does not exists or is not a directory.");
        SetupSigner signer = new SetupSigner(dir);
        signer.run();
    }

    public SetupSigner(File dir) {
        super();
        this.initialDir=dir;
    }

    public void run() {
        try {
            sign(initialDir);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

    protected void sign(File dir) throws NoSuchAlgorithmException, IOException {
        System.out.println("signing "+dir.getAbsolutePath());
        File signFile = new File(dir,"1signer.key");
        if(signFile.exists()) signFile.delete();
        AbstractChecksum checksum = new MD("SHA1");
        for(File f:dir.listFiles()) {
            if(f.equals(signFile)) continue;
            else if(f.isDirectory()) {
                sign(f);
                computeDirectoryCalculation(f,checksum);
            } else {
                computeCalculation(f,checksum);
            }
        }
        FileWriter writer = new FileWriter(signFile);
        writer.write(checksum.getFormattedValue());
        writer.flush(); writer.close();
    }

    protected void computeCalculation(File f, AbstractChecksum checksum) throws IOException {
        InputStream is = new FileInputStream(f);
        byte[] buffer = new byte[1024];
        int read = is.read(buffer);
        while (read > 0) {
            checksum.update(buffer, 0, read);
            read = is.read(buffer);
        }
        is.close();
    }

    protected void computeDirectoryCalculation(File dir, AbstractChecksum checksum) throws IOException {
        for(File f:dir.listFiles()) {
            if(f.isDirectory()) computeDirectoryCalculation(f, checksum);
            else if(f.getName().equals("1signer.key")) continue;
            else computeCalculation(f, checksum);
        }
    }
}
