#!/bin/bash

#Mozc Setup Helper
#Copyright 2013-2014 AWASHIRO Ikuya <ikuya@fruitsbasket.info>
#Contributor: Jiro Matsuzawa, Akira Nakagawa
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.

#どういう人が使うの？
#1.Ubuntuオフィシャルイメージ:ibus-anthyからibus-mozcに→できたはず？
#2.Ubuntu日本語Remix:FcitxからIBusに→できたはず？
#3.Ubuntu GNOME/Kubuntu/Xubuntu/Lubuntuユーザー
#4.それぞれ13.10からのアップグレード

#テスト済Ubuntuフレーバー

#要テストUbuntuフレーバー
#Ubuntu 14.04 (新規)
#Ubuntu 14.04 (13.10からのアップグレード)
#Ubuntu GNOME 14.04 (新規)
#Ubuntu GNOME 14.04 (13.10からのアップグレード)
#Xubuntu 14.04 (新規)
#Xubuntu 14.04 (13.10からのアップグレード）

#確認するパッケージ
PKG_ZENITY=zenity
PKG_GSETTINGS=libglib2.0-bin
PKG_IBUS_MOZC=ibus-mozc
PKG_UBIQUITY=ubiquity

NOWUSE=`env |grep XMODIFIERS|cut -d = -f 3`

#パッケージチェック
if ! dpkg -l | grep -qs "^ii  *${PKG_ZENITY} "; then
	echo "zenityがインストールされていません" >&2
	echo "'${PKG_ZENITY}'パッケージをインストールしてください" >&2
	exit 1
fi

if ! dpkg -l | grep -qs "^ii  *${PKG_GSETTINGS} "; then
	zenity --error --title="Mozc セットアップヘルパー" --text="gsettingsがインストールされていません\n'${PKG_GSETTINGS}'パッケージをインストールしてください"
	exit 1
fi

if ! dpkg -l | grep -qs "^ii  *${PKG_IBUS_MOZC} "; then
	zenity --error --title="Mozc セットアップヘルパー" --text="ibus-mozcがインストールされていません\n'${PKG_IBUS_MOZC}'パッケージをインストールしてください"
	exit 1
fi

#日本語キーボードと英語キーボードを判別
LAYOUT=`setxkbmap -query|grep layout|awk '{print $2}'`
if [ "${LAYOUT}" = "jp" ] || [ "${LAYOUT}" = "jp,us" ]  ; then
	:
else
	zenity --error --title="Mozc セットアップヘルパー" --text="日本語キーボード以外は非対応です"
	exit 1
fi
#if [ "${LAYOUT}" = "us" ]; then
#	IBUS_LANG="'xkb:us::eng'"
#	GSD_LANG="('xkb', 'us')"
#else
#	IBUS_LANG="'xkb:jp::jpn'"
#	GSD_LANG="('xkb', 'jp')"
#fi

PRELOAD_ENGINE_MODE=`gsettings get org.freedesktop.ibus.general preload-engine-mode`
PRELOAD_ENGINES=`gsettings get org.freedesktop.ibus.general preload-engines|grep -o mozc-jp`
INDICATOR_KEYBOARD=`gsettings get com.canonical.indicator.keyboard visible`
UBUNTU_VERSION=`lsb_release -r|awk '{print $2}'|tr -d .`
IBUS_VERSION=`ibus-daemon -V|grep -o [0-9].[0-9].[0-9]|tr -d .`

if [ "${GDM_LANG}" = "ja" -o "${GDM_LANG}" = "ja_JP" ]; then
	:
else
	zenity --error --title="Mozc setup helper" --text="This program intends to run on Japanese environment. Sorry for inconvenience."
       exit 1
fi

if [ "${UBUNTU_VERSION}" -lt "1404" ]; then
	zenity --error --title="Mozc セットアップヘルパー" --text="Ubuntuのバージョンが14.04未満のようです。終了します"
	exit 1
fi

if [ "${IBUS_VERSION}" -lt "155" ]; then
	zenity --error --title="Mozc セットアップヘルパー" --text="IBusのバージョンが1.5.5未満のようです。終了します"
	exit 1
fi

if [ "${NOWUSE}" != "ibus" ]; then
	zenity --question --title="Mozc セットアップヘルパー" --text="FcitxからIBusに変更しますか？"
	REPAIR=`echo $?`
	SET_MOZC="0"
