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

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

    let host = ns.args[0];
    let remote = ns.args[1];
    let script = 'simple-hack.js';

    if (!ns.serverExists(host)) {
        ns.print(`Host '${host}'' doesn't exist!`);
        return 1;
    }

    if (!ns.serverExists(remote)) {
        ns.print(`Remote '${remote}'' doesn't exist!`);
        return 2;
    }

    if (!ns.fileExists(script, host)) {
        if (!ns.fileExists(script, 'home')) {
            ns.print(`Script '${script}' doesn't exist!`);
            return 3;
        }

        ns.scp(script, host, 'home');
    }

    let serverMaxRam = ns.getServerMaxRam(host);
    let serverUsedRam = ns.getServerUsedRam(host);
    let serverFreeRam = serverMaxRam - serverUsedRam;
    let scriptRam = ns.getScriptRam(script, host);
    let maxThreads = Math.floor(serverFreeRam / scriptRam);
    if(maxThreads == 0)
    {
        ns.print(`Not enough ram to run '${script}', ${scriptRam}/${serverFreeRam}!`)
    }

    ns.scriptKill(script, host);

    if (!ns.exec(script, host, maxThreads, remote)) {
        ns.tprint(`Exec error running script '${script} on '${host}!`);
    }

    
}
