// Generated by CoffeeScript 1.7.1

/*
runscript.js
------------

A script that helps to track errors happening durin coffee
compilation and running of module code
 */

(function() {
  var code, crypto, cs, fs, issueApiCall, issueRequest, needle, params, request, sandbox, src, vm;

  if (!process.argv[2]) {
    console.log('Please provide a path to a coffee file');
    process.exit();
  }

  fs = require('fs');

  vm = require('vm');

  cs = require('coffee-script');

  needle = require('needle');

  crypto = require('crypto-js');

  request = require('request');

  issueApiCall = function(method, url, data, options, cb) {
    var err;
    try {
      return needle.request(method, url, data, options, (function(_this) {
        return function(err, resp, body) {
          try {
            return cb(err, resp, body);
          } catch (_error) {
            err = _error;
            return console.log('Error during needle request! ' + err.message);
          }
        };
      })(this));
    } catch (_error) {
      err = _error;
      return console.log('Error before needle request! ' + err.message);
    }
  };

  issueRequest = function(options, cb) {
    var err;
    try {
      return request(options, (function(_this) {
        return function(err, resp, body) {
          try {
            return cb(err, resp, body);
          } catch (_error) {
            err = _error;
            return console.log('Error during request! ' + err.message);
          }
        };
      })(this));
    } catch (_error) {
      err = _error;
      return console.log('Error before request! ' + err.message);
    }
  };

  params = JSON.parse(fs.readFileSync('params.json', 'utf8'));

  code = fs.readFileSync(process.argv[2], 'utf8');

  src = cs.compile(code);

  sandbox = {
    id: 'test.vm',
    params: params.userparams,
    needlereq: issueApiCall,
    request: issueRequest,
    cryptoJS: crypto,
    log: console.log,
    debug: console.log,
    exports: {}
  };

  vm.runInNewContext(src, sandbox, sandbox.id);

  if (process.argv[3] === 'ep') {
    sandbox.exports[process.argv[4]](function(evt) {
      return console.log(evt);
    });
  } else {
    sandbox.exports[process.argv[3]](params.event);
  }

  console.log("If no error happened until here it seems the script compiled and ran correctly! Congrats!");

}).call(this);
