/*
 * grunt-asset-version-json
 * https://github.com/andyford/grunt-asset-version-json
 *
 * Copyright (c) 2013 Andy Ford
 * Licensed under the MIT license.
 */

'use strict';

var fs = require('fs')
  , path = require('path')
  , crypto = require('crypto');

module.exports = function(grunt) {

  // Please see the Grunt documentation for more information regarding task
  // creation: http://gruntjs.com/creating-tasks

  grunt.registerMultiTask('asset-version-json', 'Rename assets files with hash and store hashes in a JSON file', function() {
    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      punctuation: '.',
      separator: ', '
    });

    // Iterate over all specified file groups.
    this.files.forEach(function(f) {
      // Concat specified files.
      var src = f.src.filter(function(filepath) {
        // Warn on and remove invalid source files (if nonull was set).
        if (!grunt.file.exists(filepath)) {
          grunt.log.warn('Source file "' + filepath + '" not found.');
          return false;
        } else {
          return true;
        }
      }).map(function(filepath) {
        // Read file source.
        return grunt.file.read(filepath);
      }).join(grunt.util.normalizelf(options.separator));

      // Handle options.
      src += options.punctuation;

      // Write the destination file.
      grunt.file.write(f.dest, src);

      // Print a success message.
      grunt.log.writeln('File "' + f.dest + '" created.');
    });
  });

};

@@ -83,16 +83,78 @@
 json%0A *%0A
+ * Based on:%0A * https://github.com/hariadi/grunt-assets-wp%0A *%0A
  * Copyr
@@ -355,134 +355,8 @@
  %7B%0A%0A
-  // Please see the Grunt documentation for more information regarding task%0A  // creation: http://gruntjs.com/creating-tasks%0A%0A
   gr
@@ -379,33 +379,33 @@
 iTask('asset
--
+_
 version
--
+_
 json', 'Rena
@@ -481,94 +481,46 @@
 ) %7B%0A
-    // Merge task-specific and/or target-specific options with these defaults.
+%0A    var dest = this.data.dest
 %0A    
-var
+  ,
  opt
@@ -551,103 +551,170 @@
     
-punctuation: '.',%0A      separator: ', '%0A    %7D);%0A%0A    // Iterate over all specified file groups.
+    encoding: 'utf8',%0A          algorithm: 'md5',%0A          format: true,%0A          length: 4,%0A          rename: false,%0A          filetype: 'default'%0A        %7D);%0A
 %0A   
@@ -747,281 +747,394 @@
 on(f
+iles
 ) %7B%0A
+%0A
       
-// Concat specified files.%0A      var src = f.src.filter(function(filepath) %7B%0A        // Warn on and remove invalid source files (if nonull was set).%0A        if (!grunt.file.exists(filepath)) %7B%0A          grunt.log.warn('Source file %22' + filepath + '%22 not found.
+files.src.forEach(function (file) %7B%0A%0A        if (file.length === 0) %7B%0A          grunt.log.warn('src does not exist');%0A          return false;%0A        %7D%0A%0A        var basename = path.basename%0A          , name = basename(file)%0A          , content = grunt.file.read(file)%0A          , hash = crypto.createHash(options.algorithm).update(content, options.encoding).digest('hex
 ')
-;
 %0A   
@@ -1144,293 +1144,632 @@
     
-return false;%0A        %7D else %7B%0A          return true;%0A        %7D%0A      %7D).map(function(filepath) %7B%0A        // Read file source.%0A        return grunt.file.read(filepath);%0A      %7D).join(grunt.util.normalizelf(options.separator));%0A%0A      // Handle options.%0A      src += options.punctuation
+, jsoncontent = grunt.file.readJSON(dest)%0A          , suffix = hash.slice(0, options.length)%0A          , ext = path.extname(file)%0A          , newName = options.format ? %5Bsuffix, basename(file, ext), ext.slice(1)%5D.join('.') : %5Bbasename(file, ext), suffix, ext.slice(1)%5D.join('.');%0A%0A        // Copy/rename file base on hash and format%0A        var resultPath = path.resolve(path.dirname(file), newName);%0A        if (options.rename) %7B%0A          fs.renameSync(file, resultPath);%0A        %7D else %7B%0A          grunt.file.copy(file, resultPath);%0A        %7D%0A        grunt.log.writeln('  ' + file.grey + (' changed to ') + newName.green)
 ;%0A%0A 
@@ -1773,16 +1773,18 @@
 %0A%0A      
+  
 // Write
@@ -1788,36 +1788,109 @@
 ite 
-the destina
+new hashes to revs/hashes tracking JSON file%0A        jsoncontent%5Bop
 tion
- 
+s.
 file
-.%0A
+type%5D = suffix;%0A 
       
+ 
 grun
@@ -1906,57 +1906,55 @@
 ite(
-f.
 dest, 
-src);%0A%0A      // Print a success message.%0A
+JSON.stringify(jsoncontent, null, 2));%0A  
     
@@ -1978,39 +1978,105 @@
 ln('
-File %22
+  
 ' + 
-f.
 dest
- + '%22 created.'
+.grey + (' updated with with ' + options.filetype + ' hash: ') + suffix.green);%0A      %7D
 );%0A 
@@ -2088,12 +2088,11 @@
 ;%0A  %7D);%0A
-%0A
 %7D;%0A

replace default task with actual task