const copy = require("../../copy");
const path = require("path");
const fs = require("fs");

const templates = {
  test: {
    filename: path.join(__dirname, "templates", "example.js"),
    variable: "example"
  },
  contract: {
    filename: path.join(__dirname, "templates", "Example.sol"),
    name: "Example",
    variable: "example"
  },
  migration: {
    filename: path.join(__dirname, "templates", "migration.js")
  }
};

var processFile = function(file_path, processfn, callback) {
  fs.readFile(file_path, { encoding: "utf8" }, function(err, data) {
    if (err != null) {
      callback(err);
      return;
    }

    var result = processfn(data);
    fs.writeFile(file_path, result, { encoding: "utf8" }, callback);
  });
};

var replaceContents = function(file_path, find, replacement, callback) {
  processFile(
    file_path,
    function(data) {
      if (typeof find === "string") {
        find = new RegExp(find, "g");
      }
      return data.replace(find, replacement);
    },
    callback
  );
};

var toUnderscoreFromCamel = function(string) {
  string = string.replace(/([A-Z])/g, function($1) {
    return "_" + $1.toLowerCase();
  });

  if (string[0] === "_") {
    string = string.substring(1);
  }

  return string;
};

var Create = {
  contract: function(directory, name, options, callback) {
    if (typeof options === "function") {
      callback = options;
    }

    var from = templates.contract.filename;
    var to = path.join(directory, name + ".sol");

    if (!options.force && fs.existsSync(to)) {
      return callback(
        new Error("Can not create " + name + ".sol: file exists")
      );
    }

    copy.file(from, to, function(err) {
      if (err) return callback(err);

      replaceContents(to, templates.contract.name, name, callback);
    });
  },

  test: function(directory, name, options, callback) {
    if (typeof options === "function") {
      callback = options;
    }

    var underscored = toUnderscoreFromCamel(name);
    underscored = underscored.replace(/\./g, "_");
    var from = templates.test.filename;
    var to = path.join(directory, underscored + ".js");

    if (!options.force && fs.existsSync(to)) {
      return callback(
        new Error("Can not create " + underscored + ".js: file exists")
      );
    }

    copy.file(from, to, function(err) {
      if (err) return callback(err);

      replaceContents(to, templates.contract.name, name, function(err) {
        if (err) return callback(err);
        replaceContents(to, templates.contract.variable, underscored, callback);
      });
    });
  },

  migration: function(directory, name, options, callback) {
    if (typeof options === "function") {
      callback = options;
    }

    var underscored = toUnderscoreFromCamel(name || "");
    underscored = underscored.replace(/\./g, "_");
    var from = templates.migration.filename;
    var filename = (new Date().getTime() / 1000) | 0; // Only do seconds.

    if (name != null && name !== "") {
      filename += "_" + underscored;
    }

    filename += ".js";
    var to = path.join(directory, filename);

    if (!options.force && fs.existsSync(to)) {
      return callback(
        new Error("Can not create " + filename + ": file exists")
      );
    }

    copy.file(from, to, callback);
  }
};

module.exports = Create;

@@ -307,16 +307,36 @@
 ample%22,%0A
+    license: %22MIT%22,%0A
     vari
@@ -461,32 +461,33 @@
 sFile = function
+ 
 (file_path, proc
@@ -531,17 +531,16 @@
 _path, %7B
- 
 encoding
@@ -547,17 +547,16 @@
 : %22utf8%22
- 
 %7D, funct
@@ -554,24 +554,25 @@
 %22%7D, function
+ 
 (err, data) 
@@ -709,17 +709,16 @@
 esult, %7B
- 
 encoding
@@ -725,17 +725,16 @@
 : %22utf8%22
- 
 %7D, callb
@@ -779,16 +779,17 @@
 function
+ 
 (file_pa
@@ -865,16 +865,17 @@
 function
+ 
 (data) %7B
@@ -1070,16 +1070,17 @@
 function
+ 
 (string)
@@ -1128,16 +1128,17 @@
 function
+ 
 ($1) %7B%0A 
@@ -1261,24 +1261,446 @@
 string;%0A%7D;%0A%0A
+// getLicense return the license property value from Truffle config first and%0A// in case that the file doesn't exist it will fallback to package.json%0Afunction getLicense(options) %7B%0A  try %7B%0A    if ((license = require(%22@truffle/config%22).detect(options).license))%0A      return license;%0A  %7D catch (err) %7B%0A    console.log(err);%0A  %7D%0A%0A  try %7B%0A    return require(path.join(process.cwd(), %22package.json%22)).license;%0A  %7D catch %7B%7D%0A%7D%0A%0A
 var Create =
@@ -1714,32 +1714,33 @@
 ntract: function
+ 
 (directory, name
@@ -2107,32 +2107,33 @@
 om, to, function
+ 
 (err) %7B%0A      if
@@ -2225,24 +2225,144 @@
 , callback);
+%0A%0A      if ((license = getLicense(options)))%0A        replaceContents(to, templates.contract.license, license, callback);
 %0A    %7D);%0A  %7D
@@ -2372,32 +2372,33 @@
   test: function
+ 
 (directory, name
@@ -2875,32 +2875,33 @@
 om, to, function
+ 
 (err) %7B%0A      if
@@ -2995,16 +2995,17 @@
 function
+ 
 (err) %7B%0A
@@ -3169,16 +3169,17 @@
 function
+ 
 (directo

Use license from config/package for new contracts