#!/usr/bin/env node

var argv = process.argv.slice(2)
  , path = require("path")
  , Runner = require("../lib/tap-runner")

  , nopt = require("nopt")

  , knownOpts =
    { cover: [path, false]
    , "cover-dir": path
    , stderr: Boolean
    , stdout: Boolean
    , diag: Boolean
    , version: Boolean
    , tap: Boolean
    , timeout: Number
    }

  , shorthands =
    // debugging 1: show stderr
    { d: ["--stderr"]
    // debugging 2: show stderr and tap
    , dd: ["--stderr", "--tap"]
    // debugging 3: show stderr, tap, AND always show diagnostics.
    , ddd: ["--stderr", "--tap", "--diag"]
    , e: ["--stderr"]
    , t: ["--timeout"]
    , o: ["--tap"]
    , c: ["--cover"]
    , v: ["--version"]
    }

  , defaults =
    { cover: "./lib"
    , "cover-dir": "./coverage"
    , stderr: process.env.TAP_STDERR
    , tap: process.env.TAP
    , diag: process.env.TAP_DIAG
    , timeout: +process.env.TAP_TIMEOUT || 30
    , version: false }

  , options = nopt(knownOpts, shorthands)

if (options.version) {
  console.log(require("../package.json").version)
  process.exit(0)
}

Object.keys(defaults).forEach(function (k) {
  if (!options.hasOwnProperty(k)) options[k] = defaults[k]
})

// other tests that might rely on these
if (options.diag) process.env.TAP_DIAG = true
if (options.tap) process.env.TAP = true
if (options.timeout) process.env.TAP_TIMEOUT = options.timeout

var r = new Runner(options)
  , TapProducer = require("../lib/tap-producer")

if (options.tap || options.diag) {
  r.pipe(process.stdout)
} else {
  r.on("file", function (file, results, details) {
    var s = (details.ok ? "" : "not ") + "ok "+results.name
      , n = details.pass + "/" + details.testsTotal
      , dots = new Array(Math.max(1, 60 - s.length - n.length)).join(".")
    console.log("%s %s %s", s, dots, n)
    if (details.ok) {
      if (details.skip) {
        console.log("  skipped: %s", details.skipTotal)
      }
    } else {
      // console.error(details)
      console.log("    Command: %s", results.command)
      console.log("    " + TapProducer.encode(details.list)
                  .split(/\n/).join("\n    "))
    }
  })
  r.on("end", function () {
    //console.log(r)
    var s = "total"
      , n = r.results.pass + "/" + r.results.testsTotal
      , dots = new Array(60 - s.length - n.length).join(".")
      , ok = r.results.ok ? "ok" : "not ok"
    console.log("%s %s %s\n\n%s", s, dots, n, ok)
    if (r.doCoverage) {
      console.error( "\nCoverage: %s\n"
                   , path.resolve(r.coverageOutDir, "index.html") )
    }
  })
}



r.on("end", function () {
  process.exit(r.results.tests - r.results.pass)
})

@@ -708,22 +708,64 @@
 rsion%22%5D%0A
+    , %22?%22: %5B%22--help%22%5D%0A    , h: %5B%22--help%22%5D%0A
     %7D%0A
-
 %0A  , def
@@ -988,16 +988,34 @@
 n: false
+%0A    , help: false
  %7D%0A%0A  , 
@@ -1144,19 +1144,698 @@
 exit(0)%0A
-
 %7D%0A%0A
+if (options.help) %7B%0A  console.log(function()%7B/*%0A%0AUsage:%0A    tap %3Coptions%3E %3Cfiles%3E%0A%0A    Run the files as tap tests, parse the output, and report the results%0A%0AOptions:%0A%0A    --stderr    Print standard error output of tests to standard error.%0A    --tap       Print raw tap output.%0A    --diag      Print diagnostic output for passed tests, as well as failed.%0A                (Implies --tap)%0A    --timeout   Maximum time to wait for a subtest, in seconds. Default: 30%0A    --version   Print the version of node tap%0A    --help      Print this help%0A%0APlease report bugs!  https://github.com/isaacs/node-tap/issues%0A%0A*/%7D.toString().split(/%5Cn/).slice(1, -1).join(%22%5Cn%22))%0A  process.exit(0)%0A%7D%0A%0A%0A
 Object.k

Fix #32 Add 'tap --help'