/** @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) {
                if (ns.scriptRunning('simple-hack.js', nextServer)) {
                    let maxMoney = ns.getServerMaxMoney(nextServer);
                    let currentMoney = ns.getServerMoneyAvailable(nextServer);

                    if (currentMoney < 0.2 * maxMoney) {
                        await ns.grow(nextServer);
                    }
                }
                await scanServer(nextServer, currentServer);
            }
        }
    }

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