#! /usr/bin/env node

var Q = require('q');
var _ = require('lodash');
var path = require('path');
var prog = require('commander');
var tmp = require('tmp');

var pkg = require('../package.json');
var generators = require("../lib/generate").generators;
var fs = require('../lib/generate/fs');

var utils = require('./utils');
var buildFunc = require('./build');

// General options
prog
.version(pkg.version);

var buildCommand = function(command, action) {
    return command
    .option('-o, --output <directory>', 'Path to output directory, defaults to ./_book')
    .option('-f, --format <name>', 'Change generation format, defaults to site, availables are: '+_.keys(generators).join(", "))
    .option('-t, --title <name>', 'Name of the book to generate, defaults to repo name')
    .option('-i, --intro <intro>', 'Description of the book to generate')
    .option('-g, --github <repo_path>', 'ID of github repo like : username/repo')
    .option('-gh, --githubHost <url>', 'The url of the github host (defaults to https://github.com/')
    .option('--theme <path>', 'Path to theme directory')
    .action(action);
}


var buildFunc;

buildCommand(prog
.command('build [source_dir]')
.description('Build a gitbook from a directory'), buildFunc);

buildCommand(prog
.command('serve [source_dir]')
.description('Build then serve a gitbook from a directory')
.option('-p, --port <port>', 'Port for server to listen on', 4000),
function(dir, options) {
    buildFunc(dir, options)
    .then(function(_options) {
        console.log();
        console.log('Starting server ...');
        return utils.serveDir(_options.output, options.port)
        .fail(utils.logError);
    })
    .then(function() {
        console.log('Serving book on http://localhost:'+options.port);
        console.log();
        console.log('Press CTRL+C to quit ...');
    });
});

buildCommand(prog
.command('pdf [source_dir] [output_file]')
.description('Build a gitbook as a PDF')
.option('-pf, --paperformat <format>', 'PDF paper format (default is A4): "5in*7.5in", "10cm*20cm", "A4", "Letter"'),
function(dir, outputFile, options) {
    outputFile = outputFile || path.resolve(dir, "book.pdf");

    Q.nfcall(tmp.dir)
    .then(function(tmpDir) {
        return buildFunc(
            dir,
            _.extend(options, {
                output: tmpDir,
                format: "pdf"
            })
        )
        .then(function() {
            console.log("Generating PDF in", outputFile);
            return fs.copy(
                path.join(tmpDir, "index.pdf"),
                outputFile
            )
            return fs.remove(tmpDir)
            .fail(utils.logError);
        });
    })
    
});



// Parse and fallback to help if no args
if(_.isEmpty(prog.parse(process.argv).args) && process.argv.length === 2) {
    prog.help();
}

@@ -2399,36 +2399,405 @@
  .then(function(
+_options
 ) %7B%0A
+            var copyPDF = function(lang) %7B%0A                var _outputFile = outputFile;%0A                var _tmpDir = tmpDir;%0A%0A                if (lang) %7B%0A                    _outputFile = _outputFile.slice(0, -path.extname(_outputFile).length)+%22_%22+lang+path.extname(_outputFile);%0A                    _tmpDir = path.join(_tmpDir, lang);%0A                %7D%0A%0A    
             cons
@@ -2825,16 +2825,17 @@
 DF in%22, 
+_
 outputFi
@@ -2843,32 +2843,36 @@
 e);%0A            
+   
+ 
 return fs.copy(%0A
@@ -2887,16 +2887,20 @@
         
+   
+ 
 path.joi
@@ -2901,16 +2901,17 @@
 th.join(
+_
 tmpDir, 
@@ -2936,24 +2936,29 @@
             
+  
+  _
 outputFile%0A 
@@ -2972,45 +2972,632 @@
     
-)%0A            return fs.remove(tmpDir
+    );%0A            %7D;%0A%0A            // Multi-langs book%0A            return Q()%0A            .then(function() %7B%0A                if (_options.langsSummary) %7B%0A                    console.log(%22Generating PDFs for all the languages%22);%0A                    return Q.all(%0A                        _.map(_options.langsSummary.list, function(lang) %7B%0A                            return copyPDF(lang.lang);%0A                        %7D)%0A                    );%0A                %7D else %7B%0A                    return copyPDF();%0A                %7D%0A            %7D)%0A            .then(function() %7B%0A                return fs.remove(tmpDir);%0A            %7D
 )%0A  

Add support of multi-languages book in command pdf