var grunt = require('grunt');

/*
  Make substitutions for templated parts of files where substitutions
  need to happen after finger-printed keys generated.
 */

module.exports = function(grunt) {
  var taskName = "templateCSS"

  //can add additional files here
  var templateFiles = ['css/glazier.css']

  var description = "process " + taskName;
  grunt.registerTask(taskName, description, function() {
    for (var i=0; i<templateFiles.length; i++) {
      var templateFile = templateFiles[i];
      //always generate file for dev
      templateDevFile(templateFile, devConfig);
      if(process.env.GLAZIER_ENV === "prod") {
        templateProdFile(templateFile);
      }
    }
  });
};


//processed dev file => tmp/public
var devConfig = {
  tempDir: "tmp",
  templateDir: "public",
  buildDir: "public",
  sourceFile: function(template){ return [this.tempDir, this.templateDir, template].join("/"); },
  writeFile: function(template){ return [this.tempDir, this.buildDir, template].join("/"); },
  getData: function(){ return data; },
  data: {
    manifestUrl: function(path) {
      return path;
    }
  }
}

function templateDevFile(templatePath, config) {
  var template = grunt.file.read(config.sourceFile(templatePath));
  var indexContents = grunt.template.process(template, { data: config.data});
  console.log(config.writeFile(templatePath));
  grunt.file.write(config.writeFile(templatePath), indexContents);
}


//processed prod file => tmp/md5

/*
  eventually might want to extract a config and
  unify the process for dev/prod generation but prod side
  needs a bit of unraveling
*/
function templateProdFile(templatePath) {
  var manifest = grunt.file.readJSON('tmp/manifest.json');
  var md5File = getMD5Filename(manifest, templatePath);
  var template = grunt.file.read(md5File);
  var assetHost = grunt.config.get('pkg').assetHost || '';

  var indexContents = grunt.template.process(template, {
    data: {
      manifestUrl: function(path) {
        if (!manifest[path]) {
          throw "No file found in manifest for path " + path;
        } else {
          console.log("Found entry for path " + path);
        }
        return grunt.config.process('<%= pkg.assetHost %>') + manifest[path]; //.replace(/(-[^-]+)\.js$/, '$1.js');
      },
      manifest: JSON.stringify(manifest || {}),
      assetHost: assetHost
    }
  });
  grunt.file.write(md5File, indexContents);
}

function getMD5Filename(manifest, path) {
  var file =  manifest["/"+path];
  if (!file) {
    throw "No file found in manifest for path " + path;
  } else {
    var md5FileArray = file.split("/");
    var md5File =  md5FileArray[md5FileArray.length-1];
    var pathArray = path.split("/");
    pathArray.pop();

  }
  return "tmp/md5/" + pathArray.join("/") + "/" + md5File;
}

@@ -832,32 +832,36 @@
 ction(template)%7B
+%0A   
  return %5Bthis.te
@@ -901,24 +901,26 @@
 %5D.join(%22/%22);
+%0A 
  %7D,%0A  writeF
@@ -943,16 +943,20 @@
 mplate)%7B
+%0A   
  return 
@@ -1005,16 +1005,18 @@
 in(%22/%22);
+%0A 
  %7D,%0A  ge
@@ -2233,47 +2233,9 @@
 th%5D;
- //.replace(/(-%5B%5E-%5D+)%5C.js$/, '$1.js');
 %0A
+
     
@@ -2449,16 +2449,17 @@
 +path%5D;%0A
+%0A
   if (!f
@@ -2467,32 +2467,42 @@
 le) %7B%0A    throw 
+new Error(
 %22No file found i
@@ -2512,32 +2512,33 @@
 anifest for path
+:
  %22 + path;%0A  %7D e
@@ -2526,24 +2526,25 @@
 th: %22 + path
+)
 ;%0A  %7D else %7B
@@ -2698,21 +2698,21 @@
 .pop();%0A
-%0A
   %7D%0A
+%0A
   return

clean md5 templating