/*global module:false*/

module.exports = function(grunt) {

  var marked = require('marked');
  var hl = require('node-syntaxhighlighter');

  marked.setOptions({
    highlight: function(code, lang) {
      lang = lang || 'javascript';
      lang = hl.getLanguage(lang);

      return hl.highlight(code, lang);
    },
    gfm: true
  });

  // Project configuration.
  grunt.initConfig({
    pluginName: 'smooth-scroll',
    pkg: grunt.file.readJSON('package.json'),
    meta: {
      banner: '/*!<%= "\\n" %>' +
          ' * <%= pkg.title %> - v<%= pkg.version %> - ' +
          '<%= grunt.template.today("yyyy-mm-dd")  + "\\n" %>' +
          '<%= pkg.homepage ? " * " + pkg.homepage + "\\n" : "" %>' +
          ' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>' +
          '<%= "\\n" %>' +
          ' * Licensed <%= pkg.license %>' +
          '<%= "\\n" %>' + ' */' +
          '<%= "\\n\\n" %>'
    },
    concat: {
      all: {
        src: ['src/jquery.<%= pluginName %>.js'],
        dest: 'jquery.<%= pluginName %>.js'
      },
      options: {
        stripBanners: true,
        banner: '<%= meta.banner %>',
        process: function(src) {
          var umdHead = grunt.file.read('lib/tmpl/umdhead.tpl');
          var umdFoot = grunt.file.read('lib/tmpl/umdfoot.tpl');

          src = src
          .replace('(function($) {', umdHead)
          .replace('})(jQuery);', umdFoot);

          return src;
        }
      }
    },
    uglify: {
      all: {
        files: {
          'jquery.<%= pluginName %>.min.js': ['<%= concat.all.dest %>']
        },
        options: {
          banner: '<%= meta.banner %>',
          // preserveComments: /\/\*[\s\S]*/
        }
      }
    },
    watch: {
      scripts: {
        files: '<%= jshint.all %>',
        tasks: ['jshint:all']
      },
      docs: {
        files: ['readme.md', 'lib/tmpl/**/*.html'],
        tasks: ['docs']
      }

    },
    jshint: {
      all: ['Gruntfile.js', 'src/**/*.js'],
      options: {
        curly: true,
        devel: true,
        eqeqeq: true,
        unused: true,
        immed: true,
        latedef: true,
        newcap: true,
        noarg: true,
        sub: true,
        undef: true,
        boss: true,
        eqnull: true,
        browser: true,
        globals: {
          jQuery: true,
          require: false
        }
      }
    },
    jscs: {
      src: 'src/**/*.js',
      options: {
        config: '.jscsrc',
        fix: true,
        verbose: true
      }
    },
    version: {

      files: {
        src: [
          'package.json',
          'src/jquery.<%= pluginName %>.js',
          'jquery.<%= pluginName %>.js'
        ]
      },
      banner: {
        src: ['jquery.<%= pluginName %>.js'],
        options: {
          prefix: '- v'
        }
      },

    }
  });

  grunt.registerTask('docs', 'Convert readme.md to html and concat with header and footer for index.html', function() {
    var readme = grunt.file.read('readme.md');
    var head = grunt.template.process(grunt.file.read('lib/tmpl/header.tpl'));
    var foot = grunt.file.read('lib/tmpl/footer.tpl');
    var doc = marked(readme);

    grunt.file.write('index.html', head + doc + foot);
  });

  grunt.registerTask('updateBower', 'Update bower.json to match package.json', function() {
    var pkg = require('./package.json');
    var props = ['name', 'main', 'homepage', 'repository', 'dependencies', 'keywords', 'license'];
    var json = {
      description: 'Easy implementation of smooth scrolling for same-page links'
    };

    props.forEach(function(item) {
      if (pkg[item]) {
        json[item] = pkg[item];
      }
    });

    json.authors = [pkg.author];
    json.moduleType = ['amd', 'node'];
    json.ignore = ['demo/', 'lib/', 'src/', 'test/', '**/.*', 'Gruntfile.js', 'package.json'];

    grunt.file.write('bower.json', JSON.stringify(json, null, 2));
  });

  grunt.registerTask('lint', ['jshint', 'jscs']);
  grunt.registerTask('build', ['lint', 'concat', 'version', 'uglify', 'docs']);
  grunt.registerTask('patch', ['lint', 'concat', 'version::patch', 'uglify']);
  grunt.registerTask('default', ['build']);

  grunt.loadNpmTasks('grunt-jscs');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-version');
};

@@ -53,16 +53,64 @@
 runt) %7B%0A
+  var pkg = grunt.file.readJSON('package.json');
 %0A  var m
@@ -476,43 +476,11 @@
 kg: 
-grunt.file.readJSON('package.json')
+pkg
 ,%0A  
@@ -2554,20 +2554,17 @@
 : %7B%0A
-%0A
       
-files
+src
 : %7B%0A
@@ -2581,163 +2581,153 @@
 c: %5B
-%0A          'package.json',%0A          'src/jquery.%3C%25= pluginName %25%3E.js',%0A          'jquery.%3C%25= pluginName %25%3E.js'%0A        %5D%0A      %7D,%0A      banner: %7B%0A
+'src/jquery.%3C%25= pluginName %25%3E.js'%5D%0A      %7D,%0A      banners: %7B%0A        pkg: pkg,%0A        src: %5B%0A          'jquery.%3C%25= pluginName %25%3E.js',%0A  
         
 src:
@@ -2722,22 +2722,16 @@
         
-src: %5B
 'jquery.
@@ -2740,35 +2740,48 @@
 = pluginName %25%3E.
-js'
+min.js'%0A        
 %5D,%0A        optio
@@ -2829,16 +2829,71 @@
      %7D,%0A
+      package: %7B%0A        src: %5B'package.json'%5D%0A      %7D,
 %0A    %7D%0A 
@@ -4135,106 +4135,241 @@
 sk('
-patch', %5B'lint', 'concat', 'version::patch', 'uglify'%5D);%0A  grunt.registerTask('default', %5B'build'%5D
+default', %5B'build'%5D);%0A%0A  %5B'patch', 'minor', 'major'%5D.forEach(function(release) %7B%0A    grunt.registerTask(release, %5B'lint', 'version:src:' + release, 'concat', 'uglify', 'version:banners:' + release, 'version:package:' + release%5D);%0A  %7D
 );%0A%0A

fix grunt-version config so everything gets correct version.