module.exports = (grunt) ->

  path = require 'path'

  # to preserve directory structure
  coffeeLib2SrcDest = grunt.file.expandMapping '**/*.coffee', 'lib/',
    cwd: 'src/'
    ext: '.js'
    rename: (dest, matchedSrcPath) -> path.join dest, matchedSrcPath

  grunt.initConfig

    coffee:
      options:
        bare: true
      all:
        files: coffeeLib2SrcDest.concat [
          expand: true
          src: 'test_app/**/*.coffee'
          ext: '.js'
        ,
          expand: true
          src: 'test/**/*.coffee'
          ext: '.js'
        ]

    simplemocha:
      all:
        src: [
          'node_modules/should/lib/should.js'
          'test/**/*.js'
        ]

    watch:
      coffee:
        files: [
          'src/**/*.coffee'
          'test/**/*.coffee'
          'test_app/**/*.coffee'
        ]
        tasks: 'coffee'

      simplemocha:
        files: [
          'lib/**/*.js'
          'test/**/*.js'
        ]
        tasks: 'simplemocha'

    esteDeps:
      options:
        depsWriterPath: 'node_modules/google-closure-library/closure/bin/build/depswriter.py'
      testApp:
        options:
          outputFile: 'test_app/assets/deps.js'
          prefix: '../../../../'
          root: [
            'node_modules/google-closure-library'
            'test_app/js'
          ]

    esteBuilder:
      options:
        closureBuilderPath: 'node_modules/google-closure-library/closure/bin/build/closurebuilder.py'
        compilerPath: 'node_modules/google-closure-compiler/compiler.jar'
        namespace: 'app.start'
        compilerFlags: [
          '--output_wrapper="(function(){%output%})();"'
          '--compilation_level="ADVANCED_OPTIMIZATIONS"'
          '--warning_level="VERBOSE"'
        ]
      testApp:
        options:
          root: [
            'node_modules/google-closure-library'
            'test_app/js'
          ]
          outputFilePath: 'test_app/assets/app.js'
          depsPath: 'test_app/assets/deps.js'

    release:
      options:
        bump: true
        add: true
        commit: true
        tag: true
        push: true
        pushTags: true
        npm: true

  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.loadNpmTasks 'grunt-contrib-watch'
  grunt.loadNpmTasks 'grunt-este-oldschool'
  grunt.loadNpmTasks 'grunt-release'
  grunt.loadNpmTasks 'grunt-simple-mocha'

  grunt.registerTask 'default', [
    'build', 'run'
  ]

  grunt.registerTask 'build', [
    'coffee', 'simplemocha'
  ]

  grunt.registerTask 'run', [
    'watch'
  ]

  grunt.registerTask 'test', [
    'build', 'buildTestApp'
  ]

  grunt.registerTask 'buildTestApp', [
    'coffee', 'coffee2closure', 'esteDeps:testApp', 'esteBuilder:testApp'
  ]

  grunt.registerTask 'coffee2closure', ->
    coffee2closure = require './lib/coffee2closure'
    for path in grunt.file.expand 'test_app/js/*.js'
      src = grunt.file.read path
      src = coffee2closure.fix src
      grunt.file.write path, src
      grunt.log.writeln "File #{path} fixed."