#!/usr/bin/env node
/*jshint shelljs:true, lastsemic:true, -W101*/

"use strict";

var version = require("../package.json").version;
require("shelljs/make");

exec("git log --format='%H|%h|%an|%s' " + version + "..HEAD", { silent: true }, function (code, output) {
  if (code !== 0)
    return void console.log("git log return code is non-zero");

  var commits = output.split("\n")
    .filter(function (cmt) { return cmt.trim() !== "" })
    .map(function (cmt) { return cmt.split("|") });

  var html = "";
  var authors = {};

  commits.forEach(function (cmt) {
    if (cmt[3].indexOf("Merge") === 0) {
      return;
    }
    var tr = "";
    tr += "<td class='commit'><a href='https://github.com/jshint/jshint/commit/" + cmt[0] + "'>" + cmt[1] + "</a></td>";
    tr += "<td class='desc'>" + cmt[3].replace(/(#(\d+))/, "<a href='https://github.com/jshint/jshint/issues/$2/'>$1</a>") + "</td>";
    html += "<tr>" + tr + "</tr>\n";

    if (cmt[2] !== "Anton Kovalyov")
      authors[cmt[2]] = true;
  });

  echo("<!-- auto-generated -->");
  echo("<table class='changelog'>\n" + html + "</table>\n");

  if (Object.keys(authors).length) {
    echo("<p class='thx'><strong>Thanks</strong> to " + Object.keys(authors).join(", ") + " for sending patches!</p>");
  }
});
