# socket.so is not an IME
IMENGINES=`ls /usr/lib*/scim-1.0/1.4.0/IMEngine | grep -v socket.so`

# check for m17n maps
if echo $IMENGINES | grep -q m17n.so; then
  if [ -z "`ls /usr/share/m17n/*.mim | grep -v global.mim`" ]; then
     IMENGINES=`echo $IMENGINES | sed -e "s/m17n.so//"`
  fi
fi

# check for scim-tables
if echo $IMENGINES | grep -q table.so; then
  if [ -z "`ls /usr/share/scim/tables`" ]; then
     IMENGINES=`echo $IMENGINES | sed -e "s/table.so//"`
  fi
fi

IMENGINE_NO=`echo $IMENGINES | wc -w`

# start SCIM if have active IME(s) 
if [ "$IMENGINE_NO" -ge 1 ]; then

  XIM=SCIM
  XIM_PROGRAM=/usr/bin/scim
  XIM_ARGS=""
  PREFERENCE_PROGRAM=/usr/bin/scim-setup
  SHORT_DESC="SCIM"

  gtkimm_exists () {
	# for multilib check both immodules installed
	for dir in /usr/lib*/gtk-2.0/immodules; do
		[ ! -x ${dir}/im-$1.so ] && return 1
	done
	return 0
	}

  if gtkimm_exists scim-bridge; then
    GTK_IM_MODULE=scim-bridge
  elif gtkimm_exists scim; then
    GTK_IM_MODULE=scim
  else
    GTK_IM_MODULE=xim
  fi

  qtimm_exists () {
	# for multilib check both immodules installed
	for dir in /usr/lib*/qt4/plugins/inputmethods; do
		[ ! -x ${dir}/$1.so ] && return 1
	done
	return 0
	}

  if qtimm_exists im-scim-bridge; then
    QT_IM_MODULE=scim-bridge
  elif qtimm_exists libqscim; then
    QT_IM_MODULE=scim
  else
    QT_IM_MODULE=xim
  fi

else
  echo "No SCIM IMEs found: SCIM startup was skipped."
  . /etc/X11/xinit/xinput.d/none.conf
fi
