#!/bin/bash
# omarchy:summary=Install Fcitx5 Rime and Chinese addons with a default profile
# omarchy:examples=omarchycn ime apply

set -euo pipefail

omarchy-pkg-add fcitx5-rime fcitx5-chinese-addons fcitx5-configtool

profile_dir="$HOME/.config/fcitx5"
mkdir -p "$profile_dir"

if [[ -f $profile_dir/profile ]] && ! grep -q '^Name=rime$' "$profile_dir/profile"; then
  backup_dir="$HOME/.local/state/omarchycn/backups/ime/$(date +%Y%m%d-%H%M%S)"
  mkdir -p "$backup_dir"
  cp "$profile_dir/profile" "$backup_dir/profile"
  echo "Existing profile backed up to $backup_dir"
fi

cp "$OMARCHY_PATH/cn/fcitx5/profile" "$profile_dir/profile"

if systemctl --user is-active omarchy-fcitx5.service > /dev/null 2>&1; then
  systemctl --user restart omarchy-fcitx5.service
  echo "Fcitx5 restarted with Rime profile"
else
  echo "Fcitx5 profile installed (takes effect at next graphical session)"
fi

omarchy-cn-ime-status
