var program = require('commander');
var AlfrescoApi = require('alfresco-js-api-node');
var http = require('http');

var fs = require('fs');
var path = require('path');
var archiver = require('archiver');
var unzipper = require('unzipper');

var exec = require('child_process').exec;

replaceHrefInIndex = (folder) => {
    fs.readFile(`demo-shell/${folder}/index.html`, 'utf8', function (err, data) {
        if (err) {
            return console.log(err);
        }

        var result = data.replace(`base href="/"`, `base href=\"/${folder}/\"`);

        fs.writeFile(`demo-shell/${folder}/index.html`, result, 'utf8', function (err) {
            if (err) return console.log(err);
        });
    });
}

async function main() {

    program
        .version('0.1.0')
        .option('-p, --password [type]', 'password')
        .option('-u, --username  [type]', 'username')
        .option('--base-href  [type]', '')
        .option('-f, --folder [type]', 'Name of the folder')
        .option('-host, --host [type]', 'URL of the CS')
        .parse(process.argv);

    let alfrescoJsApi = new AlfrescoApi({
        provider: 'ECM',
        hostEcm: program.host
    });

    alfrescoJsApi.login(program.username, program.password);

    let zipDemoNode;

    try {
        zipDemoNode = await alfrescoJsApi.nodes.getNode('-my-', {
            'relativePath': `Builds/${program.folder}/demo.zip`
        });
    } catch (error) {
        console.log('error:  ' + error);
    }

    const url = await alfrescoJsApi.content.getContentUrl(zipDemoNode.entry.id, true);

    console.log('Download zip');

    let outputFolder = program.baseHref ? program.baseHref : 'dist';

    var file = fs.createWriteStream('demo.zip');
    http.get(`http://${url}`, (response) => {
        response.pipe(file);
        file.on('finish', async () => {
            console.log('Unzip Demo ' + path.join(__dirname, '../demo.zip'));
            fs.createReadStream(path.join(__dirname, '../demo.zip'))
                .pipe(unzipper.Extract({path: path.join(__dirname, '../demo-shell')}))
                .on('finish', () => {

                    console.log(`mv demo-shell/demo.zip demo-shell/${outputFolder}`);

                    exec(`mv demo-shell/demo.zip demo-shell/${outputFolder}`, (err, stdout, stderr) => {
                        if (err) {
                            console.log(`err: ${err}`);
                            return;
                        }

                        if(program.baseHref) {
                            replaceHrefInIndex(outputFolder);
                        }
                    });
                })
        });
    });

}

main();

@@ -2128,44 +2128,132 @@
     
-console.log(%60mv demo-shell/demo.zip 
+let oldFolder = path.join(__dirname, %60../demo-shell/demo.zip%60)%0A                    let newFolder = path.join(__dirname, %60../
 demo
@@ -2276,17 +2276,16 @@
 older%7D%60)
-;
 %0A%0A      
@@ -2302,56 +2302,101 @@
     
-exec(%60mv demo-shell/demo.zip demo-shell/$%7Boutput
+console.log(%60mv $%7BoldFolder%7D $%7BnewFolder%7D%60);%0A%0A                    exec(%60mv $%7BoldFolder%7D $%7Bnew
 Fold

improve script download dist