module.exports = (grunt) ->
  'use strict'

  # Project configuration.
  grunt.initConfig
    pkg: grunt.file.readJSON 'package.json'

    coffeelint:
      options: grunt.file.readJSON('.coffeelint')
      all: ['{app,config,models,routes}/**/*.coffee', '*.coffee']

    # Before generating any new files, remove any previously-created files.
    clean:
      build:
        src: ['build']
      stylesheets:
        src: ['build/**/*.css']
      scripts:
        src: ['build/**/*.js']
      templates:
        src: ['build/**/*.hbs']

    copy:
      build:
        cwd: 'app'
        src: ['**', '!**/*.scss', '!**/*.coffee', '!**/*.jade']
        dest: 'build'
        expand: true

    express:
      options:
        opts: ['node_modules/coffee-script/bin/coffee']
        script: 'server.coffee'
      dev:
        options:
          node_env: 'development'
      prod:
        options:
          node_env: 'production'
      test:
        options:
          node_env: 'test'

    coffee:
      build:
        files: 'build/application.js': ['app/coffeescript/**/*.coffee']

    uglify:
      build:
        options:
          mangle: false
          sourceMap: true
          sourceMapIncludeSources: true
        files:
          'public/javascripts/application.js': ['build/**/*.js']

    sass:
      build:
        options:
          style: 'compressed'
        files: [
          'build/stylesheets/screen.css': ['app/scss/screen.scss'],
          'build/stylesheets/print.css': ['app/scss/print.scss'],
          'build/stylesheets/ie.css': ['app/scss/ie.scss']
        ]

    autoprefixer:
      build:
        expand: true
        cwd: 'build'
        src: ['**/*.css']
        dest: 'build'

    cssmin:
      build:
        files: [
          'public/stylesheets/screen.css': ['build/stylesheets/screen.css'],
          'public/stylesheets/print.css': ['build/stylesheets/print.css'],
          'public/stylesheets/ie.css': ['build/stylesheets/ie.css']
        ]

    emberTemplates:
      build:
        options:
          templateBasePath: 'app/templates/'
        files: 'build/templates.js': ['app/templates/**/*.hbs']

    # Watch for file changes.
    watch:
      options:
        livereload: true
      express:
        options:
          spawn: false
        files: ['*.coffee', '{config,models,routes}/**/*.coffee']
        tasks: ['express:dev']
      scripts:
        files: 'app/coffeescript/**/*.coffee'
        tasks: ['scripts']
      stylesheets:
        files: 'app/scss/**/*.scss'
        tasks: ['stylesheets']
      emberTemplates:
        files: 'app/templates/**/*.hbs'
        tasks: ['scripts']
      copy:
        files: 'app/**'
        tasks: ['copy']

    simplemocha:
      options:
        timeout: 3000
        ignoreLeaks: false
        ui: 'bdd'
        reporter: 'spec'
      all: ['spec/specHelper.coffee', 'spec/**/*Spec.coffee']

  # Load this project's task(s).
  grunt.loadTasks 'tasks'

  # Load third party task(s).
  grunt.loadNpmTasks 'grunt-autoprefixer'
  grunt.loadNpmTasks 'grunt-coffeelint'
  grunt.loadNpmTasks 'grunt-contrib-clean'
  grunt.loadNpmTasks 'grunt-contrib-coffee'
  grunt.loadNpmTasks 'grunt-contrib-concat'
  grunt.loadNpmTasks 'grunt-contrib-copy'
  grunt.loadNpmTasks 'grunt-contrib-cssmin'
  grunt.loadNpmTasks 'grunt-contrib-sass'
  grunt.loadNpmTasks 'grunt-contrib-uglify'
  grunt.loadNpmTasks 'grunt-contrib-watch'
  grunt.loadNpmTasks 'grunt-ember-templates'
  grunt.loadNpmTasks 'grunt-express-server'
  grunt.loadNpmTasks 'grunt-simple-mocha'

  # Compile JavaScripts.
  grunt.registerTask 'scripts', 'Compiles the JavaScripts.',
    ['coffee', 'emberTemplates', 'uglify', 'clean:templates']

  # Compile stylesheets.
  grunt.registerTask 'stylesheets', 'Compiles the stylesheets.',
    ['sass', 'autoprefixer', 'cssmin']

  # Compile static assets.
  grunt.registerTask 'build', 'Compiles all the assets and copies the files to the build directory.',
    ['clean:build', 'copy', 'scripts', 'stylesheets']

  # Run linting tasks on the application code.
  grunt.registerTask 'lint', 'Run linting tasks on the application.', 'coffeelint'

  # Run unit testing tasks on the application code.
  grunt.registerTask 'test', 'Run unit tests on the application.', 'simplemocha'

  # Run the tasks to build, spin up a server and watch
  grunt.registerTask 'reload', 'Watches the project for changes, automatically builds them and runs a server.',
    ['build', 'express:dev', 'watch']

  # Default task.
  grunt.registerTask 'default', ['lint', 'test']