#!/bin/bash

case $1 in
fcitx)
  IM=fcitx
  ;;
fcitx5)
  IM=fcitx
  ;;
ibus)
  IM=ibus
  ;;
scim)
  IM=scim
  ;;
*)
  echo -e "\
Invalid choice!
Usage: imchooser [ fcitx / fcitx5 / ibus / scim ]"
  exit 1
  ;;
esac

echo -e "Selecting \033[1;34m$IM\033[0m as the default input method engine..."

if [[ "$XDG_SESSION_DESKTOP" = "gnome" ]]; then
    echo "Please go to https://extensions.gnome.org and install KIMPanel"
    gsettings set org.gnome.settings-daemon.plugins.keyboard active false
fi

rm -f ~/.imchooser || true

echo export GTK_IM_MODULE="$IM" >> ~/.xprofile
echo export GTK_IM_MODULE="$IM" >> ~/.xinitrc
echo export QT_IM_MODULE="$IM" >> ~/.xprofile
echo export QT_IM_MODULE="$IM" >> ~/.xinitrc
echo export QT5_IM_MODULE="$IM" >> ~/.xprofile
echo export QT5_IM_MODULE="$IM" >> ~/.xinitrc
echo export XMODIFIERS=@im="$IM" >> ~/.xprofile
echo export XMODIFIERS=@im="$IM" >> ~/.xinitrc
