module.exports = testDir

var fs = require("fs")
  , child_process = require("child_process")
  , path = require("path")
  , chain = require("slide").chain
  , TapStream = require("./tap-stream")

function testDir (dir, diag, cb) {
  if (typeof cb !== "function") cb = diag, diag = false
  var ts = new TapStream(diag)
  ts.on("error", cb)
  ts.on("end", cb)

  fs.readdir(dir, function (er, files) {
    if (er) return cb(er)
    chain(files.map(function (f) { return function (cb) {
      f = path.resolve(dir, f)
      fs.stat(f, function (er, st) {
        if (er) throw er
        if (st.isDirectory()) {
          ts.write({ name: f.substr(dir.length + 1)
                   , ok: false
                   , reason: "Nested dirs not yet supported" })
          return cb()
        }
        var cmd = f
          , args = []
        if (path.extname(f) === ".js") {
          cmd = "node"
          args = [f]
        }
        var cp = child_process.spawn(cmd, args)
          , out = ""
          , err = ""
        cp.stdout.on("data", function (c) { out += c })
        cp.stderr.on("data", function (c) { err += c })
        cp.on("exit", function (code) {
          if (ts.diag) ts.write(f)
          // TODO: If the output is TAP, then be smart with it.
          // If the output is a yamlish object, then parse that, too.
          ts.write({ name: f.substr(dir.length + 1)
                   , ok: !code
                   , output: out
                   , stderr: err })
          cb()
        })
      })
    }}), function (er) {
      if (er) ts.emit("error", er)
      ts.end()
    })
  })

  return ts
}

@@ -188,16 +188,60 @@
 stream%22)
+%0A  , TapConsumer = require(%22./tap-consumer%22)
 %0A%0Afuncti
@@ -377,16 +377,78 @@
 r%22, cb)%0A
+  ts.on(%22end%22, function () %7B%0A    console.error(%22TS END%22)%0A  %7D)%0A
   ts.on(
@@ -624,16 +624,17 @@
      fs.
+l
 stat(f, 
@@ -682,218 +682,8 @@
  er%0A
-        if (st.isDirectory()) %7B%0A          ts.write(%7B name: f.substr(dir.length + 1)%0A                   , ok: false%0A                   , reason: %22Nested dirs not yet supported%22 %7D)%0A          return cb()%0A        %7D
 %0A   
@@ -721,16 +721,17 @@
 gs = %5B%5D%0A
+%0A
         
@@ -817,16 +817,98 @@
       %7D%0A
+        if (st.isDirectory()) %7B%0A          return testDir(dir, diag, cb)%0A        %7D%0A
         
@@ -985,24 +985,173 @@
  , err = %22%22%0A
+          , tc = new TapConsumer%0A          , childTests = %5Bf%5D%0A%0A        tc.on(%22data%22, function (c) %7B childTests.push(c) %7D)%0A        cp.stdout.pipe(tc)%0A
         cp.s
@@ -1242,32 +1242,33 @@
 ) %7B err += c %7D)%0A
+%0A
         cp.on(%22e
@@ -1305,166 +1305,92 @@
     
-if (ts.diag) ts.write(f)%0A          // TODO: If the output is TAP, then be smart with it.%0A          // If the output is a yamlish object, then parse that, too.
+console.error(childTests)%0A          childTests.forEach(function (c) %7B ts.write(c) %7D)
 %0A   
@@ -1492,14 +1492,16 @@
     
-, outp
+//, stdo
 ut: 

test directories recursively, and parse tap output