#!/bin/bash

#Mozc Setup Helper
#Copyright 2013 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/>.

#テスト済Ubuntuフレーバー
#Ubuntu 13.10 (GNOME Flashback Session含む)
#Ubuntu GNOME 13.10  (GNOME Classic Session含む)
#Xubuntu 13.10

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

#パッケージチェック
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

if dpkg -l | grep -qs "^ii  *${PKG_UBIQUITY} "; then
	SESSION=live
fi

#日本語キーボードと英語キーボードを判別
LAYOUT=`setxkbmap -query|grep layout|awk '{print $2}'`
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`
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 [ "${1}" = -f ]; then
	:
elif [ "${1}" = -a ] && [ -r ~/.config/mozc-setup-helper/run ]; then
	#/etc/xdg/autostartに置くdesktopファイルは、-aオプションをつける。これで二回目以降に起動しても何もせずに終了するようになる。
	exit 0
elif [ "${1}" = -ac ] && [ -r ~/.config/mozc-setup-helper/run ]; then
	#私用の隠し？ コマンド。ログイン時に勝手にmozcが起動するのは嫌な感じ。ちゃんと動くかは知らない
	gsettings set org.gnome.desktop.input-sources current 0
	exit 0
elif [ "${1}" = -a ] && [ "${SESSION}" = "live" ]; then
	gsettings set org.gnome.desktop.input-sources current 0
	gsettings set org.gnome.desktop.input-sources sources "[${GSD_LANG}, ('ibus', 'mozc-jp')]"
	gsettings set org.freedesktop.ibus.general preload-engines "[${IBUS_LANG}, 'mozc-jp']"
	gsettings set org.freedesktop.ibus.general preload-engine-mode 0
	exit 0
elif [ -r ~/.config/mozc-setup-helper/run ]; then
	zenity --error --title="Mozc セットアップヘルパー" --text="過去に実行された形跡があります。再実行したい場合は、$0 -fで起動してください"
	exit 1
fi

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 "1310" ]; then
	zenity --error --title="Mozc セットアップヘルパー" --text="Ubuntuのバージョンが13.10未満のようです。終了します"
	exit 1
fi

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

zenity --question --title="Mozc セットアップヘルパー" --text="ibus-mozcをデフォルトにする設定を行いますか？"
if [ $? != "0" ]; then
	zenity --info --title="Mozc セットアップヘルパー" --text="終了します"
	exit
fi

if [ "${PRELOAD_ENGINE_MODE}" != "1" ]; then
       gsettings set org.freedesktop.ibus.general preload-engine-mode 1
       zenity --error --title="Mozc セットアップヘルパー" --text="IBusのエンジンが手動で変更されていました。自動で変更するようにしたので、一度ログアウトして再ログインし、再実行してください"
       exit 1
fi

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

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

LAST_MESSAGE="\n新しいIBusの挙動が気に入らない場合は、fcitxも試してみてください。fcitx-setup-helperで切り替えの設定ができます"

if [ "$DESKTOP_SESSION" = "ubuntu" ] || [ "$DESKTOP_SESSION" = "gnome-fallback" ]; then
	#[('xkb', 'jp'), ('ibus', 'anthy'), ('ibus', 'mozc-jp')]
	gsettings set org.gnome.desktop.input-sources current 0
	gsettings set org.gnome.desktop.input-sources sources "[${GSD_LANG}, ('ibus', 'mozc-jp')]"
	gsettings set org.freedesktop.ibus.general preload-engines "[${IBUS_LANG}, 'mozc-jp']"
	gsettings set org.freedesktop.ibus.general preload-engine-mode 0
	zenity --info --title="Mozc セットアップヘルパー" --text="設定が完了しました。"
elif [ "$DESKTOP_SESSION" = "gnome" ] || [ "$DESKTOP_SESSION" = "gnome-classic" ]; then
	gsettings set org.gnome.desktop.input-sources current 0
	gsettings set org.gnome.desktop.input-sources sources "[${GSD_LANG}, ('ibus', 'mozc-jp')]"
	zenity --info --title="Mozc セットアップヘルパー" --text="設定が完了しました。 ${LAST_MESSAGE}"
	exit
else
	gsettings set org.freedesktop.ibus.general engines-order "[${IBUS_LANG}, 'mozc-jp']"
	gsettings set org.freedesktop.ibus.general preload-engines "[${IBUS_LANG}, 'mozc-jp']"
	gsettings set org.freedesktop.ibus.general preload-engine-mode 0
	zenity --info --title="Mozc セットアップヘルパー" --text="設定が完了しました。 ${LAST_MESSAGE}"
	exit
fi

zenity --question --title="Mozc セットアップヘルパー" --text="入力ソースの切り替えをSuper+spaceからCtrl+spaceに変更しますか？"
if [ $? = "0" ]; then
	gsettings set org.gnome.desktop.wm.keybindings switch-input-source "['<Primary>space']"
	gsettings set org.gnome.desktop.wm.keybindings switch-input-source-backward "['<Primary><Shift>space']"
fi

zenity --question --title="Mozc セットアップヘルパー" --text="Mozcツールをメニューに登録しますか？"
if [ $? != "0" ]; then
	zenity --info --title="Mozc セットアップヘルパー" --text="終了します。${LAST_MESSAGE}"
	exit
fi

if ! [ -d ~/.local/share/applications/ ]; then
	mkdir -p ~/.local/share/applications/
fi

cat << EOD > ~/.local/share/applications/mozc_character_palette.desktop 
[Desktop Entry]
Comment=mozc moji palette
Terminal=false
Name=Mozc 文字パレット
Exec=/usr/lib/mozc/mozc_tool --mode=character_palette
Type=Application
Icon=/usr/share/ibus-mozc/tool.png
EOD

cat << EOD >  ~/.local/share/applications/mozc_config_dialog.desktop 
[Desktop Entry]
Comment=mozc property
Terminal=false
Name=Mozc プロパティ
Exec=/usr/lib/mozc/mozc_tool --mode=config_dialog
Type=Application
Icon=/usr/share/ibus-mozc/properties.png
EOD

cat << EOD > ~/.local/share/applications/mozc_dictionary_tool.desktop 
[Desktop Entry]
Comment=mozc dictionary tool
Terminal=false
Name=Mozc 辞書ツール
Exec=/usr/lib/mozc/mozc_tool --mode=dictionary_tool
Type=Application
Icon=/usr/share/ibus-mozc/dictionary.png
EOD

cat << EOD > ~/.local/share/applications/mozc_hand_writing.desktop 
[Desktop Entry]
Comment=mozc hand writing
Terminal=false
Name=Mozc 手書き文字認識
Exec=/usr/lib/mozc/mozc_tool --mode=hand_writing
Type=Application
Icon=/usr/share/ibus-mozc/tool.png
Keywords=tegaki
EOD

cat << EOD > ~/.local/share/applications/mozc_word_register_dialog.desktop 
[Desktop Entry]
Comment=mozc word register dialog
Terminal=false
Name=Mozc 単語登録
Exec=/usr/lib/mozc/mozc_tool --mode=word_register_dialog
Type=Application
Icon=/usr/share/ibus-mozc/dictionary.png
EOD

cat << EOD > ~/.local/share/applications/ibus-setup-mozc-jp.desktop
[Desktop Entry]
Name=IBus Mozc Setup
Comment=Set up IBus Mozc engine
Exec=/usr/lib/mozc/mozc_tool --mode=config_dialog
Icon=production
NoDisplay=true
Type=Application
StartupNotify=true
EOD

zenity --info --title="Mozc セットアップヘルパー" --text="完了しました。一度ログアウトして再ログインしたら、メニューから起動できるようになります。${LAST_MESSAGE}"
