#! /bin/sh -e

TMP=`tempfile`
DEFAULT_IM=""

# create desktop resource file, if it does not exist.
if [ ! -f $HOME/.uim ]; then
  echo "(define default-im-name '$DEFAULT_IM)"> $HOME/.uim
fi

# Sanity checks
if [ ! -w $HOME/.uim ]; then
  echo "I can not write on $HOME/.uim"
  exit 1
fi

uim-xim --list >$TMP

IMSERVER=$(cat $TMP| grep -e "^  " | awk '{ print $1 ; }')

# Create menu screen
MENU_COMMAND='whiptail --title "Select conversion engine for UIM" --menu 
"Select your choice of conversion engine for UIM.  If you do not find 
appropriate one, please ask administrator (root) to install 
one for you." 
23 78 12 '
for x in $IMSERVER ; do
  y=$(grep -e "^ *$x " $TMP|awk '{ print $2 ; }'|sed -e 's/(//' -e 's/)//' -e 's/:/ /g')
  #' look nice in mc
  MENU_COMMAND="$MENU_COMMAND $x \"in $y\""
done


# Display selection
eval $MENU_COMMAND 2> $TMP

# Alter $HOME/.uim
if grep -e "^ *(define *default-im-name '" $HOME/.uim 2>/dev/null >/dev/null ; then
  (
  echo "/^[ \t]*(define *default-im-name '/s/'\(.*\))/'$(cat $TMP))/"
  echo wq
  ) | ed -s $HOME/.uim
else
  echo >> $HOME/.uim
  echo "(define default-im-name '$(cat $TMP))">> $HOME/.uim
fi
