// Read data files and generates presskit from templates
'use strict'

const fs = require('fs')
const path = require('path')
const converter = require('../lib/converter')
const handlebars = require('handlebars')

const templatesPath = { product: '../assets/project.html', company: '../assets/index.html' }

// -------------------------------------------------------------
// Module.
// -------------------------------------------------------------
var Generator = function () {
}

Generator.prototype.generate = function (dataPath) {
  console.log('Looking for data files in ' + dataPath)

  // Inspect given path for data files
  explore(dataPath, function (file) {
    var ext = path.extname(file)
    var filename = path.basename(file, ext)

    if (filename === 'data') {
      generateForData(dataPath, file)
    }
  })
}

// Find all files in the given folder and raise the callback for each
function explore (dirPath, callback) {
  fs.readdirSync(dirPath).forEach(function (name) {
    var filePath = path.join(dirPath, name)
    var stat = fs.statSync(filePath)
    if (stat.isFile()) {
      callback(filePath, stat)
    } else if (stat.isDirectory()) {
      // Resursivity
      explore(filePath, callback)
    }
  })
}

// Build the HTML for the given data file
function generateForData (basePath, dataFile) {
  var ext = path.extname(dataFile)

  // Find the right parser
  if (ext === '.xml') {
    // Old presskit() format
    generateDoPresskit(basePath, dataFile)
  } else {
    // TODO .json .md
    console.warn(ext + ' files cannot be processed... yet!')
  }
}

// Build the HTML for a data.xml (presskit()) file
function generateDoPresskit (basePath, dataFile) {
  // Convert data.xml into the nex JSON format
  // -- Read file
  fs.readFile(dataFile, function (err, xml) {
    if (err) console.error(err)
    else {
      // -- Call converter
      converter.convert(xml, function (err, presskit) {
        if (err) console.error(err)
        else {
          // Generate template
          build(basePath, dataFile, presskit)
        }
      })
    }
  })
}

// Build the HTML
function build (basePath, dataFile, presskit) {
  // Get template
  var templatePath
  if (presskit.type === 'product') {
    templatePath = templatesPath.product
  } else if (presskit.type === 'company') {
    templatePath = templatesPath.company
  }

  var t = fs.readFileSync(templatePath, 'utf-8')
  var template = handlebars.compile(t)

  // Call Handlebars
  var html = template({ presskit: presskit })

  // Export result in HTML
  // -- Ensure ./build exists
  var buildDir = path.join(basePath, 'build')
  createDir(buildDir)

  if (presskit.type !== 'company') {
    // Company should be placed in the root folder
    // but each product is a subfolder, named as the source's folder name
    buildDir = path.join(buildDir, path.basename(path.dirname(dataFile)))
    createDir(buildDir)
  }

  // Prepare output filename
  var htmlFilename = 'index.html'
  var htmlDestination = path.join(buildDir, htmlFilename)

  console.log('Generating: ' + dataFile + ' ->  ' + htmlDestination)

    // -- Write HTML
  fs.writeFileSync(htmlDestination, html)

  // -- Copy all assets from images/ to build/images/
  var assetsSourceDir = path.join(path.dirname(dataFile), 'images')
  if (fs.existsSync(assetsSourceDir)) {
    var assetsTargetDir = path.join(buildDir, 'images')

    // Create target dir if necessary
    createDir(assetsTargetDir)

    // Copy all assets from source to target
    fs.readdirSync(assetsSourceDir).forEach(function (name) {
      var source = path.join(assetsSourceDir, name)
      var target = path.join(assetsTargetDir, name)
      fs.writeFileSync(target, fs.readFileSync(source))
    })
  }
}

// Create dir if it doesn't exists
function createDir (dir) {
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir)
  }
}

// -------------------------------------------------------------
// Exports.
// -------------------------------------------------------------
module.exports = new Generator()

@@ -238,16 +238,37 @@
 product:
+ path.join(__dirname,
  '../ass
@@ -284,16 +284,17 @@
 ct.html'
+)
 , compan
@@ -295,16 +295,37 @@
 company:
+ path.join(__dirname,
  '../ass
@@ -339,16 +339,17 @@
 ex.html'
+)
  %7D%0A%0A// -
@@ -1518,16 +1518,128 @@
 taFile)%0A
+  %7D else if (ext === '.json') %7B%0A    // New presskit.html format%0A    generatePresskitDotHTML(basePath, dataFile)%0A
   %7D else
@@ -1657,14 +1657,8 @@
 ODO 
-.json 
 .md%0A
@@ -1860,17 +1860,17 @@
 o the ne
-x
+w
  JSON fo
@@ -2217,32 +2217,433 @@
 )%0A    %7D%0A  %7D)%0A%7D%0A%0A
+// Build the HTML for a data.json (presskit.html) file%0Afunction generatePresskitDotHTML (basePath, dataFile) %7B%0A  // Read file%0A  console.log(dataFile)%0A  fs.readFile(dataFile, function (err, json) %7B%0A    if (err) console.error(err)%0A    else %7B%0A      var presskit = JSON.parse(json)%0A      presskit.type = 'product' // TODO%0A      // Generate template%0A      build(basePath, dataFile, presskit)%0A    %7D%0A  %7D)%0A%7D%0A%0A
 // Build the HTM
@@ -2897,16 +2897,155 @@
 ny%0A  %7D%0A%0A
+  if (!templatePath) %7B%0A    console.error('Missing template! Make sure your presskit has a %22type%22 field (product/company)')%0A    return%0A  %7D%0A%0A
   var t 

Add base for data.json files