import { execFileSync } from 'node:child_process'
import { resolve } from 'node:path'

const root = resolve(new URL('..', import.meta.url).pathname)
const profile = process.env.FREECAD_ORACLE_PROFILE || 'headless'
if (!['headless', 'desktop'].includes(profile)) throw new Error(`Unknown FreeCAD oracle profile: ${profile}`)
const build = resolve(root, profile === 'desktop' ? '.cache/freecad/build-desktop' : '.cache/freecad/build-native')
const install = resolve(root, profile === 'desktop' ? '.cache/freecad/install-desktop' : '.cache/freecad/install-native')
const jobs = process.env.FREECAD_BUILD_JOBS || '4'
execFileSync('cmake', ['--build', build, '--parallel', jobs], { cwd: root, stdio: 'inherit' })
execFileSync('cmake', ['--install', build], { cwd: root, stdio: 'inherit' })
console.log(`FreeCAD ${profile} oracle installed: ${install}`)
