import fs from 'node:fs';
import path from 'node:path';
import { getYagrHomeDir } from './yagr-home.js';
export function getGatewayPidPath() {
    return path.join(getYagrHomeDir(), 'gateway.pid');
}
export function getGatewayLogPath() {
    return path.join(getYagrHomeDir(), 'gateway.log');
}
export function writeGatewayPid(pid) {
    fs.writeFileSync(getGatewayPidPath(), String(pid), 'utf8');
}
export function readGatewayPid() {
    try {
        const raw = fs.readFileSync(getGatewayPidPath(), 'utf8').trim();
        const pid = parseInt(raw, 10);
        return Number.isNaN(pid) ? undefined : pid;
    }
    catch {
        return undefined;
    }
}
export function clearGatewayPid() {
    try {
        fs.unlinkSync(getGatewayPidPath());
    }
    catch { /* already gone */ }
}
export function isGatewayRunning() {
    const pid = readGatewayPid();
    if (pid === undefined)
        return { running: false };
    try {
        process.kill(pid, 0);
        return { running: true, pid };
    }
    catch {
        clearGatewayPid();
        return { running: false };
    }
}
//# sourceMappingURL=gateway-daemon.js.map