import { readFile, writeFile, mkdir, copyFile } from 'node:fs/promises' import { dirname, join } from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' const __dirname = dirname(fileURLToPath(import.meta.url)) const repoRoot = join(__dirname, '../..') const kernelBin = join(repoRoot, 'kernel/bin') const seederKernelBin = join(repoRoot, 'packages/bare-os-seeder/kernel/bin') const kernelMan = join(repoRoot, 'kernel/share/man/man.json') const seederMan = join( repoRoot, 'packages/bare-os-seeder/kernel/share/man/man.json' ) const runtimePath = join( repoRoot, 'packages/bare-os-coreutils/lib/runtime.js' ) const coreutilsCommands = join( repoRoot, 'packages/bare-os-coreutils/lib/commands.mjs' ) export async function build() { const runtime = await readFile(runtimePath, 'utf8') const body = await readFile(join(__dirname, 'src/sshd.js'), 'utf8') const out = runtime + '\n' + body await mkdir(kernelBin, { recursive: true }) await mkdir(seederKernelBin, { recursive: true }) await writeFile(join(kernelBin, 'sshd'), out) await writeFile(join(seederKernelBin, 'sshd'), out) await writeFile(join(kernelBin, 'bare-sshd'), out) await writeFile(join(seederKernelBin, 'bare-sshd'), out) const etcSsh = join(repoRoot, 'kernel/etc/ssh') const seederEtc = join(repoRoot, 'packages/bare-os-seeder/kernel/etc/ssh') await mkdir(etcSsh, { recursive: true }) await mkdir(seederEtc, { recursive: true }) const cfgSrc = join(__dirname, 'etc/ssh/sshd_config') await copyFile(cfgSrc, join(etcSsh, 'sshd_config')) await copyFile(cfgSrc, join(seederEtc, 'sshd_config')) const { COREUTILS_COMMANDS } = await import( pathToFileURL(coreutilsCommands).href ) const manRaw = await readFile(kernelMan, 'utf8') const db = JSON.parse(manRaw) const pages = Array.isArray(db.pages) ? [...db.pages] : [] for (const name of ['sshd', 'sshd_config']) { const add = JSON.parse( await readFile(join(__dirname, 'man/pages', `${name}.json`), 'utf8') ) const i = pages.findIndex((p) => p && p.name === name) if (i >= 0) pages[i] = add else pages.push(add) } db.pages = pages db.generatedAt = new Date().toISOString() const manOut = JSON.stringify(db, null, 2) + '\n' await mkdir(dirname(kernelMan), { recursive: true }) await mkdir(dirname(seederMan), { recursive: true }) await writeFile(kernelMan, manOut) await writeFile(seederMan, manOut) const posixPath = join(repoRoot, 'kernel/etc/bare-os/posix_utilities.json') const seederPosix = join( repoRoot, 'packages/bare-os-seeder/kernel/etc/bare-os/posix_utilities.json' ) let posix = {} try { posix = JSON.parse(await readFile(posixPath, 'utf8')) } catch { posix = { schema: 2, commandIndex: [], utilities: {} } } const idx = Array.isArray(posix.commandIndex) ? [...posix.commandIndex] : [] const have = new Set(idx.map((r) => r.name)) for (const name of COREUTILS_COMMANDS) { if (!have.has(name)) { idx.push({ name, tier: 'tier1_bin' }) have.add(name) } } if (!have.has('bare-sshd')) { idx.push({ name: 'bare-sshd', tier: 'tier1_bin' }) } idx.sort((a, b) => a.name.localeCompare(b.name)) posix.commandIndex = idx await writeFile(posixPath, JSON.stringify(posix, null, 2) + '\n') await writeFile(seederPosix, JSON.stringify(posix, null, 2) + '\n') } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { await build() }