elif [ "${NOWUSE}" = "ibus" ] && [ -e ~/.config/mozc-setup-helper/run ]; then
	gsettings set org.gnome.desktop.wm.keybindings switch-input-source "['<Super>space']"
	gsettings set org.gnome.desktop.wm.keybindings switch-input-source-backward "['<Super><Shift>space']"
	gsettings set org.freedesktop.ibus.general.hotkey triggers "['<Control>space']" 
	rm -f ~/.local/share/applications/mozc_character_palette.desktop
	rm -f ~/.local/share/applications/mozc_config_dialog.desktop
	rm -f ~/.local/share/applications/mozc_dictionary_tool.desktop
	rm -f ~/.local/share/applications/mozc_hand_writing.desktop
	rm -f ~/.local/share/applications/mozc_word_register_dialog.desktop
	rm -f ~/.local/share/applications/ibus-setup-mozc-jp.desktop
	rm -f ~/.config/mozc-setup-helper/run
	zenity --info --title="Mozc セットアップヘルパー" --text="13.10で行った設定の一部を削除しました"
	SET_MOZC="0"
elif [ "${NOWUSE}" = "ibus" ]; then
	zenity --question --title="Mozc セットアップヘルパー" --text="ibus-mozcをデフォルトにする設定を行いますか？"
	SET_MOZC=`echo $?`
fi

if [ "${SET_MOZC}" != "0" ]; then
	zenity --info --title="Mozc セットアップヘルパー" --text="終了します"
	exit 0
fi

if [ "${REPAIR}" = 0 ]; then
	im-config -n ibus
	zenity --info --title="Mozc セットアップヘルパー" --text="一度ログアウトして再ログインし、Mozc セットアップヘルパーを再実行してください。終了します"
	exit 0
elif [ "${REPAIR}" = 1 ]; then
	zenity --info --title="Mozc セットアップヘルパー" --text="終了します"
	exit 0
fi


#if [ "${PRELOAD_ENGINE_MODE}" != "1" ] && [ "${NOWUSE}" = "ibus" ]; then
#	gsettings set org.freedesktop.ibus.general preload-engine-mode 1
#fi

if [ "${PRELOAD_ENGINES}" != "mozc-jp" ]; then
	zenity --error --title="Mozc セットアップヘルパー" --text="何らかの理由でibus-mozcがIBusに認識されていません。\n一回ログアウトして再ログインするか、可能であればIBusを再起動してみてください。\nそれでもこのメッセージが表示される場合は非対応の環境ですので、手動で設定してください。"
	exit 1
fi

#if ! [ -d ~/.config/mozc-setup-helper ]; then
#	mkdir ~/.config/mozc-setup-helper
#	touch ~/.config/mozc-setup-helper/1404
#fi

#indicator-keyboardが表示されていない場合は、FcitxからIBusに変更したとみなす
if [ "${INDICATOR_KEYBOARD}" = "true" ]; then
	LAST_MESSAGE="\n新しいIBusの挙動が気に入らない場合は、fcitxも試してみてください。fcitx-setup-helperを実行すると切り替えの設定ができます"
fi

if [ "$XDG_CURRENT_DESKTOP" = "Unity" ]; then
	gsettings set org.gnome.desktop.input-sources sources "[('ibus', 'mozc-jp')]"
	gsettings set com.canonical.indicator.keyboard visible true
	#gsettings set org.gnome.settings-daemon.plugins.keyboard active true
	gsettings set org.freedesktop.ibus.general preload-engine-mode 0
	zenity --info --title="Mozc セットアップヘルパー" --text="設定が完了しました。 ${LAST_MESSAGE}"
	exit
elif [ "$XDG_CURRENT_DESKTOP" = "GNOME" ]; then
	gsettings set org.gnome.desktop.input-sources sources "[('ibus', 'mozc-jp')]"
	#gsettings set org.gnome.settings-daemon.plugins.keyboard active true
	gsettings set org.freedesktop.ibus.general preload-engine-mode 0
	zenity --info --title="Mozc セットアップヘルパー" --text="設定が完了しました。 ${LAST_MESSAGE}"
	exit
else
	gsettings set org.freedesktop.ibus.general engines-order "['mozc-jp', 'xkb:jp::jpn']"
	gsettings set org.freedesktop.ibus.general preload-engines "['xkb:jp::jpn', 'mozc-jp']"
	gsettings set org.freedesktop.ibus.general preload-engine-mode 0
	gsettings set org.gnome.desktop.input-sources current 0
	zenity --info --title="Mozc セットアップヘルパー" --text="設定が完了しました。一旦IBusを再起動してください。 ${LAST_MESSAGE}"
	exit
fi
