#!/bin/sh

VERSION=0.2
PROGNAME=`basename $0`
SYSI18N=/etc/sysconfig/i18n
USERI18N=$HOME/.i18n
I18NFILE=$USERI18N
LNG=`echo $LANG | awk -F. '{ print $1 }'`

auto() {
    if grep -q "XIM=" $I18NFILE 2> /dev/null; then
	if [ -w $I18NFILE ]; then
	    NEWFILE=`mktemp`
	    cat $I18NFILE | grep -v "XIM=" > $NEWFILE 2> /dev/null
	    cat $NEWFILE > $I18NFILE
	    rm $NEWFILE
	    echo $"Input method set to installation default."
	else
	    echo $"You do not have write access to $I18NFILE"
	fi
    else
	if [ -f $I18NFILE ]; then
	    echo $"Current Input Method is already installation default: configuration not changed."
	else
	    echo $"Current Input Method is already installation default: no $I18NFILE."
	fi

    fi

}

list() {
    if grep -q "XIM=" $USERI18N 2> /dev/null; then
        echo -n $"Your input method: "
        cat $USERI18N | awk -F= '{ if ($0 ~/XIM/) print $2 }'
    fi
    echo -n $"Default system input method: "
    if grep -q "XIM=" $SYSI18N 2> /dev/null; then
        cat $SYSI18N | awk -F= '{ if ($0 ~/XIM/) print $2 }'
    else
        echo $"install default"
    fi
}

setalt() {
    if [ -w $I18NFILE ]; then
	NEWFILE=`mktemp`
        cat $I18NFILE | grep -v "XIM=" > $NEWFILE
        echo "XIM=$1" >> $NEWFILE
        cat $NEWFILE > $I18NFILE
	rm $NEWFILE
	echo $"set input method to $1"
    else
        if [ ! -f $I18NFILE ]; then
            if touch $I18NFILE 2> /dev/null; then
                echo "XIM=$1" >> $I18NFILE
		echo $"Set input method to $1."
            else
                echo $"You do not have write access to $I18NFILE"
            fi
        else
            echo $"You do not have write access to $I18NFILE"
        fi
    fi
}

iiimf() {
    case $1 in
    xim)
        echo $"Current language is $LNG."
        case $LNG in
        ja_JP)
            setalt "kinput2";;
        ko_KR)
            setalt "nabi";;
        zh_TW)
            setalt "xcin";;
        zh_CN)
            setalt "Chinput";;
        *)
            echo $"No XIM input method available for this language: setting to auto."
            auto;;
        esac;;
    iiimf)
        setalt "htt";;
    *)
        echo $"Invalid option. Please use -h for help";;
    esac
    
}

help() {
echo $"Input Method Switcher v$VERSION"
echo $""
echo $"Usage: $PROGNAME [-w] -m [iiimf|xim]"
echo $"           set a specific input method: use \"iiimf\" to switch to IIIMF or \"xim\" to switch to the default XIM input method for your current language"
echo $"       $PROGNAME [-w] -a"
echo $"           revert to installation default"
echo $"       $PROGNAME -l"
echo $"           show current settings"
echo $"       $PROGNAME [-w] -s InputMethodName"
echo $"           set a specific input method"
echo $"       $PROGNAME -h"
echo $"           show this help"
echo $""

echo $"   Options:"
echo $"      -w               act on the system configuration instead of the user's"
    exit 65
}

case $* in
    -w) help;;
    -*) :;;
    *) help;;
esac

while getopts ":wm:als:h" OPTION; do
    case $OPTION in
        w) I18NFILE=$SYSI18N;;
        m) iiimf $OPTARG;;
        a) auto;;
        l) list;;
        s) setalt $OPTARG;;
        *) help;;
    esac
done
