module.exports = function(grunt) {

  grunt.initConfig({
    jshint: {
      config: {
        options: {jshintrc: '.jshintrc'},
        src: ['Gruntfile.js', 'config/**/*.js'],
      },
      app: {
        options: {jshintrc: 'app/.jshintrc'},
        src: ['app/**/*.js'],
      },
    },
    jade: {
      options: {
        data: {
          config: require('./config/app'),
          target: '<%= grunt.task.current.target %>',
        },
      },
      dev: {
        expand: true,
        cwd: 'app/pages',
        src: '*.jade',
        dest: 'build/wwwroot',
        ext: '.html',
      },
      prod: '<%= jade.dev %>',
    },
    handlebars: {
      options: {
        amd: true,
        processName: function(name) {
          return require('path').basename(name, '.hbs');
        },
      },
      templates: {
        src: 'app/templates/*.hbs',
        dest: 'build/templates.js',
      },
    },
    stylus: {
      dev: {
        options: {compress: false},
        files: [{src: 'app/css/app.styl', dest: 'build/wwwroot/app.css'}],
      },
      prod: {
        files: '<%= stylus.dev.files %>',
      },
    },
    requirejs: {
      prod: {
        options: {
          baseUrl: '.',
          mainConfigFile: 'app/config/requirejs.js',
          name: 'components/almond/almond',
          out: 'build/wwwroot/app.js',
          optimize: 'uglify2',
          generateSourceMaps: true,
          preserveLicenseComments: false,
        },
      },
    },
    fix_sourcemaps: {
      prod: ['build/wwwroot/app.js.map']
    },
    connect: {
      dev: {
        options: {
          middleware: function(connect, options) {
            return [
              // Serve files in /build/wwwroot as if they were in the root.
              connect.static(__dirname + '/build/wwwroot'),
              // But serve everything else from the root.
              connect.static(__dirname),
            ];
          },
        },
      },
      prod: {
        options: {
          base: 'build/wwwroot',
          keepalive: true,
        },
      },
    },
    watch: {
      options: {
        livereload: true,
      },
      jshint: {
        files: ['<%= jshint.config.src %>', '<%= jshint.app.src %>'],
        tasks: ['jshint']
      },
      jade: {
        files: ['app/pages/*.jade', 'config/config.json'],
        tasks: ['jade:dev'],
      },
      handlebars: {
        files: ['<%= handlebars.templates.src %>'],
        tasks: ['handlebars'],
      },
      stylus: {
        files: ['<%= stylus.dev.files[0].src %>'],
        tasks: ['stylus:dev'],
      },
    },
    'gh-pages': {
      site: {
        options: {
          base: 'build/wwwroot',
          clone: 'build/gh-pages',
        },
        src: ['**/*'],
      },
    },
  });

  grunt.registerMultiTask('fix_sourcemaps', 'Fix sourcemaps generated by requirejs task.', function() {
    this.filesSrc.forEach(function(mapfile) {
      var data = grunt.file.readJSON(mapfile);
      // Sources should be relative to the project root, but are actually
      // relative to where app.js lives. I'm not sure how to change this,
      // other than to rewrite ../ and ../../ (and ../ times 8 for !json??)
      data.sources = data.sources.map(function(filepath) {
        return filepath.replace(/^(\.\.\/)+/, function(parents) {
          var depth = parents.match(/\.\.\//g).length - 1;
          return ['build/', ''][depth] || '';
        });
      });
      grunt.file.write(mapfile, JSON.stringify(data));
    });
  });

  grunt.loadNpmTasks('grunt-contrib-requirejs');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-jade');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-stylus');
  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-contrib-handlebars');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-gh-pages');

  grunt.registerTask('build',
    'Build site files for testing or deployment.',
    ['jshint', 'jade:prod', 'handlebars', 'requirejs:prod', 'fix_sourcemaps', 'stylus:prod']);

  grunt.registerTask('deploy',
    'Deploy site via gh-pages.',
    ['build', 'gh-pages']);

  grunt.registerTask('dev',
    'Start a live-reloading dev webserver on localhost.',
    ['jshint', 'jade:dev', 'handlebars', 'stylus:dev', 'connect:dev', 'watch']);

  grunt.registerTask('prod',
    'Publish to build/wwwroot and start a webserver on localhost.',
    ['build', 'connect:prod:keepalive']);

  grunt.registerTask('default', ['dev']);

};

@@ -2312,19 +2312,12 @@
 fig/
-config.json
+**/*
 '%5D,%0A

Rebuild Jade templates when any config files change.