'use strict';

const Metalsmith   = require('metalsmith')
const jade         = require('metalsmith-jade')
const sass         = require('metalsmith-sass')
const browserSync  = require('metalsmith-browser-sync')
const markdown     = require('metalsmith-markdown')
const layouts      = require('metalsmith-layouts')
const permalinks   = require('metalsmith-permalinks')
const babel        = require('metalsmith-babel')
const bourbon      = require('node-bourbon')
const path         = require('path')
const postcss      = require('metalsmith-postcss')
const autoprefixer = require('autoprefixer')
const copy         = require('metalsmith-copy')
const each         = require('metalsmith-each')
const navigation   = require('metalsmith-navigation')
const changed      = require('metalsmith-changed')

// --- general build settings --- //
const docsVersion = 'latest';

const updatePaths = function(file, filename){
  if(filename.substr(filename.length-5, filename.length) === '.html' && filename.substr(0, 5) !== 'docs/' && process.env.CI) {
    console.log(`Change filename ${filename} to ${filename.substr(0, filename.length-5)}`);
    return filename = filename.substr(0, filename.length-5);
  }
  return filename;
};

// --------- figuring out the navigation ---------- //
const navConfig = {
    header: {
      includeDirs: true,
      // pathProperty: docsVersion,
      pathProperty: 'nav_path',
      childrenProperty: 'nav_children',
    }
}

const navSettings = {
  navListProperty: 'navs',
  permalinks: false,
}

let nav = navigation(navConfig, navSettings);

// --------- Compiling the Markdown files to HTML --------//
let createDocs = function (event, file) {
  Metalsmith(path.join(__dirname, 'dcos-docs'))
    .source(docsVersion)
    .use(markdown({
      smartypants: true,
      gfm: true,
      tables: true
    }))
    .use(nav)
    .use(layouts({
      engine: 'jade',
      directory: path.join('..', 'layouts'),
      default: 'docs.jade',
    }))
    .use(jade({
      pretty: true
    }))
    .clean(false)
    .use(changed({
      extnames: {
        '.md': '.html' // build if src/file.md is newer than build/file.html
      }
    }))
    .destination(path.join('..', 'build', 'docs', docsVersion))
    .build((err) => {
      if (err) throw err
    })
}

Metalsmith(__dirname)
  .use(markdown({
    smartypants: true,
    gfm: true,
    tables: true
  }))
  .use(jade({
    pretty: true
  }))
  .use(sass({
    outputStyle: 'expanded',
    includePaths: [
      '/node_modules/',
      path.join(__dirname, 'node_modules/support-for/sass')
     ].concat(bourbon.includePaths)
  }))
  .use(postcss([
    autoprefixer({ browsers: ['last 4 versions'] })
  ]))
  .use(babel({
    presets: ['es2015']
  }))
  .use(copy({
    pattern: 'assets/*',
    directory: 'assets'
  }))
  .use(each(updatePaths))
  .clean(false)
  .use(changed())
  .use((() => {
    if(!process.env.CI) {
      return browserSync({
        server: {
          baseDir: './build',
          middleware: function(req, res, next) {
            var file = `./build${req.originalUrl}.html`;
            require('fs').exists(file, function(exists) {
              if (exists) req.url += '.html';
              next();
            });
          }
        },
        files: ['./src/**/*', './dcos-docs/**/*', './layouts/**/*', './mixins/**/*']
      }, null, createDocs);
    }
  })())
  .build((err) => {
    if (err) throw err
  })


createDocs();

@@ -912,51 +912,29 @@
  if(
-filename.substr(filename.length-5, 
+path.extname(
 filename
 .len
@@ -929,23 +929,16 @@
 filename
-.length
 ) === '.
@@ -969,20 +969,28 @@
 (0, 
-5
+12
 ) !== '
-docs
+get-started
 /' &
@@ -2128,24 +2128,52 @@
   %7D%0A    %7D))%0A
+    .use(each(updatePaths))%0A
     .destina

Fix compilation on CI