/** @param {import(".").NS } ns */

export async function main(ns) {
    // ns.disableLog('ALL');

    // TODO: add increasing threshold if no servers lower than current threshold
    async function scanServer(currentServer, previousServer = currentServer) {
        let availableServers = ns.scan(currentServer);

        for (let i = 0; i < availableServers.length; ++i) {
            let nextServer = availableServers[i];

            if (nextServer != currentServer && nextServer != previousServer) {
                let currentMoney = ns.getServerMoneyAvailable(nextServer);
                if (ns.hasRootAccess(nextServer) && currentMoney > 0) {
                    await ns.grow(nextServer);
                }
                await scanServer(nextServer, currentServer);
            }
        }
    }

    while (true) {
        ns.print(`Running scan`);
        await scanServer('home');
        await ns.sleep(1000);
    }
}