"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const child_process_1 = require("child_process");
const checkAuth_1 = require("../middleware/checkAuth");
const router = (0, express_1.Router)();
router.post('/update', checkAuth_1.checkAuth, (req, res) => {
    try {
        console.log('[DAEMON] Received request to start update process.');
        // Using `spawn` with `detached: true` and `stdio: 'ignore'` allows the parent (daemon)
        // to exit while the child (update script) continues running. `unref()` is crucial.
        const child = (0, child_process_1.spawn)('repoburg', ['update'], {
            detached: true,
            stdio: 'ignore',
            shell: true,
        });
        child.unref();
        res.status(202).json({ message: 'Update process initiated. The daemon and services will restart shortly.' });
    }
    catch (error) {
        console.error('Failed to spawn update process:', error);
        res.status(500).json({ error: 'Failed to start update process.' });
    }
});
exports.default = router;
