/*
 * grunt-cv
 * https://github.com/whowgames/grunt-cv
 *
 * Copyright (c) 2015 Marko Kercmar
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {

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

  grunt.registerMultiTask('cv', 'Appends __cv hash on all IMG URLs in CSS', function() {
    var _fs = require('fs');
    var _crypto = require('crypto');
    var _uri = require('uri-js');

    var _md5 = function(str) {
        return _crypto
            .createHash('md5')
            .update(str)
            .digest('hex');
    };

    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
        docroot: './',
    });

    // 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)) {
                return false;
            }

            var source = grunt.file.read(filepath);
		
            var pattern = /url\(.*?['"]{0,1}\)/ig

            var matches = source.match(pattern);

            for (var i in matches) {
                var match = matches[i];

                if (match.indexOf('?') === 1) continue;

                var clean = match.replace('url(', '').replace(')', '').replace("'", '').replace("'", '').replace('"', '').replace('"', '');
                var filepath = _uri.normalize(clean);
                var filepath_fs = options.docroot + filepath;

                if (filepath.charAt(0) != '/') filepath = '/' + filepath;

                if (_fs.existsSync(filepath_fs)) {
                    var data = _fs.readFileSync(filepath_fs);
                    var hash = _md5(data);
                    hash = hash.substr(0, 30) + "00";

                    source = source.replace(match, "url('" + filepath + "?__cv=" + hash + "')");

                    //console.log(match);
                    //console.log("url('" + filepath + "?__cv=" + hash + "')");
                } else {
                    //console.log(match);
                }
            }

            grunt.file.write(f.dest, source);
        
            grunt.log.writeln('File "' + f.dest + '" written.');
        });
    });
  });
};

@@ -1181,10 +1181,8 @@
 h);%0A
-%09%09
 %0A   
@@ -1360,64 +1360,391 @@
 i%5D;%0A
-%0A                if (match.indexOf('?') === 1) continue;
+                var orig = match;%0A%0A                //grunt.log.writeln(match);%0A%0A                if (orig.match(/%5C?__cv=%5Ba-z0-9%5D%7B32%7D/ig) && orig.indexOf('&') %3C 0) %7B%0A                    match = orig.substring(0, orig.indexOf('?')) + orig.substring(orig.indexOf('?')+38);%0A                %7D%0A%0A                if (match.indexOf('?') === 1) %7B%0A                    continue;%0A                %7D
 %0A%0A  
@@ -2328,21 +2328,20 @@
 replace(
-match
+orig
 , %22url('
@@ -2403,33 +2403,38 @@
       //
-console.log(match
+grunt.log.writeln(orig
 );%0A     
@@ -2450,27 +2450,33 @@
       //
-console.log
+grunt.log.writeln
 (%22url('%22
@@ -2565,19 +2565,25 @@
   //
-console.log
+grunt.log.writeln
 (mat
@@ -2666,24 +2666,16 @@
 ource);%0A
-        
 %0A       

clean out existing __cv tags before putting new ones in..