/*
 * grunt-writetags
 * https://github.com/cb/grunt-contrib-writetags
 *
 * Copyright (c) 2015 Carsten Blüm
 * Licensed under the MIT license.
 */

'use strict';

module.exports = function (grunt) {

    grunt.registerMultiTask(
        'writetags',
        'Writes tags for JS  and/or CSS files to a file',
        function () {

            var prefix, replace;
            var path = require('path');
            var paths = grunt.config.get(this.name + '.' + this.target + '.paths');
            var dest = grunt.config.get(this.name + '.' + this.target + '.dest');
            var destPath = path.dirname(dest);

            if (grunt.config.get(this.name + '.' + this.target + '.prefix')) {
                prefix = grunt.config.get(this.name + '.' + this.target + '.prefix');
            } else if (grunt.config.get(this.name + '.options.prefix')) {
                prefix = grunt.config.get(this.name + '.options.prefix');
            } else {
                prefix = '';
            }

            if (grunt.config.get(this.name + '.' + this.target + '.replace')) {
                replace = grunt.config.get(this.name + '.' + this.target + '.replace');
            } else if (grunt.config.get(this.name + '.options.replace')) {
                replace = grunt.config.get(this.name + '.options.replace');
            } else {
                replace = null;
            }

            if (replace && '[object Object]' !== Object.prototype.toString.call(replace)) {
                grunt.fail.warn('Option “replace” is expected to be an object');
                replace = null;
            }

            var html = paths.map(
                function (configuredPath) {

                    var usePath = prefix ? prefix + configuredPath : path.relative(destPath, configuredPath);

                    if (replace) {
                        Object.keys(replace).forEach(function(key) {
                            usePath = usePath.replace(key, replace[key]);
                        });
                    }

                    if ('.js' === usePath.substr(-3).toLowerCase()) {
                        return '<script src="' + usePath + '"></script>\n';
                    }

                    if ('.css' === usePath.substr(-4).toLowerCase()) {
                        return '<link rel="stylesheet" href="' + usePath + '" />\n';
                    }

                    grunt.fail.warn('Expected filenames to end with *.css or *.js, but got filename: ' + configuredPath);
                }
            ).join('');

            grunt.file.write(dest, html);
            grunt.log.ok('Wrote tags for ' + paths.length + ' file(s) to ' + dest);
        }
    );

};

@@ -88,16 +88,21 @@
 (c) 2015
+-2016
  Carsten
@@ -277,17 +277,16 @@
  for JS 
- 
 and/or C
@@ -333,41 +333,8 @@
  %7B%0A%0A
-            var prefix, replace;%0A
     
@@ -579,24 +579,93 @@
 ame(dest);%0A%0A
+            var getOption = function(optionName, defaultValue) %7B%0A    
             
@@ -719,23 +719,30 @@
 get + '.
-prefix'
+' + optionName
 )) %7B%0A   
@@ -750,32 +750,34 @@
             
-prefix =
+    return
  grunt.confi
@@ -816,23 +816,30 @@
 get + '.
-prefix'
+' + optionName
 );%0A     
@@ -837,38 +837,53 @@
 e);%0A            
-%7D else
+    %7D%0A               
  if (grunt.confi
@@ -910,23 +910,30 @@
 options.
-prefix'
+' + optionName
 )) %7B%0A   
@@ -945,24 +945,26 @@
         
-prefix =
+    return
  grunt.c
@@ -999,15 +999,22 @@
 ons.
-prefix'
+' + optionName
 );%0A 
@@ -1020,32 +1020,29 @@
             
-%7D else %7B
+    %7D
 %0A           
@@ -1050,19 +1050,27 @@
     
-p
 re
-fix = ''
+turn defaultValue
 ;%0A  
@@ -1072,32 +1072,44 @@
 e;%0A            %7D
+.bind(this);
 %0A%0A            if
@@ -1110,78 +1110,46 @@
     
-if (grunt.config.get(this.name + '.' + this.target + '.replace')) %7B%0A  
+var prefix = getOption('prefix', '');%0A
     
@@ -1148,33 +1148,35 @@
 ');%0A            
- 
+var
  replace = grunt
@@ -1175,66 +1175,26 @@
  = g
-runt.config.get(this.name + '.' + this.target + '.
+etOption('
 replace'
 );%0A 
@@ -1181,32 +1181,38 @@
 Option('replace'
+, null
 );%0A            %7D
@@ -1214,70 +1214,95 @@
     
-%7D else if (grunt.config.get(this.name + '.options.replace')) %7B
+var scriptTemplate = getOption('scriptTemplate', '%3Cscript src=%22%7B%7Bpath%7D%7D%22%3E%3C/script%3E%5Cn');
 %0A   
@@ -1314,138 +1314,102 @@
     
-    replac
+var styleTemplat
 e = g
-runt.config.get(this.name + '.options.replace');%0A            %7D else %7B%0A                replace = null;%0A            %7D
+etOption('styleTemplate', '%3Clink rel=%22stylesheet%22 href=%22%7B%7Bpath%7D%7D%22 /%3E%5Cn');
 %0A%0A  
@@ -2150,51 +2150,51 @@
 urn 
-'%3C
 script
- src=%22' + usePath + '%22%3E%3C/script%3E%5Cn'
+Template.replace('%7B%7Bpath%7D%7D', usePath)
 ;%0A  
@@ -2320,60 +2320,50 @@
 urn 
-'%3Clink rel=%22stylesheet%22 href=%22' + usePath + '%22 /%3E%5Cn'
+styleTemplate.replace('%7B%7Bpath%7D%7D', usePath)
 ;%0A  

Implement new options