Skip to content

Commit

Permalink
use simpler find for resource constrain Linuxes
Browse files Browse the repository at this point in the history
  • Loading branch information
ekarak committed Jan 17, 2017
1 parent 8efd212 commit ee97b45
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions lib/ozwpaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,68 +22,66 @@ var cp = require('child_process');
var util = require('util');
var path = require('path');

function doScript(script, suppressException) {
//console.log('---> ' + script);
try {
return(cp.execSync(script).toString());
} catch(e) {
//console.log('---> ' + e.toString());
if (!suppressException) throw e;
}
}

var configs = {
includedir: {
findpattern: 'OZWException.h',
locations: ["/usr/include/", "/usr/local/include/"],
default: "/usr/local/include/openzwave/"
locations: ["/usr/local/include", "/usr/include"]
},
libdir: {
findpattern: 'libopenzwave.so',
locations: ["/usr/lib*", "/usr/local/lib*"],
default: "/usr/local/lib64/"
locations: ["/usr/local/lib64", "/usr/local/lib", "/usr/lib64", "/usr/lib"]
},
sysconfdir: {
findpattern: 'zwcfg.xsd',
locations: ["/usr/etc/", "/usr/local/etc/", "/etc/"],
default: "/usr/local/etc/openzwave/"
locations: ["/usr/local/etc", "/usr/etc", "/etc"]
},
docdir: {
findpattern: 'openzwave-*',
locations: ["/usr/local/share/doc/"],
default: "/usr/local/share/doc/openzwave-1.4.2278"
findpattern: 'Doxyfile.in',
locations: ["/usr/local/share/doc", "/usr/doc"]
}
}

var usepkgconfig = true;
var usepkgconfig = false;
try {
cp.execSync("pkg-config --exists libopenzwave");
} catch (e) {
usepkgconfig = false;
}

function getConfigItem(item) {
// 1. try using pkg-config
// 1. try the easy way first: use pkg-config and libopenzwave.pc
if (usepkgconfig) {
var cmdfmt = "pkg-config --variable=%s libopenzwave";
var cmd = util.format(cmdfmt, item);
return (cp.execSync(cmd).toString().split('\n')[0]);
} else {
// 2. try using find
var cmdfmt = "find %s ! -readable -prune -o -name %s -print";
var cmdfmt = "find %s -name %s";
var fp = configs[item].findpattern;
for (var i in configs[item].locations) {
var loc = configs[item].locations[i];
if (!fs.existsSync(loc)) continue;
var cmd = util.format(cmdfmt, loc, fp);
try {
var dir = cp.execSync(cmd).toString().split('\n')[0];
if (!dir) continue;
return path.dirname(dir)
} catch (e) {
// console.log('not found in %s', loc);
}
var dir = doScript(cmd, true);
if (!dir) continue;
return path.dirname(dir.split('\n')[0]);
}
}
}

var paths = {};

// try the easy way first: use pkg-config and libopenzwave.pc
Object.keys(configs).forEach(function(item) {
var ret = getConfigItem(item);
// or fallback to the default value if automatic scan fails
paths[item] = ret || configs[item].default;
paths[item] = getConfigItem(item);
});

//console.log('=====RESULTS=====');
Expand Down

0 comments on commit ee97b45

Please sign in to comment.