#!/usr/bin/env node
/* -*- Mode:Js */
/* vim: set expandtab ts=4 sw=4: */
/*
 * You may redistribute this program and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

var PublicToIp6 = require('./lib/publicToIp6');
var Cjdns = require('./lib/cjdnsadmin/cjdnsadmin');
var nThen = require('nthen');

var printSession = function (session) {
    var state = session.state.replace(/CryptoAuth_/,'');
    while (state.length < ('ESTABLISHED').length) { state = state + ' ' }
    var addr = session.addr;
    if (process.argv.indexOf('--ip6') !== -1) {
        addr = addr.replace(/[a-z0-9]{52}.k/, PublicToIp6.convert);
    }
    var out = [ addr, state, session.handle, session.sendHandle, Number(session.metric).toString(16) ];
    if (Number(session.duplicates) !== 0) { out.push(' DUP ', session.duplicates); }
    if (Number(session.lostPackets) !== 0) { out.push(' LOS ', session.lostPackets); }
    if (Number(session.receivedOutOfRange) !== 0) { out.push(' OOR ', session.receivedOutOfRange); }
    if (Number(session.timeOfLastUsage) !== 0) { out.push('USE'); }
    console.log(out.join(' '));
};

var cjdns;
var handles = [];
var sessions = [];

nThen(function (waitFor) {

    Cjdns.connectWithAdminInfo(waitFor(function (c) { cjdns = c; }));

}).nThen(function (waitFor) {

    var more = function (i) {
        cjdns.SessionManager_getHandles(i, waitFor(function (err, ret) {
            if (err) { throw err; }
            handles.push.apply(handles, ret.handles);
            if (ret.more) { more(i+1); }
        }));
    };
    more(0);

}).nThen(function (waitFor) {

    var next = function (i) {
        cjdns.SessionManager_sessionStats(Number(handles[i]), waitFor(function (err, ret) {
            if (err) { throw err; }
            sessions.push(ret);
            i++
            if (i < handles.length) { next(i); }
        }));
    };
    if (handles.length) {
        next(0);
    } else {
        console.log("No active sessions");
    }

}).nThen(function (waitFor) {
    cjdns.disconnect();

    sessions.sort(function (a, b) {
        return (a.addr.substring(a.addr.indexOf('.')) < b.addr.substring(b.addr.indexOf('.')))
            ? -1 : 1;
    });
    for (var i = 0; i < sessions.length; i++) {
        printSession(sessions[i]);
    }
});
