import { $ } from "bun"

const REMOTE = "iago@iago-mac-mini.local"
const REMOTE_PATH = "/Users/iago/iago"
const LOCAL_PATH = "."

async function deploy() {
    await $`rsync -avz --exclude=.env --exclude=node_modules --exclude=.git ${LOCAL_PATH} ${REMOTE}:${REMOTE_PATH}`
}

async function start() {
    await $`ssh ${REMOTE} 'cd ${REMOTE_PATH} && nohup bun run start > out.log 2>&1 &'`
}

async function stop() {
    await $`ssh ${REMOTE} 'pkill -f "bun run start" || true'`
}

async function main() {
    const cmd = Bun.argv[2]
    if (cmd === "deploy") {
        await deploy()
    } else if (cmd === "start") {
        await start()
    } else if (cmd === "stop") {
        await stop()
    } else {
        console.log("Usage: bun run deploy|start|stop")
        process.exit(1)
    }
}

await main()