/**
 * @file get local mac address
 * @author liulangyu(liulangyu90316@gmail.com)
 */

var execSync = require('child_process').execSync;
var platform = process.platform;

module.exports = (function () {
    var cmd = {
        win32: 'getmac',
        darwin: 'ifconfig -a',
        linux: 'ifconfig -a || ip link'
    }[platform];

    var regStr = '((?:[a-z0-9]{2}[:-]){5}[a-z0-9]{2})';

    var macReg = new RegExp('ether\\s' + regStr + '\\s', 'i');

    try {
        var data = execSync(cmd).toString();
        var res = {
            win32: new RegExp(regStr, 'i').exec(data),
            darwin: macReg.exec(data),
            linux: macReg.exec(data)
        }[platform];


        if (res) {
            return res[1];
        }
    }
    catch (e) {
        return '';
    }
})();
