#!/usr/bin/env bash
# Read-only metadata collection. Run as the ordinary logged-in desktop user.
# Does not restart frameworks, open the settings app, upload data or read word lists.
set -u
if [ "$(id -u)" -eq 0 ]; then
  printf '请在日常桌面用户的终端运行，不要 sudo；否则不能判断该用户的输入法会话。\n' >&2
  exit 2
fi
section() { printf '\n[%s]\n' "$1"; }
section system
date -Is
uname -m
if [ -r /etc/os-release ]; then
  grep -E '^(NAME|VERSION|VERSION_ID|PRETTY_NAME|ID|ID_LIKE)=' /etc/os-release
fi
getconf GNU_LIBC_VERSION
section desktop
printf 'XDG_CURRENT_DESKTOP=%s\nXDG_SESSION_TYPE=%s\nGTK_IM_MODULE=%s\nQT_IM_MODULE=%s\nXMODIFIERS=%s\n' \
  "${XDG_CURRENT_DESKTOP-}" "${XDG_SESSION_TYPE-}" "${GTK_IM_MODULE-}" "${QT_IM_MODULE-}" "${XMODIFIERS-}"
for task in fcitx fcitx5 ibus-daemon; do
  pgrep -l -u "$(id -u)" -x "$task" || true
done
section session-bus
# ListNames never activates a missing framework, unlike calling its controller.
if command -v gdbus >/dev/null; then
  gdbus call --session --dest org.freedesktop.DBus --object-path /org/freedesktop/DBus \
    --method org.freedesktop.DBus.ListNames 2>/dev/null | \
    tr ',' '\n' | grep -Ei 'fcitx|ibus' || true
fi
section packages
if command -v dpkg-query >/dev/null; then
  dpkg-query -W -f='${binary:Package}\t${Version}\t${Architecture}\n' \
    'maquan-ime*' '*fcitx*' 'ibus*' 'libgtk-3-0*' 'libstdc++6' 2>/dev/null || true
elif command -v rpm >/dev/null; then
  rpm -qa --qf '%{NAME}\t%{VERSION}-%{RELEASE}\t%{ARCH}\n' | \
    grep -E '^(maquan-ime|fcitx|ibus|gtk3|libstdc\+\+)' || true
fi
section entrypoints
command -v jdime-settings fcitx fcitx5 fcitx-configtool fcitx-config-gtk3 fcitx5-configtool || true
for entry in /usr/share/applications/jdime-settings.desktop \
    /usr/share/fcitx/addon/fcitx-jdime.conf /usr/share/fcitx5/addon/jdime.conf \
    /usr/share/fcitx5/inputmethod/jdime.conf; do
  if [ -f "$entry" ]; then
    printf '%s\n' "$entry"
    grep -E '^(Name|Name\[zh_CN\]|Exec|Type|Library|Enabled|IMRegisterMethod|LangCode|Addon)=' "$entry" || true
  fi
done
section loaders
# Only inspect MaQuan's known installed executables/libraries, not user-selected files.
for binary in /usr/bin/jdime-settings /usr/lib/jdime/libjdime_mobile_core.so \
    /usr/lib/x86_64-linux-gnu/libjdime_mobile_core.so /usr/lib64/libjdime_mobile_core.so \
    /usr/lib/fcitx/fcitx-jdime.so /usr/lib/x86_64-linux-gnu/fcitx/fcitx-jdime.so \
    /usr/lib/x86_64-linux-gnu/fcitx5/libjdime.so /usr/lib64/fcitx5/libjdime.so; do
  if [ -f "$binary" ]; then
    printf '\n%s\n' "$binary"
    command -v file >/dev/null && file -L "$binary"
    command -v ldd >/dev/null && ldd "$binary" 2>&1
  fi
done
section crash-log-presence
# Show existence only: crash logs can contain application names and private text.
for log in "${XDG_CONFIG_HOME:-$HOME/.config}/fcitx5/crash.log" \
           "${XDG_CONFIG_HOME:-$HOME/.config}/fcitx/log/crash.log"; do
  if [ -f "$log" ]; then stat -c '%y %s bytes %n' "$log"; fi
done
printf '\n检查结束。不会上传结果；分享前请检查输出中的路径。未读取个人词库、输入记录或账号配置。\n'
