package org.webdsl.ant;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import org.apache.tools.ant.Task;

/**
 *  copies all sub folders(recursively) of Basedir that equals to parameter name and are not in the folder of Exclude
 */
public class TaskCopyAllDirsHavingName  extends Task {
    private String Basedir;
    private String Name;
    private ArrayList<String> Exclude;
    private String To;
    private String nameWindows;
    private String nameUNIX;
    private int numberOfFiles;

    public void setBasedir(String basedir) {
        Basedir = basedir;
    }

    public void setName(String name) {
        Name = name;
        nameWindows = name.replace('/', '\\');
        nameUNIX = name.replace('\\', '/');
    }

    public void setExclude(String exclude) {
        String[] elements = exclude.split(",");
        Exclude = new ArrayList<String>();
        for (String elem : elements){
            Exclude.add(elem.trim());
        }
    }

    public void setTo(String to) {
        To = to;
    }

    public void execute() {
        numberOfFiles = 0;
//        System.out.println(this.toString());
        try {
            findDirectoryAndCopy(new File(Basedir));
        } catch (Exception e) {
            System.out.println("Skipped copying " + Basedir + " to: " + To + " : " + e.getMessage());
        }
        if(numberOfFiles > 0){
        	System.out.println("Copied " + numberOfFiles + " modified files from directories named " + Name + " to " + To);
        }
    }

    @Override
    public String toString() {
        return "TaskCopyAllDirsHavingName [Basedir=" + Basedir + ", Name="
                + Name + ", Exclude=" + Exclude + ", To=" + To + "]";
    }

    private void findDirectoryAndCopy(File basedir) throws IOException {
        if(basedir.isDirectory()) {
            for(String file : basedir.list()) {
                File newFile = new File(basedir, file);
                if(Exclude.contains(file) || newFile.isHidden()) {
                    continue;
                }
                else if(newFile.getAbsolutePath().endsWith(nameUNIX) || newFile.getAbsolutePath().endsWith(nameWindows)) {
                    copyDirectory(newFile, new File (To));
                } else {
                    findDirectoryAndCopy(newFile);
                }
            }
        }
    }

    private void copyDirectory(File src, File dest) throws IOException {
        if(src.isDirectory()) {
            if(!dest.exists()) {
                 dest.mkdir();
            }

            for(String file : src.list()) {
                File from = new File(src, file);
                File to = new File(dest, file);
                copyDirectory(from, to);
            }
        } else {
        	if(dest.exists() && src.lastModified() <= dest.lastModified()){
        		return; //skip when files are the same
            } else {
	            copyFile(src, dest);
	            numberOfFiles ++;
            }
       }
    }

    private static void copyFile(File source, File dest) throws IOException {
        if(!dest.exists()) {
            dest.createNewFile();
        }
        FileChannel in = null;
        FileChannel out = null;
        try {
            in = new FileInputStream(source).getChannel();
            out = new FileOutputStream(dest).getChannel();
            out.transferFrom(in, 0, in.size());
        }
        finally {
            if(in != null) {
                in.close();
            }
            if(out != null) {
                out.close();
            }
        }
    }


}



@@ -1540,24 +1540,26 @@
 iles %3E 0)%7B%0D%0A
+//
         %09Sys
@@ -1664,24 +1664,137 @@
 o %22 + To);%0D%0A
+        %09System.out.println(%22Copied %22 + numberOfFiles + %22 files from directories named %22 + Name + %22 to %22 + To);%0D%0A
         %7D%0D%0A 
@@ -3056,32 +3056,34 @@
       %7D else %7B%0D%0A
+//
         %09if(dest
@@ -3136,24 +3136,26 @@
 dified())%7B%0D%0A
+//
         %09%09re
@@ -3192,16 +3192,18 @@
 e same%0D%0A
+//
         
@@ -3283,16 +3283,18 @@
 es ++;%0D%0A
+//
         
@@ -3895,32 +3895,90 @@
 %0A            %7D%0D%0A
+            dest.setLastModified(source.lastModified());%0D%0A
         %7D%0D%0A    %7D

Undo more efficient copying of resource files. It caused built-in resources to be kept while it should have been overwritten with equally named application-resources