/*
 * grunt-livereload-snippet
 * 
 *
 * Copyright (c) 2013 timdouglas
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function(grunt) {
  grunt.registerMultiTask('livereload_snippet', 'Add livereload js snippet to html', function() {
    var fs = require('fs'),
      path = require('path'),
      done = this.async(),
      options = this.options({
        port: 35729,
        hostname: 'localhost',
        file: 'index.html',
        add: true,
        after: undefined,
        before: undefined
      }),
      file = path.resolve(options.file),
      contents,
      res,
      snippet_regex = /<script src\=\"http\:\/\/.*\:[0-9]+\/livereload\.js\?snipver\=1\"\><\/script>/gi,
      snippet = '<script src="http://'+options.hostname+':'+options.port+'/livereload.js?snipver=1"></script>';

    //check the file is there...
    fs.exists(file, function(exists) {

      if(exists === true) {
        // get file contents
        contents = grunt.file.read(file);

        if(contents === undefined) {
          grunt.warn("could not open file "+file);
          done(false);
        }
        
        if(contents.length === 0 && options.add == false) {
          grunt.log.writeln(file+" is empty, done.");
          done();
        }

        //remove any existing snippets
        contents = contents.replace(snippet_regex, '');

        //add snippet if allowed...
        if(options.add === true) {
          if(options.after !== undefined) {
            contents = contents.replace(new RegExp(options.after, "g"), options.after + snippet);
          } else if(options.before !== undefined) {
            contents = contents.replace(new RegExp(options.before, "g"), snippet + options.before);
          } else {
            contents += snippet;
          }
        }

        var res = grunt.file.write(file, contents);

        if(res) {
          if(options.add === true) {
            grunt.log.writeln("Added livereload snippet to "+file);
          } else {
            grunt.log.writeln("Remove livereload snippet from "+file);
          }

          done();
        }
        else {
          grunt.warn("could not write to file "+file);
          done(false);
        }
      } else {
        grunt.warn("file "+file+" does not exist");
        done(false);
      }
    });
  });
};

@@ -314,35 +314,8 @@
 '),%0A
-      done = this.async(),%0A
     
@@ -397,36 +397,8 @@
 t',%0A
-        file: 'index.html',%0A
     
@@ -484,49 +484,24 @@
     
-file = path.resolve(options.file)
+contents
 ,%0A      
 cont
@@ -488,39 +488,34 @@
 contents,%0A      
-content
+re
 s,%0A      res,%0A  
@@ -499,35 +499,43 @@
      res,%0A      
-res
+err = false
 ,%0A      snippet_
@@ -562,14 +562,8 @@
 %5C=%5C%22
-http%5C:
 %5C/%5C/
@@ -653,13 +653,8 @@
 rc=%22
-http:
 //'+
@@ -731,104 +731,331 @@
 ';%0A%0A
+%0A
     
-//check the file is there...%0A    fs.exists(file, function(exists) %7B%0A%0A      if(exists === tru
+this.files.forEach(function(f) %7B%0A      //filter out missing files%0A      var src = f.src.filter(function(path) %7B%0A        if (!grunt.file.exists(path)) %7B%0A          grunt.log.warn('Source file %22' + path + '%22 not found.');%0A          return false;%0A        %7D else %7B%0A          return true;%0A        %7D%0A      %7D).map(function(fil
 e) %7B
@@ -1065,17 +1065,16 @@
       //
- 
 get file
@@ -1137,24 +1137,25 @@
      if(
+!
 contents
  === und
@@ -1150,22 +1150,8 @@
 ents
- === undefined
 ) %7B%0A
@@ -1215,170 +1215,41 @@
     
-done(false);%0A        %7D%0A        %0A        if(contents.length === 0 && options.add == false) %7B%0A          grunt.log.writeln(file+%22 is empty, done.%22);%0A          done()
+err = true;%0A          return true
 ;%0A  
@@ -1494,33 +1494,32 @@
 ts = contents.re
-p
 lace(new RegExp(
@@ -2073,27 +2073,8 @@
    %7D
-%0A%0A          done();
 %0A   
@@ -2164,126 +2164,60 @@
     
-done(false);%0A        %7D%0A      %7D else %7B%0A        grunt.warn(%22file %22+file+%22 does not exist%22);%0A        done(false);
+err = true%0A          return false;%0A        %7D
 %0A      %7D
 %0A   
@@ -2208,24 +2208,26 @@
    %7D%0A      %7D
+);
 %0A    %7D);%0A  %7D

allow multiple files to be passed to task