SERVICE_HELPER="/opt/codex-desktop/update-builder/packaging/linux/codex-update-manager-user-service.sh"
DESKTOP_ENTRY_DOCTOR="/opt/codex-desktop/.codex-linux/codex-desktop-entry-doctor.sh"
if [ -f "$SERVICE_HELPER" ]; then
    # shellcheck source=/opt/codex-desktop/update-builder/packaging/linux/codex-update-manager-user-service.sh
    . "$SERVICE_HELPER"
fi
if [ -f "$DESKTOP_ENTRY_DOCTOR" ]; then
    # shellcheck source=/opt/codex-desktop/.codex-linux/codex-desktop-entry-doctor.sh
    . "$DESKTOP_ENTRY_DOCTOR"
fi

post_install() {
    if command -v update-desktop-database >/dev/null 2>&1; then
        update-desktop-database /usr/share/applications >/dev/null 2>&1 || true
    fi
    if [ -f "$DESKTOP_ENTRY_DOCTOR" ]; then
        codex_desktop_repair_system_package_shadow_entries codex-desktop || true
    fi
    if [ -f "$SERVICE_HELPER" ]; then
        codex_ensure_user_service_running || true
    fi
}

post_upgrade() {
    post_install
}

pre_remove() {
    if [ -f "$SERVICE_HELPER" ]; then
        codex_cleanup_user_service stop || true
        codex_cleanup_user_service disable || true
    fi
}

post_remove() {
    if [ -f "$SERVICE_HELPER" ]; then
        codex_reload_user_managers || true
    fi
}
