#!/usr/bin/env node
var fs = require('fs')
var GitHubSlugger = require('github-slugger')
var markybars = require('./lib/markybars.js')
var path = require('path')
var pullOrClone = require('./lib/pull-or-clone.js')
var join = path.join
var sh = require('shelljs')
var resolve = require('path').resolve

var awesomePath = join('tmp/awesome-standard')
var buildPath = 'build'
var logoPath = join(buildPath, 'docs')
var demoPath = join('tmp/standard-demo')
var mdPath = join('tmp/markdown')
var stdPath = join('tmp/standard')
var stdDocsPath = join('tmp/standard/docs')
var page = join('layout/page.html')
var demoPage = join('layout/demo.html')

var partials = {
  toc: join('layout/partials/toc.html'),
  ga: join('layout/partials/ga.html')
}

var slugger = new GitHubSlugger()

if (!sh.which('git')) {
  sh.echo('Sorry, this script requires git')
  sh.exit(1)
}

pullOrClone('https://github.com/feross/standard', stdPath)
pullOrClone('https://github.com/feross/awesome-standard', awesomePath)
pullOrClone('https://github.com/flet/standard-demo', demoPath)

sh.rm('-rf', buildPath)
sh.rm('-rf', mdPath)

sh.mkdir(buildPath)
sh.mkdir('-p', logoPath)
sh.mkdir(mdPath)

sh.cp('-f', join('markdown/*.md'), mdPath)
sh.cp('-f', join(stdPath, '*.md'), mdPath)
sh.cp('-f', join(stdDocsPath, '*.md'), mdPath)
sh.cp('-f', join(awesomePath, 'README.md'), join(mdPath, 'awesome.md'))
sh.cp('-R', join(stdDocsPath, 'logos'), logoPath)
var genPage = markybars.compile(page, partials)
var genDemo = markybars.compile(demoPage, partials)

var files = sh.ls(mdPath)
files.forEach(function (file) {
  var fileData = fs.readFileSync(join(mdPath, file), 'utf8')
  var name = path.parse(file).name
  var gen = name === 'demo' ? genDemo : genPage

  var htmlData = gen({data: fileData})
  var fileName = slugger.slug(name) + '.html'

  fs.writeFileSync(join(buildPath, fileName), htmlData, 'utf8')
})

// replace all RULES.md instances in links with rules.html
sh.sed('-i', /RULES(.*?)\.md/g, 'rules.html', join(buildPath, 'readme.html'))
sh.sed('-i', /"docs\/RULES(.*?)\.md/g, '"rules$1.html', join(buildPath, 'readme.html'))
sh.sed('-i', /"docs\/README(.*?)\.md/g, '"readme$1.html', join(buildPath, 'readme.html'))
sh.sed('-i', /"docs\/webstorm\.md"/g, '"webstorm.html"', join(buildPath, 'readme.html'))

sh.sed('-i', /"docs\/RULES(.*?)\.md/g, '"rules$1.html', join(buildPath, 'rules.html'))

// rename files to be internet friendly
sh.mv('-f', join(buildPath, 'readme.html'), join(buildPath, 'index.html'))

// once everything is built, copy it to root
sh.rm('dist/*.html')
sh.cp('-R', buildPath + '/', resolve(__dirname, 'dist'))

// copy standard-demo bundle.js to root
sh.cp('-f', join(demoPath, 'bundle.js'), resolve(__dirname, 'dist', 'standard-demo.js'))

@@ -1871,16 +1871,119 @@
 8')%0A%7D)%0A%0A
+sh.find(buildPath)%0A.filter(function (file) %7B return file.match(/%5C.html$/) %7D)%0A.forEach(function (f) %7B%0A  
 // repla
@@ -2033,16 +2033,18 @@
 es.html%0A
+  
 sh.sed('
@@ -2049,16 +2049,33 @@
 ('-i', /
+%22(docs%5C/%7C%5C.%5C.%5C/)?
 RULES(.*
@@ -2089,21 +2089,24 @@
 g, '
+%22
 rules
+$2
 .html', 
 join
@@ -2105,128 +2105,68 @@
 l', 
-join(buildPath, 'readme.html'))%0Ash.sed('-i', /%22docs%5C/RULES(.*?)%5C.md/g, '%22rules$1.html', join(buildPath, 'readme.html'))%0A
+f)%0A%0A  sh.sed('-i', /%22%5C.%5C.%5C/README.md/g, '%22index.html', f)%0A  
 sh.s
@@ -2176,22 +2176,21 @@
 '-i', /%22
-docs%5C/
+(.*?)
 README(.
@@ -2209,25 +2209,25 @@
 %22readme$
-1
+2
 .html', 
 join(bui
@@ -2222,40 +2222,14 @@
 l', 
-join(buildPath, 'readme.html'))%0A
+f)%0A%0A  
 sh.s
@@ -2243,168 +2243,69 @@
 , /%22
+(
 docs%5C/
-webstorm%5C.md%22/g, '%22webstorm.html%22', join(buildPath, 'readme.html'))%0A%0Ash.sed('-i', /%22docs%5C/RULES(.*?)%5C.md/g, '%22rules$1.html', join(buildPath, 'rules.html')
+%7C%5C.%5C.%5C/)?webstorm(.*?)%5C.md/g, '%22webstorm$2.html', f)%0A%7D
 )%0A%0A/

fix translation links