#!/usr/bin/env node
// Copyright (C) 2026 Garudex Labs.  All Rights Reserved.
// Caracal, a product of Garudex Labs
//
// Generates packages/engine/src/embedded.ts with the runtime compose YAML bundled into the binary.

import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'

const here = dirname(fileURLToPath(import.meta.url))
const coreRoot = resolve(here, '..')
const repoRoot = resolve(coreRoot, '..', '..')

function read(rel) {
  return readFileSync(resolve(repoRoot, rel), 'utf8')
}

function escape(value) {
  return value.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$\{/g, '\\${')
}

const compose = read('infra/docker/runtime-compose.yml')

const lines = []
lines.push('// Copyright (C) 2026 Garudex Labs.  All Rights Reserved.')
lines.push('// Caracal, a product of Garudex Labs')
lines.push('//')
lines.push('// Auto-generated by packages/engine/scripts/build-embedded.mjs. Do not edit by hand.')
lines.push('')
lines.push('export const COMPOSE_YML = `' + escape(compose) + '`')
lines.push('')

const outDir = resolve(coreRoot, 'src')
if (!existsSync(outDir)) mkdirSync(outDir, { recursive: true })
writeFileSync(resolve(outDir, 'embedded.ts'), lines.join('\n'), 'utf8')
process.stdout.write('caracal: wrote packages/engine/src/embedded.ts\n')
