/**
 * hermes-kg-hook.ts — install / uninstall the kg-first.sh hook for Hermes.
 *
 * Shared by:
 *   - `olam hermes bootstrap` (via runHermesBootstrap)
 *   - `olam kg install-hook --for hermes`
 *   - `olam kg uninstall-hook --for hermes`
 *
 * Writes the bundled kg-first.sh template from hermes-bundle/ into
 * ~/.hermes/hooks/kg-first.sh and makes it executable. Idempotent:
 * re-installing over an identical file is detected via content hash.
 *
 * HERMES_KG_HOOK_SENTINEL is embedded in the script body (the comment
 * `# OLAM_KG_HOOK` present in the template). It is the stable string
 * both this module and the config-entry writer use to detect presence.
 */
import * as fs from 'node:fs';
import * as path from 'node:path';
import { createHash } from 'node:crypto';
import { fileURLToPath } from 'node:url';
/** Stable sentinel that appears in both the hook script and config entry. */
export const HERMES_KG_HOOK_SENTINEL = 'OLAM_KG_HOOK';
function bundleDir() {
    // Resolve hermes-bundle/ relative to this source file.
    // In the published tarball: dist/commands/hermes-kg-hook.js → ../../hermes-bundle/
    // In source (tsx): src/commands/hermes-kg-hook.ts → ../../hermes-bundle/
    const thisFile = fileURLToPath(import.meta.url);
    return path.resolve(path.dirname(thisFile), '..', '..', 'hermes-bundle');
}
function templateContent() {
    const templatePath = path.join(bundleDir(), 'kg-first.sh');
    return fs.readFileSync(templatePath);
}
function sha256(buf) {
    return createHash('sha256').update(buf).digest('hex');
}
/**
 * Install kg-first.sh into hooksDir.
 * Returns 'already-present' when an identical script is already there.
 * Returns 'dry-run' when dryRun=true.
 */
export function installKgFirstHookForHermes(hooksDir, dryRun) {
    const destPath = path.join(hooksDir, 'kg-first.sh');
    const template = templateContent();
    // Idempotency: compare content hashes
    if (fs.existsSync(destPath)) {
        const existing = fs.readFileSync(destPath);
        if (sha256(existing) === sha256(template)) {
            return { status: 'already-present', destPath };
        }
    }
    if (dryRun) {
        return { status: 'dry-run', destPath };
    }
    fs.mkdirSync(hooksDir, { recursive: true });
    fs.writeFileSync(destPath, template);
    fs.chmodSync(destPath, 0o755);
    return { status: 'installed', destPath };
}
/**
 * Uninstall: remove kg-first.sh if it contains the sentinel.
 * Returns 'already-present' (noop) when the file doesn't exist or lacks the sentinel.
 */
export function uninstallKgFirstHookForHermes(hooksDir, dryRun) {
    const destPath = path.join(hooksDir, 'kg-first.sh');
    if (!fs.existsSync(destPath)) {
        return { status: 'already-present', destPath };
    }
    const content = fs.readFileSync(destPath, 'utf-8');
    if (!content.includes(HERMES_KG_HOOK_SENTINEL)) {
        // Not ours — do not remove
        return { status: 'already-present', destPath };
    }
    if (dryRun) {
        return { status: 'dry-run', destPath };
    }
    fs.unlinkSync(destPath);
    return { status: 'installed', destPath };
}
//# sourceMappingURL=hermes-kg-hook.js.map