#!/usr/bin/env node
"use strict";

var UTF8 = "utf-8";

var pkg = require("../package.json");
var fs = require("fs");
var path = require("path");
var chalk = require("chalk");
var nomnom = require("nomnom")
    .script("squiggle")
    .option("input", {
        position: 0,
        metavar: "FILE",
        help: "Compile this Squiggle file",
        type: "string"
    })
    .option("output", {
        abbr: "o",
        metavar: "FILE",
        help: "Write JavaScript to this file",
        type: "string"
    })
    .option("interactive", {
        abbr: "i",
        help: "Enter interactive shell",
        flag: true
    })
    .option("help", {
        abbr: "h",
        help: "Print this message",
        flag: true
    })
    .option("version", {
        abbr: "v",
        help: "Print verision number",
        flag: true,
        callback: function() { return pkg.version; }
    });
var argv = nomnom.parse();

var parse = require("./file-parse");
var transformAst = require("./transform-ast");
var compile = require("./compile");
var lint = require("./lint");
var repl = require("./repl");

function error(message) {
    console.error(chalk.bold.red(message));
}

function die(message) {
    error("squiggle: error: " + message);
    process.exit(1);
}

function compileTo(src, dest) {
    var txt = fs.readFileSync(src, "utf-8");
    var ast;
    try {
        ast = parse(txt);
    } catch (e) {
        if (e.name !== "SyntaxError") {
            throw e;
        }
        var expectations = e
            .expected
            .map(function(x) { return x.description; })
            .join(", ");
        die(
            e.line + ":" + e.column +
            " expected one of " + expectations +
            ", but found '" + e.found + "'."
        );
    }
    var warnings = lint(ast);
    warnings.forEach(function(m) {
        error('squiggle: lint: ' + m);
    });
    var es = transformAst(ast);
    var code = compile(es);
    fs.writeFileSync(dest, code, UTF8);
}

if (argv.interactive) {
    repl.start();
} else if (argv._.length === 1 && argv.output) {
    compileTo(argv._[0], argv.output);
} else {
    console.log(nomnom.getUsage());
    process.exit(1);
}

@@ -45,24 +45,71 @@
 = %22utf-8%22;%0A%0A
+var WRITE_TO_STDOUT = %7B_: %22WRITE_TO_STDOUT%22%7D;%0A%0A
 var pkg = re
@@ -450,17 +450,19 @@
     
-abbr: %22o%22
+position: 1
 ,%0A  
@@ -1996,16 +1996,96 @@
 le(es);%0A
+    if (dest === WRITE_TO_STDOUT) %7B%0A        console.log(code);%0A    %7D else %7B%0A    
     fs.w
@@ -2116,16 +2116,22 @@
  UTF8);%0A
+    %7D%0A
 %7D%0A%0Aif (a
@@ -2201,23 +2201,85 @@
 == 1
- && argv.output
+) %7B%0A    compileTo(argv._%5B0%5D, WRITE_TO_STDOUT);%0A%7D else if (argv._.length === 2
 ) %7B%0A
@@ -2308,22 +2308,20 @@
 %5D, argv.
-output
+_%5B1%5D
 );%0A%7D els

Write to stdout if not file given; removes -o