/* jshint node: true */
var path = require("path");
var fs = require("fs");
var EOL = require("os").EOL;

function makeConfigLine(config, $var, opt, defaultValue) {
  return config[opt] || config[opt] === 0 ?
  $var + ": " + config[opt] + ";" :
  $var + ": " + defaultValue + "; // " + $var + ": flexi.config." + opt;
}

function makeBreakpointLine(bp, isLast) {
  var str = "  " + bp.name + ": ('" +
    bp.prefix + "', " + bp.begin + ")";
  return isLast ? str : str + ",";
}

module.exports = function compile(base, config) {
  var filePath = path.join(base, "_variables.scss");

  // clean out the old
  fs.unlinkSync(filePath);

  var lines = [
    "/*",
    " * This scss file is auto generated by Flexi to expose your configuration",
    " * options defined in config/flexi.js to flexi's SCSS",
    " * ",
    " * All unused rules are automatically filtered from your build.",
    " */"
  ];

  // simple variables
  lines.push(
    makeConfigLine(config, "$gutter-padding", "gutterPadding", ".5rem"),
    makeConfigLine(config, "$columns", "columns", "12"),
    makeConfigLine(config, "$column-prefix", "columnPrefix", "'col'"),
    makeConfigLine(config, "$include-element-css", "includeElementCSS", "true"),
    makeConfigLine(config, "$include-css", "includeCSS", "true"),
    makeConfigLine(config, "$include-media-css", "includeMediaCSS", "true")
  );

  // breakpoints
  lines.push("$breakpoints: (");

  var bps = config.breakpoints.sort(function (a, b) {
    return a.begin > b.begin ? 1 : -1;
  });

  bps.forEach(function (bp, i) {
    lines.push(makeBreakpointLine(bp, i === bps.length - 1));
  });
  lines.push(");");

  lines.forEach(function (line) {
    fs.appendFileSync(filePath, line + EOL);
  });

  return filePath;
};

/* jshint node: true */
var path = require("path");
var fs = require("fs");
var EOL = require("os").EOL;

function makeConfigLine(config, $var, opt, defaultValue) {
  return opt in config ?
  $var + ": " + config[opt] + ";" :
  $var + ": " + defaultValue + "; // " + $var + ": flexi.config." + opt;
}

function makeBreakpointLine(bp, isLast) {
  var str = "  " + bp.name + ": ('" +
    bp.prefix + "', " + bp.begin + ")";
  return isLast ? str : str + ",";
}

module.exports = function compile(base, config) {
  var filePath = path.join(base, "_variables.scss");

  // clean out the old
  fs.unlinkSync(filePath);

  var lines = [
    "/*",
    " * This scss file is auto generated by Flexi to expose your configuration",
    " * options defined in config/flexi.js to flexi's SCSS",
    " * ",
    " * All unused rules are automatically filtered from your build.",
    " */"
  ];

  // simple variables
  lines.push(
    makeConfigLine(config, "$gutter-padding", "gutterPadding", ".5rem"),
    makeConfigLine(config, "$columns", "columns", "12"),
    makeConfigLine(config, "$column-prefix", "columnPrefix", "'col'"),
    makeConfigLine(config, "$include-element-css", "includeElementCSS", "true"),
    makeConfigLine(config, "$include-css", "includeCSS", "true"),
    makeConfigLine(config, "$include-media-css", "includeMediaCSS", "true")
  );

  // breakpoints
  lines.push("$breakpoints: (");

  var bps = config.breakpoints.sort(function (a, b) {
    return a.begin > b.begin ? 1 : -1;
  });

  bps.forEach(function (bp, i) {
    lines.push(makeBreakpointLine(bp, i === bps.length - 1));
  });
  lines.push(");");

  lines.forEach(function (line) {
    fs.appendFileSync(filePath, line + EOL);
  });

  return filePath;
};

Use a config option if its defined.