var Function, atomCompileLess, compileFile, fs, getFileContents, less, path;

less = require("less");
fs = require("fs");
path = require("path");

Function = require("loophole").Function;

getFileContents = function(filePath, callback) {
  var content, read;
  content = '';
  return fs.readFile(filePath, 'utf-8', read = function(err, data) {
    if (err) {
      throw err;
    }
    return callback(data);
  });
};

compileFile = function(filepath) {
  var outputCompressed, showSuccessMessage;
  outputCompressed = atom.config.get('atom-compile-less.compressCss');
  showSuccessMessage = atom.config.get('atom-compile-less.showSuccessMessage');
  return getFileContents(filepath, function(content) {
    var parser;
    if (!content) {
      throw err;
    }
    parser = new less.Parser({
      paths: [path.dirname(filepath)]
    });
    return parser.parse(content, (function(_this) {
      return function(err, parsedContent) {
        var cssFilePath, outputCss;
        if (err) {
          throw err;
        }
        outputCss = parsedContent.toCSS({
          compress: outputCompressed
        });
        cssFilePath = filepath.replace(".less", ".css");
        return fs.writeFile(cssFilePath, outputCss, function(err) {
          var fileName, message;
          if (showSuccessMessage) {
            fileName = cssFilePath.split('/');
            fileName = fileName[fileName.length - 1];
            message = 'File <strong>' + fileName + '</strong> compiled! Yeih!';
            return atom.notifications.addSuccess(message);
          }
        });
      };
    })(this));
  });
};

atomCompileLess = function() {
  var currentEditor, currentFilePath, includeMainFile, projectMainLess, projectPath;
  currentEditor = atom.workspace.getActiveTextEditor();
  if (currentEditor) {
    currentFilePath = currentEditor.getPath();
    if (currentFilePath.substr(-4) === "less") {
      projectPath = atom.project.getPaths();
      projectMainLess = atom.project.getPaths() + atom.config.get('atom-compile-less.mainLessFile');
      console.log(atom.config.get('atom-compile-less.mainLessFile'));
      includeMainFile = atom.config.get('atom-compile-less.compileMainFile');
      compileFile(currentFilePath);
      if (includeMainFile) {
        compileFile(projectMainLess);
      }
      return global.Function = Function;
    }
  }
};

module.exports = {
  config: {
    compressCss: {
      type: 'boolean',
      default: true
    },
    mainLessFile: {
      type: 'string',
      default: 'main.less'
    },
    compileMainFile: {
      type: 'boolean',
      default: true
    },
    showSuccessMessage: {
      type: 'boolean',
      default: true
    },
  },

  activate: (function(_this) {
    return function(state) {
      return atom.workspace.observeTextEditors(function(editor) {
        return editor.onDidSave((function(_this) {
          return function() {
            return atomCompileLess();
          };
        })(this));
      });
    };
  })(this),
  deactivate: function() {},
  serialize: function() {}
};

@@ -443,16 +443,32 @@
 filepath
+,outputDirectory
 ) %7B%0A  va
@@ -1134,30 +1134,139 @@
     
-cssFilePath = filepath
+var fileName;%0A        fileName = filepath.split('/');%0A        fileName = fileName%5BfileName.length - 1%5D;%0A        fileName = fileName
 .rep
@@ -1274,24 +1274,104 @@
 ace(
-%22
+'
 .less
-%22, %22.css%22)
+','.css');%0A        cssFilePath = atom.project.getPaths()%5B'0'%5D+outputDirectory+'/'+fileName
 ;%0A  
@@ -1453,18 +1453,8 @@
  var
- fileName,
  mes
@@ -1499,109 +1499,8 @@
 ) %7B%0A
-            fileName = cssFilePath.split('/');%0A            fileName = fileName%5BfileName.length - 1%5D;%0A
     
@@ -2269,32 +2269,110 @@
 pileMainFile');%0A
+      outputDirectory = atom.config.get('atom-compile-less.outputDirectory');%0A
       compileFil
@@ -2388,16 +2388,32 @@
 FilePath
+,outputDirectory
 );%0A     
@@ -2471,16 +2471,32 @@
 MainLess
+,outputDirectory
 );%0A     
@@ -2649,24 +2649,197 @@
 true%0A    %7D,%0A
+    outputDirectory: %7B%0A      type: 'string',%0A      default: '/css' // add location to output directory, relative to%0A                      // project's root directory%0A    %7D,%0A
     mainLess

add ability to mention output directory for saving css files