module.exports = (grunt) ->
  fs = require("fs")

  exec_conf = {
    all:
      command: "mocha test/* --compilers coffee:coffee-script --require should"
  }

  files = fs.readdirSync("test")
  for file in files
    if file.match /\.coffee$/
      name = file.replace /\.coffee$/, ""
      exec_conf[name] = {command: "mocha test/#{file} --compilers coffee:coffee-script --require should"}

  grunt.initConfig
    pkg: grunt.file.readJSON("package.json")
    exec: exec_conf
    watch:
      files: ["src/**/*.coffee"]
      tasks: ["build"]

  grunt.loadNpmTasks "grunt-contrib-coffee"
  grunt.loadNpmTasks "grunt-contrib-watch"
  grunt.loadNpmTasks "grunt-exec"


  grunt.registerTask "test", "Run tests", () ->
    if name = grunt.option("test")
      grunt.task.run "exec:#{name}"
    else
      grunt.task.run "exec:all"

  grunt.registerTask "build", "Builds preproc to js", ->
    Builder = require("./src/builder")
    builder = new Builder();
    builder.setEnv {
      building: true
    }
    builder.build("src/preproc.coffee", "dist/preproc.js")
