 url = require 'url'
 colors = require 'colors'
 minimatch = require 'minimatch'
+
+# options passed to minimatch for ignore and plugin matching
+minimatchOptions =
+  dot: false
 
 class ContentPlugin
   @property = (name, getter) ->

  tree = new ContentTree env, reldir

  env.logger.verbose "creating content tree from #{ directory }"

  # options passed to minimatch for ignore and plugin matching
  minimatchOptions =
    dot: false

  readDirectory = (callback) ->
    fs.readdir directory, callback

    else
      callback null, filenames

  createInstance = (filepath, callback) ->
    ### Create plugin or subtree instance for *filepath*. ###
    setImmediate ->
      async.waterfall [
        async.apply fs.lstat, filepath.full
        (stats, callback) ->
          basename = path.basename filepath.relative

          # recursively map directories to content tree instances
          if stats.isDirectory()
            ContentTree.fromDirectory env, filepath.full, (error, result) ->
              result.parent = tree
              tree[basename] = result
              tree._.directories.push result # add instance to the directory group of its parent
              callback error

          # map files to content plugins
          else if stats.isFile()
            loadContent env, filepath, (error, instance) ->
              if not error
                instance.parent = tree
                tree[basename] = instance
                tree._[instance.__plugin.group].push instance
              callback error

          # This should never happen™
          else
            callback new Error "Invalid file #{ filepath.full }."
      ], callback

  createInstances = (filenames, callback) ->
    # NOTE: the file limit is not really enforced here since this is a recursive function
    #       but won't be a problem in 99% of cases, patches welcome :-)
    # TODO: actually this has to be fixed, we want an error to stop instance creation
    async.forEachLimit filenames, env.config._fileLimit, createInstance, callback

  async.waterfall [
