   console.log '\n' + message if message
   process.exit code
 
-options =
-  bare: false
+opts =
+  bare:    false
+  include: []
   exclude: null
-  export: null
-  include: []
-  minify: false
-  output: null
+  export:  null
+  minify:  false
+  output:  null
   prelude: null
-  watch: false
+  watch:   false
 
 args = process.argv.slice 2
 

while opt = args.shift()
  switch opt
    when '-b', '--bare'
      options.bare = true
    when '-i', '--include'
      while (module = args.shift())? and module.charAt(0) != '-'
        try
          [requireAs, absolutePath] = module.split ':'
          options.include[requireAs] = absolutePath
        catch err
          help 1, 'Invalid argument to include'
    when '-x', '--exclude'
      options.exclude = new RegExp args.shift()
    when '-e', '--export'
      options.export = args.shift()
    when '-m', '--minify'
      options.minify = true
    when '-o', '--output'
      options.output = args.shift()
    when '-p', '--prelude'
      options.prelude = args.shift()
    when '-w', '--watch'
      options.watch = true
    when '-h', '--help'
      help 0
    else
      help 1

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

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

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

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) ->
  console.log """

  Usage: requisite path/to/entry-module [options]

  options:
    -b, --bare                   Compile without a top-level function wrapper
    -e, --export <name>          Export module as <name>
    -i, --include [module, ...]  Additional modules to include, in <require as>:<path to module> format
