'use strict';

module.exports = function(grunt) {

  grunt.initConfig({

    /**
     * Pull in the package.json file so we can read its metadata.
     */
    pkg: grunt.file.readJSON('package.json'),

    /**
     *
     *  Pull in environment-specific vars
     *
     */
    env: grunt.file.readJSON('config.json'),

    /**
     * Copy dependencies into static paths
     */
    copy: {
      main: {
        files: [
          {
            expand: true,
            flatten: true,
            src: ['node_modules/respond.js/dest/*'],
            dest: '<%= env.frontEndPath %>/js/built/lib/respond/',
            filter: 'isFile'
          }
        ]
      }
    },

    /**
     * https://github.com/gruntjs/grunt-contrib-sass
     */
    sass: {
        dev: {
            options: {
              style: 'expanded'
            },
            files: {
                '<%= env.frontEndPath %>/css/style.css': '<%= env.frontEndPath %>/css/scss/main.scss'
            }
        }
    },

    /**
     * https://github.com/gruntjs/grunt-contrib-less
     */
    less: {
        dev: {
            options: {
                paths: ['<%= env.frontEndPath %>/css/less'],
                compress: false,
                sourceMap: true,
                sourceMapFilename: '<%= env.frontEndPath %>/css/style.css.map',
                sourceMapBasepath: '<%= env.frontEndPath %>/css/less/',
                sourceMapURL: 'style.css.map'
            },
            files: {
                '<%= env.frontEndPath %>/css/style.css': '<%= env.frontEndPath %>/css/less/main.less'
            }
        }
    },

    /**
     * CSSMin: https://github.com/gruntjs/grunt-contrib-cssmin
     *
     * Minify CSS for production
     */
    cssmin: {
      target: {
        files: {
          '<%= env.frontEndPath %>/css/regulations.min.css': ['<%= env.frontEndPath %>/css/style.css']
        }
      }
    },
    /**
     * ESLint: https://github.com/sindresorhus/grunt-eslint
     *
     * Validate files with ESLint.
     */
    eslint: {
        target: [
          'Gruntfile.js',
          '<%= env.frontEndPath %>/js/source/*.js',
          '<%= env.frontEndPath %>/js/source/events/**/*.js',
          '<%= env.frontEndPath %>/js/source/models/**/*.js',
          '<%= env.frontEndPath %>/js/source/views/**/*.js'
        ]
    },

    /**
    * Browserify:
    *
    * Require('modules') in the browser/bundle up dependencies.
    */
    browserify: {
      dev: {
        files: {
          '<%= env.frontEndPath %>/js/built/regulations.js': ['<%= env.frontEndPath %>/js/source/regulations.js','<%= env.frontEndPath %>/js/source/regulations.js']
        },
        options: {
          transform: ['browserify-shim'],
          browserifyOptions: {
            debug: true
          }
        }
      },
      dist: {
        files: {
          '<%= env.frontEndPath %>/js/built/regulations.min.js': ['<%= env.frontEndPath %>/js/source/regulations.js']
        },
        options: {
          transform: ['browserify-shim'],
          browserifyOptions: {
            debug: true
          },
          plugin: [
            [function(b) {
              b.plugin('minifyify', {
                map: '/static/regulations/js/built/regulations.min.map',
                output: grunt.template.process('<%= env.frontEndPath %>/js/built/regulations.min.map')
              });
            }]
          ]
        }
      }
    },

    mocha_istanbul: {
      coverage: {
        src: ['<%= env.frontEndPath %>/js/unittests/specs/**/*.js'],
        options: {
          root: '<%= env.frontEndPath %>/js',
          istanbulOptions: ['--include-all-sources'],
          coverageFolder: '<%= env.frontEndPath %>/js/unittests/coverage',
          excludes: [
            'built/**/*',
            'unittests/**/*',
            'source/otherlibs/**/*'
          ],
          coverage: false
        }
      }
     },

    shell: {
      'nose-chrome': {
        command: 'nosetests -s <%= env.testPath %> --tc=remote:chrome --tc=testUrl:<%= env.testUrl %>',
        options: {
            stdout: true,
            stderr: true
        }
      },

      'nose-ie11': {
        command: 'nosetests -s <%= env.testPath %> --tc=remote:ie11 --tc=testUrl:<%= env.testUrl %>',
        options: {
            stdout: true,
            stderr: true
        }
      }
    },

    /**
     * Watch: https://github.com/gruntjs/grunt-contrib-watch
     *
     * Run predefined tasks whenever watched file patterns are added, changed or deleted.
     * Add files to monitor below.
     */
    watch: {
      js: {
        files: ['Gruntfile.js', '<%= env.frontEndPath %>/js/source/**/*.js'],
        tasks: ['eslint', 'browserify:dev']
      },
      css: {
        files: ['<%= env.frontEndPath %>/css/less/**/*.less'],
        tasks: ['less:dev']
      },
      options: {
        livereload: true
      }
    }
  });

  grunt.event.on('coverage', function( lcov, done ) {
    require('coveralls').handleInput( lcov, function( err ) {
      if ( err ) {
        return done( err );
      }
      done();
    });
  });
  /**
   * The above tasks are loaded here.
   */
  require('load-grunt-tasks')(grunt);

  /**
   * Create task aliases by registering new tasks
   */
  grunt.registerTask('nose', ['shell:nose-chrome', 'shell:nose-ie11']);
  grunt.registerTask('test', ['eslint', 'mocha_istanbul', 'nose']);
  grunt.registerTask('test-js', ['eslint', 'mocha_istanbul']);
  grunt.registerTask('build-dev', ['copy', 'browserify:dev', 'sass']);
  grunt.registerTask('build-dist', ['copy', 'browserify:dist', 'sass', 'cssmin']);
  grunt.registerTask('default', ['build-dist']);
};

@@ -992,622 +992,8 @@
 %7D,%0A%0A
-    /**%0A     * https://github.com/gruntjs/grunt-contrib-less%0A     */%0A    less: %7B%0A        dev: %7B%0A            options: %7B%0A                paths: %5B'%3C%25= env.frontEndPath %25%3E/css/less'%5D,%0A                compress: false,%0A                sourceMap: true,%0A                sourceMapFilename: '%3C%25= env.frontEndPath %25%3E/css/style.css.map',%0A                sourceMapBasepath: '%3C%25= env.frontEndPath %25%3E/css/less/',%0A                sourceMapURL: 'style.css.map'%0A            %7D,%0A            files: %7B%0A                '%3C%25= env.frontEndPath %25%3E/css/style.css': '%3C%25= env.frontEndPath %25%3E/css/less/main.less'%0A            %7D%0A        %7D%0A    %7D,%0A%0A
     

remove less action from gruntfile