// Generated by CoffeeScript 2.1.1
// Requires
var CSON, conversion, create, data, filePath, fs, hasData, i, opts, outputHelp, parse, processData, result, stdin, timeout, timeoutFunction;

fs = require('fs');

CSON = require('../');

opts = {};

// Helpers
outputHelp = function() {
  return process.stdout.write('CSON CLI\n\nUsage:\n\n	# Convert a JSON file into a CSON file\n	json2cson in.json > out.cson\n\n	# Same thing via piping\n	cat in.json | json2cson > out.cson\n\n	# Convert a CSON file into a JSON file\n	cson2json in.cson > out.json\n\n	# Same thing via piping\n	cat in.cson | cson2json > out.json\n\nOptions\n\n	# Display this help\n	--help\n\n	# Indentation for CSON output\n	--tabs\n	--2spaces\n	--4spaces\n');
};

// Check arguments
if (process.argv.indexOf('--help') !== -1) {
  outputHelp();
  process.exit(0);
}

// Figure out conversion
if (process.argv.toString().indexOf('cson2json') !== -1) {
  conversion = 'cson2json';
} else if (process.argv.toString().indexOf('json2cson') !== -1) {
  conversion = 'json2cson';
  opts.indent = (i = process.argv.indexOf('--tabs')) !== -1 ? '\t' : (i = process.argv.indexOf('--2spaces')) !== -1 ? '  ' : (i = process.argv.indexOf('--4spaces')) !== -1 ? '    ' : void 0;
  if (i !== -1) {
    process.argv = process.argv.slice(0, i).concat(process.argv.slice(i + 1));
  }
} else {
  process.stderr.write('Unknown conversion');
  process.exit(1);
}

// File conversion
if (process.argv.length === 3) {
  filePath = process.argv[2];
  if (conversion === 'cson2json') {
    parse = CSON.parseCSONFile.bind(CSON);
    create = CSON.createJSONString.bind(CSON);
  } else {
    parse = CSON.parseJSONFile.bind(CSON);
    create = CSON.createCSONString.bind(CSON);
  }
  result = parse(filePath);
  if (result instanceof Error) {
    throw result;
  }
  result = create(result, opts);
  if (result instanceof Error) {
    throw result;
  }
  process.stdout.write(result);
// Try STDIN
} else if (process.argv.length === 2) {
  // Prepare
  data = '';
  hasData = function() {
    return data.replace(/\s+/, '').length !== 0;
  };
  processData = function() {
    if (conversion === 'cson2json') {
      parse = CSON.parseCSONString.bind(CSON);
      create = CSON.createJSONString.bind(CSON);
    } else {
      parse = CSON.parseJSONString.bind(CSON);
      create = CSON.createCSONString.bind(CSON);
    }
    result = parse(data);
    if (result instanceof Error) {
      throw result;
    }
    result = create(result, opts);
    if (result instanceof Error) {
      throw result;
    }
    return process.stdout.write(result);
  };
  // Timeout if we don't have stdin
  timeoutFunction = function() {
    var timeout;
    // Clear timeout
    timeout = null;
    // Skip if we are using stdin
    if (hasData() === false) {
      stdin.pause();
      process.stderr.write('No STDIN data received...');
      return process.exit(1);
    }
  };
  timeout = setTimeout(timeoutFunction, 1000);
  // Read stdin
  stdin = process.stdin;
  stdin.setEncoding('utf8');
  stdin.resume(); // node 0.8
  stdin.on('data', function(_data) {
    return data += _data.toString();
  });
  process.stdin.on('end', function() {
    if (timeout) {
      clearTimeout(timeout);
      timeout = null;
    }
    return processData();
  });
} else {
  outputHelp();
  process.exit(1);
}
