var glob = require('glob');
var fs = require('fs');
var path = require('path');
var os = require('os');

require('find-java-home')(function(err, home){
  var dll;
  var dylib;
  var so,soFiles;
  var binary;
  var newHome = process.platform === 'darwin' ? '/Applications/LowCode.app/Contents/Resources/app/plugins/lcap-extension-node-server-0.0.1/extension/lib/jdk1.8.0_101' : home;
  if(newHome){
    dll = glob.sync('**/jvm.dll', {cwd: newHome})[0];
    dylib = glob.sync('**/libjli.dylib', {cwd: newHome})[0];
    soFiles = glob.sync('**/libjvm.so', {cwd: newHome});
    
    if(soFiles.length>0)
      so = getCorrectSoForPlatform(soFiles);

    binary = dll || dylib || so;

    fs.writeFileSync(
      path.resolve(__dirname, './build/jvm_dll_path.json'),
      binary
      ? JSON.stringify(
          path.delimiter
          + path.dirname(path.resolve(newHome, binary))
        )
      : '""'
    );
  }
});

function getCorrectSoForPlatform(soFiles){
  var so = _getCorrectSoForPlatform(soFiles);
  if (so) {
    so = removeDuplicateJre(so);
  }
  return so;
}

function removeDuplicateJre(filePath){
  while(filePath.indexOf('jre/jre')>=0){
    filePath = filePath.replace('jre/jre','jre');
  }
  return filePath;
}

function _getCorrectSoForPlatform(soFiles){
  
  var architectureFolderNames = {
    'ia32': 'i386',
    'x64': 'amd64'
  };

  if(os.platform() != 'sunos')
    return soFiles[0];

  var requiredFolderName = architectureFolderNames[os.arch()];

  for (var i = 0; i < soFiles.length; i++) {
    var so = soFiles[i];

    if(so.indexOf('server')>0)
      if(so.indexOf(requiredFolderName)>0)
        return so;
  }

  return soFiles[0];
}
