#!/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 Cjdns = require('./lib/cjdnsadmin/cjdnsadmin');
var nThen = require('nthen');

var dump = function (millName) {

var cjdns;
nThen(function (waitFor) {
    Cjdns.connectWithAdminInfo(waitFor(function (x) { cjdns = x; }));
}).nThen(function (waitFor) {
    var list = [];
    var again = function (i) {
        cjdns.Janitor_dumpRumorMill(i, millName, function (err, out) {
            if (err) { throw err; }
            list.push.apply(list, out.addresses);
            if (out.addresses.length) {
                again(i+1);
            } else {
                list.sort();
                list.forEach(function (addr) { console.log(addr); });
                console.log("total " + out.total);
                cjdns.disconnect();
            }
        });
    };
    again(0);
});

};

var MILLS = ['externalMill','linkMill','nodeMill','dhtMill','splitMill'];

var main = function (args) {
    if (MILLS.indexOf(args[args.length-1]) !== -1) {
        dump(args[args.length-1]);
        return;
    }
    console.log("Usage:");
    console.log("    dumpRumorMill externalMill # dump external mill (highest priority)");
    console.log("    dumpRumorMill linkMill     # dump high-priority mill for improving routes");
    console.log("    dumpRumorMill nodeMill     # dump new node discovery mill");
    console.log("    dumpRumorMill dhtMill      # dump DHT maintanence mill");
    console.log("    dumpRumorMill splitMill    # dump link splitting mill");
};
main(process.argv);
