#!/usr/bin/env node

var path = require('path');
var fs = require('fs');
var millstone = require('../lib/millstone');
var optimist = require('optimist')
            .usage("Usage: $0 <source MML file>")
            .options('h', {alias:'help', describe:'Display this help message'})
            .options('n', {alias:'nosymlink', boolean:true, describe:'Use unmodified paths instead of symlinking files'})
            .options('b', {alias:'benchmark', boolean:true, describe:'Basic timing of key operations'});

var options = optimist.argv;

if (options.help) {
    optimist.showHelp();
    process.exit(0);
}

var input = options._[0];

if (!input || path.extname(input) !== '.mml') {
   console.log('millstone: please provide a path to an mml file');
   process.exit(1);
}
var mml = JSON.parse(fs.readFileSync(input));

var options = {
    mml: mml,
    base: path.dirname(input),
    cache: '/tmp/millstone-test',
    nosymlink: options.nosymlink,
    benchmark: options.benchmark
};

millstone.resolve(options, function(err, resolved) {
    if (err) throw err;
    console.log(JSON.stringify(resolved,null,4));
    millstone.drainPool(function(err) {
        if (err) throw err;
    });
});