#!/bin/sh
# Copyright (C) 2005 Kenshi Muto <kmuto@debian.org>
#  Modified for Debian package.
# Copyright (C) 1999 - 2004 Red Hat, Inc. All rights reserved. This
# copyrighted material is made available to anyone wishing to use, modify,
# copy, or redistribute it subject to the terms and conditions of the
# GNU General Public License version 2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

XINPUT_PATH=/etc/X11/xinit/xinput.d
DOTXINPUT_PATH=${HOME}/.xinput.d
ALTER_XINPUT=/etc/alternatives/xinput-
LNG=${LC_ALL:-${LC_CTYPE:-${LANG}}}; LNG=${LNG%@*}; LNG=${LNG%.*};
DEFAULT=$(/usr/sbin/update-alternatives --display xinput-$LNG | sed -ne 's/ link currently points to \(.*\)/\1/p')
DEPENDENCY=/usr/share/im-switch/dependscheck.pl
HOOKDIR=/usr/share/im-switch
USERID=$(id -u)
VER=0.3+debian1

auto() {
    if [ ! -r "${ALTER_XINPUT}${LNG}" ]; then
	echo "No alternatives is defined for language $LNG"
	exit 1
    else
	if [ "$USERID" -eq 0 ]; then
	    /usr/sbin/update-alternatives --auto xinput-$LNG
	    PREVIOUS=$DEFAULT
	    DEFAULT=$(/usr/sbin/update-alternatives --display xinput-$LNG | sed -ne 's/ link currently points to \(.*\)/\1/p')
	    if [ "$DEFAULT" != "$PREVIOUS" ]; then
               [ "$PREVIOUS" -a -x "$HOOKDIR/$PREVIOUS" ] &&  $HOOKDIR/$PREVIOUS remove
               [ "$DEFAULT" -a -x "$HOOKDIR/$DEFAULT" ] &&  $HOOKDIR/$DEFAULT install
            fi
	else
	   if [ -r "${XINPUT_PATH}/${LNG}" -a -r "${DOTXINPUT_PATH}/${LNG}" ]; then
	     PREVIOUS=$(readlink ${DOTXINPUT_PATH}/${LNG} | sed -e "s%$XINPUT_PATH/%%")
             [ "$PREVIOUS" -a -x "$HOOKDIR/$PREVIOUS" ] &&  $HOOKDIR/$PREVIOUS remove
	     mv ${DOTXINPUT_PATH}/${LNG} ${DOTXINPUT_PATH}/${LNG}.backup
	   fi
	fi
    fi
}

list() {
    if [ -r "${DOTXINPUT_PATH}/${LNG}" ]; then
	echo "You have ${LNG} setup in \"${DOTXINPUT_PATH}\"."
	echo "======================================================="
    fi
    if [ ! -r "${ALTER_XINPUT}${LNG}" ]; then
	echo "No alternatives defined for language $LNG"
    else
	/usr/sbin/update-alternatives --display xinput-$LNG | sed -e "s%$XINPUT_PATH/%%"
    fi
    echo "======================================================="
    echo "The following languages have been configured to use input methods:"
    for i in `/bin/ls $ALTER_XINPUT* | sed -e "s%$ALTER_XINPUT%%"`; do
        echo -n "$i "
    done
    echo
}

dependency() {
    DEPENDS=$(grep "DEPENDS=" $XINPUT_PATH/$1 | sed 's/DEPENDS=//')
    [ "$DEPENDS" ] && RESULT=$($DEPENDENCY $DEPENDS)
    if [ "$RESULT" ]; then
      echo "From the package dependency information, following packages"
      echo "may be useful to install:"
      echo "  $RESULT"
    fi
}

setalt() {
    if [ "$USERID" -eq 0 ]; then
	if [ ! -r "${ALTER_XINPUT}${LNG}" ]; then
	    echo "No alternatives defined for language $LNG"
	else
	    /usr/sbin/update-alternatives --set xinput-$LNG $XINPUT_PATH/$1
	    PREVIOUS=$DEFAULT
	    DEFAULT=$(/usr/sbin/update-alternatives --display xinput-$LNG | sed -ne 's/ link currently points to \(.*\)/\1/p')
	    if [ "$DEFAULT" != "$PREVIOUS" ]; then
               [ "$PREVIOUS" -a -x "$HOOKDIR/$PREVIOUS" ] &&  $HOOKDIR/$PREVIOUS remove
               [ "$DEFAULT" -a -x "$HOOKDIR/$DEFAULT" ] &&  $HOOKDIR/$DEFAULT install
            fi
            dependency $1
	fi
    else
	[ -r "$XINPUT_PATH/$1" ] || { echo "No xinput.d config for $1 available." ; exit 1 ; }
	if [ -d ${DOTXINPUT_PATH} ]; then
	    if [ -r "${DOTXINPUT_PATH}/${LNG}" ]; then
	      PREVIOUS=$(readlink ${DOTXINPUT_PATH}/${LNG} | sed -e "s%$XINPUT_PATH/%%")
              mv ${DOTXINPUT_PATH}/${LNG} ${DOTXINPUT_PATH}/${LNG}.backup
              [ "$PREVIOUS" ] && [ "$1" != "$PREVIOUS" ] && [ -x "$HOOKDIR/$PREVIOUS" ] &&  $HOOKDIR/$PREVIOUS remove
            fi
	else
	    mkdir -p ${DOTXINPUT_PATH}
	fi
	ln -s $XINPUT_PATH/$1 ${DOTXINPUT_PATH}/$LNG
        [ "$1" -a -x "$HOOKDIR/$1" ] &&  $HOOKDIR/$1 install
        dependency $1
    fi
}

help() {
echo "Input Method Switcher $VER"
echo ""
echo "Usage: $0 [-z lang] -s inputmethodname"
echo "           to set a specific input method for the language"
echo "       $0 [-z lang] -a"
echo "           to revert to the default input method for the language"
echo "       $0 [-z lang] -l"
echo "           to list current settings and available input methods for"
echo "           the language, and also all the available languages"
echo "       $0 -h"
echo "           to show this help"
echo
echo "The \"LANG\" argument of -z takes the form \"ll_CC\" and is used to override"
echo "the current language with ll_CC instead: eg \"-z ja_JP\""
    exit 65
}

setcommand() {
if [ -z "$COMMAND" ]; then
    COMMAND=$1
else
    echo "Use only of -s, -a or -l."
    echo "Try \"$0 -h\" for usage."
    exit 1
fi
}

if [ $# -eq 0 ]; then
    help
    exit 1
fi

bad_option=0

while getopts "hz:als:" OPTION; do
    case $OPTION in
        z) LNG=$OPTARG;;
        a) setcommand auto;;
        l) setcommand list;;
        s) setcommand "setalt $OPTARG";;
        h) setcommand "help $0";;
	?) bad_option=1
    esac
done

if [ "$bad_option" -eq 1 ]; then
    echo "Try \"$0 -h\" for usage."
    exit 1
else
    $COMMAND
fi
