 fs = require 'fs'
 path = require 'path'
 _ = require 'underscore'
+async = require './async'
 argv = require('optimist')
   .demand(['source'])
-  .boolean('w')
+  #.boolean('w')
   .usage('Usage: amdify [-w] -source <source_module_dir> -target <target_output_dir>')
   .argv
 

        else
          console.log "Saved to #{target}"
        if watch
          watcher.addFileMap parsed.scripts, ({event, file}) ->
            entry source, target, watch
    else if not nothing
      console.log if obj then parsed else parsed.serialize()


class Watcher extends EventEmitter
  constructor: () ->
    @inner = {}
  addFileMap: (scripts, onChange) ->
    # the goal is to determine whether something exists...
    # we also want to anything that
    @removeOldWatchers scripts
    helper = (script) =>
      console.log "[watch:add] #{script.name} => #{script.fullPath}"
      @inner[script.name] = fs.watch script.fullPath, (evt, fileName) =>
        console.log "[watch:#{evt}] #{script.name} => #{script.fullPath}"
        onChange {event: evt, file: script, fullPath: script.fullPath}
    for script in scripts
      helper script
    #async.forEach scripts, helper, cb
  removeOldWatchers: (scripts) ->
    for file, watcher of @inner
      watcher.close()
      delete @inner[file]

watcher = new Watcher()

entry = (opts) ->
  # remove the .extension? we'll figure this one out later...
  {source, target, obj, nothing, watch} = opts
  parseFile source, (err, parsed) ->
    if err
      console.log 'ERROR'
