#!/bin/sh
set -e
# Remove obsolete v1.x static APT repository list (prevents stale 1.1.1 entries in Synaptic / apt update)
if [ -f /etc/apt/sources.list.d/vielotus.list ] || [ -f /etc/apt/sources.list.d/vielotus.sources ]; then
    rm -f /etc/apt/sources.list.d/vielotus.list /etc/apt/sources.list.d/vielotus.sources 2>/dev/null || true
fi
if command -v ldconfig >/dev/null 2>&1; then
    ldconfig >/dev/null 2>&1 || true
fi
if command -v update-desktop-database >/dev/null 2>&1; then
    update-desktop-database -q /usr/share/applications >/dev/null 2>&1 || true
fi
if command -v gtk-update-icon-cache >/dev/null 2>&1; then
    gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor >/dev/null 2>&1 || true
fi
if command -v ibus >/dev/null 2>&1; then
    ibus write-cache --system >/dev/null 2>&1 || true
fi
if command -v fc-cache >/dev/null 2>&1; then
    fc-cache -f >/dev/null 2>&1 || true
fi

# Terminate stale engine workers so ibus-daemon / fcitx5 spawns the newly installed binary
if command -v pkill >/dev/null 2>&1; then
    pkill -f ibus-engine-vielotus >/dev/null 2>&1 || true
fi

echo "----------------------------------------------------------------------"
echo "Sen Việt (VieLotusIM): Đã cài đặt gói hệ thống thành công."
echo "  • Kích hoạt nhanh    : Chạy 'vielotus-settings --activate' hoặc mở ứng dụng 'Sen Việt'"
echo "  • Chuyển bộ gõ nhanh : Super + Space / Ctrl + Space"
echo "----------------------------------------------------------------------"
exit 0
