#!/bin/sh

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
	    cat $I18NFILE | grep -v "XIM=" > /tmp/imswitcher$$ 2> /dev/null
	    mv /tmp/imswitcher$$ $I18NFILE
	    echo $"Default input method set to installation default."
	else
	    echo $"You do not have write access to $I18NFILE"
	fi
    else
	if [ -f $I18NFILE ]; then
	    echo $"Default is already installation default: configuration not changed."
	else
	    echo $"Default is already installation default: no $I18NFILE."
	fi

    fi

}

list() {
    if grep -q "XIM=" $USERI18N 2> /dev/null; then
        echo -n $"Your default 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
        cat $I18NFILE | grep -v "XIM=" > /tmp/imswitcher$$;
        echo "XIM=$1" > /tmp/imswitcher$$
        mv /tmp/imswitcher$$ $I18NFILE
	echo $"set default input method to $1"
    else
        if [ ! -f $I18NFILE ]; then
            if touch $I18NFILE 2> /dev/null; then
                echo "XIM=$1" >> $I18NFILE
		echo $"Set default 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 v0.1"
echo $""
echo $"Usage: $PROGNAME [-w] -m [xim|iiimf]"
echo $"           to set a specific input method for your current language"
echo $"       $PROGNAME [-w] -a"
echo $"           to revert to installation default"
echo $"       $PROGNAME [-w] -l"
echo $"           to list current settings, input methods, and available languages"
echo $"       $PROGNAME [-w] -s InputMethodName"
echo $"           to set specific input method for your current language"
echo $""

echo $"Arguments:"
echo $"  -w                   system wide"
echo $"Commands:"
echo $"  -m                   use "xim" as switch it to xim input methods"
echo $"                       use "iiimf" to switch back to use IIIMF"
echo $"  -a                   Revent to installation default"
echo $"  -l                   List the current setting and input method available for current language"
echo $"  -s                   Set specific Input Method for current language"
echo $"  -h                   This help"
    exit 65
}

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

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