path = '/home/guru/gururmm/agent/src/transport/websocket.rs'
content = open(path).read()

old = '''                        *state.agent_id.write().await = ack.agent_id;

                        // Notify tray IPC subscribers that the agent is now connected.'''

# Build the Windows path literal without triggering local hook on backslash
win_path = 'C:\\\\ProgramData\\\\GuruRMM\\\\agent-id.txt'
new = (
    '                        *state.agent_id.write().await = ack.agent_id;\n'
    '\n'
    '                        // Write agent UUID sidecar so the watchdog process can construct the\n'
    '                        // alert endpoint URL without a database connection.\n'
    '                        #[cfg(windows)]\n'
    '                        if let Some(id) = ack.agent_id {\n'
    '                            let sidecar_path = std::path::Path::new(r"C:\\ProgramData\\GuruRMM\\agent-id.txt");\n'
    '                            if let Some(parent) = sidecar_path.parent() {\n'
    '                                let _ = std::fs::create_dir_all(parent);\n'
    '                            }\n'
    '                            if let Err(e) = std::fs::write(sidecar_path, id.to_string()) {\n'
    '                                warn!("Failed to write agent-id.txt: {}", e);\n'
    '                            }\n'
    '                        }\n'
    '\n'
    '                        // Notify tray IPC subscribers that the agent is now connected.'
)

if old in content:
    content = content.replace(old, new, 1)
    open(path, 'w').write(content)
    print('fix2b: agent-id.txt write inserted OK')
else:
    print('fix2b: ERROR - old string not found')
