#! /usr/bin/env node

var fs = require('fs'),
    shell = require('shelljs'),
    path = require('path'),
    program = require('commander'),
    paraview = process.env.PARAVIEW_HOME,
    pkg = require('../package.json'),
    version = /semantically-release/.test(pkg.version) ? 'development version' : pkg.version;

program
  .version(version)
  .option('-p, --port [8080]', 'Start web server with given port', 8080)
  .option('-d, --data [directory]', 'Data directory to serve')
  .option('-s, --server-only', 'Do not open the web browser\n')

  .option('--paraview [path]', 'Provide the ParaView root path to use\n')

  .parse(process.argv);

// Try to find a paraview directory inside /Applications or /opt
const pvPossibleBasePath = [];
['/Applications', '/opt', '/usr/local/opt/'].forEach(function (directoryPath) {
  shell.ls(directoryPath).forEach(function (fileName) {
    if (fileName.toLowerCase().indexOf('paraview') !== -1) {
      pvPossibleBasePath.push(path.join(directoryPath, fileName));
    }
  });
});

if(!paraview) {
    paraview = [];
    [program.paraview].concat(pvPossibleBasePath).forEach(function(directory){
        try {
            if(fs.statSync(directory).isDirectory()) {
                paraview.push(directory);
            }
        } catch(err) {
            // skip
        }
    });
}

if (!process.argv.slice(2).length || paraview.length === 0) {
    program.outputHelp();
    process.exit(0);
}

var pvPythonExecs = shell.find(paraview).filter(function(file) { return file.match(/pvpython$/) || file.match(/pvpython.exe$/); });
if(pvPythonExecs.length < 1) {
    console.log('Could not find pvpython in your ParaView HOME directory ($PARAVIEW_HOME)');
    program.outputHelp();
} else {
    const cmdLine = [
        pvPythonExecs[0], '-dr',
        path.normalize(path.join(__dirname, '../server/pvw-visualizer.py')),
        '--content', path.normalize(path.join(__dirname, '../dist')),
        '--port', program.port,
        '--data', program.data,
    ];

    console.log('\n===============================================================================');
    console.log('| Execute:');
    console.log('| $', cmdLine.join('\n|\t'));
    console.log('===============================================================================\n');
    shell.exec(cmdLine.join(' '), { async: true }).stdout.on('data', function(data) {
        if (data.indexOf('Starting factory') !== -1) {
            // Open browser if asked
            if (!program.serverOnly) {
                require('open')('http://localhost:' + program.port);
            }
        }
    });
}



@@ -310,16 +310,71 @@
 rsion;%0A%0A
+function quotePath(str) %7B%0A  return '%22' + str + '%22';%0A%7D%0A%0A
 program%0A
@@ -1415,29 +1415,21 @@
  %7C%7C 
-paraview.length === 0
+!program.help
 ) %7B%0A
@@ -1799,16 +1799,26 @@
         
+quotePath(
 pvPython
@@ -1825,16 +1825,17 @@
 Execs%5B0%5D
+)
 , '-dr',
@@ -1839,24 +1839,34 @@
 r',%0A        
+quotePath(
 path.normali
@@ -1920,16 +1920,17 @@
 er.py'))
+)
 ,%0A      
@@ -1944,16 +1944,26 @@
 ntent', 
+quotePath(
 path.nor
@@ -2001,16 +2001,17 @@
 /dist'))
+)
 ,%0A      
@@ -2054,16 +2054,26 @@
 -data', 
+quotePath(
 program.
@@ -2076,16 +2076,17 @@
 ram.data
+)
 ,%0A    %5D;

allow path with space