/** @param {import(".").NS } ns */
const doc = eval('document');

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

    const terminalInput = doc.getElementById("terminal-input");
    const handler = Object.keys(terminalInput)[1];

    async function scanServer(currentServer, previousServer = currentServer, connectString = '', scanLevel = 0) {
        let availableServers = ns.scan(currentServer);
        let nextConnectString = connectString + `connect ${currentServer}; `;

        let backdoorCommand = nextConnectString;
        backdoorCommand += 'run BruteSSH.exe; ';
        backdoorCommand += 'run FTPCrack.exe; ';
        backdoorCommand += 'run relaySMTP.exe; ';
        backdoorCommand += 'run HTTPWorm.exe; ';
        backdoorCommand += 'run SQLInject.exe; ';
        backdoorCommand += 'run NUKE.exe; ';
        backdoorCommand += 'backdoor';

        terminalInput.value = backdoorCommand;
        terminalInput[handler].onChange({ target: terminalInput });
        terminalInput[handler].onKeyDown({ key: 'Enter', preventDefault: () => null });
        await ns.sleep(60000)

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

            if (nextServer != currentServer && nextServer != previousServer) {
                await scanServer(nextServer, currentServer, nextConnectString, scanLevel + 1);
            }
        }
    }
    
    await scanServer('home');
    terminalInput.value = 'home';
    terminalInput[handler].onChange({ target: terminalInput });
    terminalInput[handler].onKeyDown({ key: 'Enter', preventDefault: () => null });
}