 #!/usr/bin/env coffee
-
-fs = require 'fs'
+fs        = require 'fs'
 requisite = require('../lib')
 
-writeBundle = (bundle, opts) ->
-  if opts.output?
-    fs.writeFileSync opts.output, bundle.toString opts, 'utf8'
-  else
-    console.log bundle.toString opts
 
-help = (code, message) ->
+error = (message) ->
+  console.log message
+  process.exit 1
+
+
+help = ->
   console.log """
 
   Usage: requisite path/to/entry-module [options]

    when '-w', '--watch'
      opts.watch = true
    when '-h', '--help'
      help 0
    else
      help 1

if opts.watch and not opts.output?
  help 1, 'Output must be specified when using watch'

requisite.bundle entry, opts, (err, bundle) ->
  writeBundle bundle, opts

  if opts.watch
    requisite.watch bundle, (event, filename, mod) ->
      console.log "#{/\d{2}:\d{2}:\d{2}/.exec(new Date())[0]} - recompiling, #{filename} changed"
      writeBundle bundle, opts

    -v, --version                Display version
    -h, --help                   Display this help
  """
  process.exit 0


version = ->
  console.log require('../package').version
  process.exit 0


args = process.argv.slice 2
entry = args.shift()

opts =
  entry:   entry
  bare:    false
  include: []
  exclude: null
