#!/usr/bin/env node

var exec = require('child_process').exec
  , path = require('path')
  , fs = require('fs')
  , tmp = require('tmp')
  , upload = require('./lib/upload').upload
  , uploadIfExists = require('./lib/upload').uploadIfExists
  , parallel = require('run-parallel')
  , series = require('run-series')
  , optimist = require('optimist')
  , url = require('url')
  , request = require('request')
  , screencapture = require('./lib/screencapture')

var argv = optimist
    .usage('Usage: $0')
    .describe('help', 'Print this')
    .describe('times', 'Screenshot N times and upload them all')
    .describe('quiet', 'Don\'t open an image in browser (Copy url only)')
    .describe('output', 'Write screenshot to an output file (also upload)')
    .alias('h', 'help')
    .alias('t', 'times')
    .alias('q', 'quiet')
    .alias('o', 'output')
    .argv

if (argv.help) {
  console.log(optimist.help())
  process.exit(0)
}

var stdinReadable
process.stdin.on('readable', function () {
  stdinReadable = true
})

function pbcopy(text, callback) {
  callback = callback || function () {}
  exec(['echo', '"' + text + '"' , '| pbcopy'].join(' '), callback)
}

function openURL(url) {
  if (argv.quiet) return

  exec('open ' + url)
}

function openURLs(urls) {
  if (argv.quiet) return

  exec('open ' + urls.join(' '))
}

function isURL(str) {
  return !!url.parse(str).host
}

function fetchURL(url, callback) {
  tmp.file(function (err, tmpPath) {
    var ws = fs.createWriteStream(tmpPath)
    request(url).pipe(ws)
    ws.on('finish', function () {
      callback(tmpPath)
    })
  })
}

// upload from url or path
var inputs = argv._
if (inputs.length) {
  parallel(
    inputs.map(function (input) {
      return function (callback) {
        if (isURL(input)) {
          fetchURL(input, function (imagePath) {
            upload(imagePath, callback)
          })

          return
        }

        uploadIfExists(path.resolve(process.cwd(), input), callback)
      }
    })
  , function (err, urls) {
    if (err) throw err

    pbcopy(urls.join('\\n'))
    openURLs(urls)
    process.exit(0)
  })

  return
}

// screencapture N times and upload
if (argv.times) {
  var times = argv.times
  var tasks = []
  for (var i = 0; i < times; i++) {
    tasks.push(function (callback) {
      screencapture(callback)
    })
  }

  series(tasks, function (err, paths) {
    if (err) throw err

    var uploadTasks = paths.map(function (path) {
      return function (callback) {
        upload(path, callback)
      }
    })

    parallel(uploadTasks, function (err, urls) {
      if (err) throw err

      pbcopy(urls.join('\\n'))
      openURLs(urls)
      process.exit(0)
    })
  })

  return
}

// upload from stdin
tmp.file(function (err, tmpPath) {
  var ws = fs.createWriteStream(tmpPath)
  process.stdin.pipe(ws)
  process.stdin.on('end', function () {
    upload(tmpPath, function (err, url) {
      pbcopy(url)
      openURL(url)
      process.exit(0)
    })
  })
})

if (!stdinReadable) return

var imagePath = null
if (argv.output) {
  imagePath = path.resolve(process.cwd(), argv.output)
}

screencapture(imagePath, function (err, imagePath) {
  upload(imagePath, function (err, url) {
    pbcopy(url)
    openURL(url)
    process.exit(0)
  })
})

@@ -529,24 +529,65 @@
 rint this')%0A
+    .describe('version', 'Show version')%0A
     .describ
@@ -639,16 +639,16 @@
 m all')%0A
-
     .des
@@ -813,16 +813,43 @@
 'help')%0A
+    .alias('v', 'version')%0A
     .ali
@@ -944,16 +944,16 @@
 help) %7B%0A
-
   consol
@@ -996,16 +996,106 @@
 t(0)%0A%7D%0A%0A
+if (argv.version) %7B%0A  console.log(require('./package.json').version)%0A  process.exit(0)%0A%7D%0A%0A
 var stdi

Add --version