package ca.junctionbox.cljbuck.io;

import ca.junctionbox.cljbuck.channel.Writer;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import static java.nio.file.FileVisitResult.CONTINUE;
import static java.nio.file.FileVisitResult.SKIP_SUBTREE;


public class PathTraversal extends SimpleFileVisitor<Path> {
    private final PathMatcher matcher;
    private Writer out;

    private PathTraversal(final String pattern, final Writer out) {
        this.out = out;
        this.matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
    }

    public static PathTraversal create(final String pattern, final Writer out) {
        return new PathTraversal(pattern, out);
    }

    void find(Path file) {
        Path name = file.getFileName();
        if (name != null && matcher.matches(file)) {
            out.write(file);
        }
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
        find(file);
        return CONTINUE;
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
        if (dir.getFileName().startsWith(".")) {
            return SKIP_SUBTREE;
        } else if (dir.getFileName().equals("clj-out")) {
            return SKIP_SUBTREE;
        } else if (dir.getFileName().equals("buck-out")) {
            return SKIP_SUBTREE;
        }
        find(dir);
        return CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) {
        System.err.println(exc);
        return CONTINUE;
    }
}

@@ -1302,20 +1302,33 @@
         
-if (
+final String d = 
 dir.getF
@@ -1337,16 +1337,43 @@
 eName().
+toString();%0A%0A        if (d.
 startsWi
@@ -1436,32 +1436,16 @@
 se if (d
-ir.getFileName()
 .equals(
@@ -1511,32 +1511,90 @@
 se if (d
-ir.getFileName()
+.equals(%22target%22)) %7B%0A            return SKIP_SUBTREE;%0A        %7D else if (d
 .equals(

Fix bug in PathTraversal.