#!/usr/bin/env node
import { loadRemoteConfig } from "../lib/remote-build.mjs";
import { installGcTimer } from "../lib/remote-gc-timer.mjs";

const args = process.argv.slice(2);
if (args.includes("-h") || args.includes("--help")) {
  console.error("usage: install-remote-gc-timer [host...]");
  process.exit(0);
}

const cfg = loadRemoteConfig();
if (!cfg) {
  console.error("install-remote-gc-timer: remote config disabled or missing");
  process.exit(2);
}
const hosts = args.length ? args : cfg.hosts;
if (!hosts.length) {
  console.error("install-remote-gc-timer: no hosts configured");
  process.exit(2);
}

const { ok, results } = installGcTimer({ ...cfg, hosts });
for (const entry of results) {
  console.log(`${entry.ok ? "OK" : "FAIL"} ${entry.host}: ${entry.ok ? entry.stdout.split("\n").pop() : entry.stderr}`);
}
process.exit(ok ? 0 : 1);
