#!/usr/bin/env node

var path   = require('path'),
    fs     = require('fs'),
    colors = require('colors'),
    sass   = require(path.join(__dirname, '..', 'sass')),
    cwd    = process.cwd(),

    optimist = require('optimist')
      .usage('Compile .scss files with node-sass\nUsage: node-sass [options] <input.scss> [<output.css>]')
      .describe('output-style', 'CSS output style (nested|expanded|compact|compressed)')
      .default('output-style', 'nested')
      .describe('source-comments', 'Include debug info in output (none|normal|map)')
      .default('source-comments', 'none')
      .describe('include-path', 'Path to look for @import-ed files')
      .default('include-path', cwd)
      .string('h').alias('h', 'help')
      .describe('help', 'Print usage info'),

    argv    = optimist.argv,
    inFile  = argv._[0],
    outFile = argv._[1];

if(argv.help || !inFile) return optimist.showHelp();

console.log('Starting Render Process...'.green);

sass.render({
  file: inFile,

  includePaths: [argv['include-path']],
  outputStyle: argv['output-style'],
  sourceComments: argv['source-comments'],

  success: function(css) {
    console.log('Rendering Complete, saving .css file...'.green);

    if(!outFile) outFile = path.join(cwd, path.basename(inFile, '.scss') + '.css');

    fs.writeFile(outFile, css, function(err) {
      if(err) return console.log(('Error: ' + err).red);
      console.log(('Wrote CSS to ' + outFile).green);
    });
  },

  error: function(err) {
    console.log('** Error Rendering SASS **'.red);
    console.log(err.yellow);
  }
});
