#!/usr/bin/env node
/*jshint shelljs:true */

"use strict";

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

var distDir    = path.join(__dirname, "../dist");
var srcDir     = path.join(__dirname, "../src");

if (!test("-e", distDir))
  mkdir(distDir);

bundle.require(srcDir + "/jshint.js", { expose: "jshint" });
bundle.bundle(function (err, src) {
  var web   = distDir + "/jshint.js";
  var rhino = distDir + "/jshint-rhino.js";

  [ "/*! " + version + " */",
    "var JSHINT;",
    "if (typeof window === 'undefined') window = {};",
    "(function () {",
      "var require;",
      src,
      "JSHINT = require('jshint').JSHINT;",
      "if (typeof exports === 'object' && exports) exports.JSHINT = JSHINT;",
    "}());"
  ].join("\n").to(web);

  ("#!/usr/bin/env rhino\nvar window = {};\n" + cat(web, srcDir + "/platforms/rhino.js")).to(rhino);
  chmod("+x", rhino);

  console.log("Built: ", version);
});
