#!/bin/bash

# Ubuntu One等のマウント可能なオンラインストレージを使用してAnthyのユーザー辞書を複数環境ないし複数人での共有・同期操作の自動化スクリプト

# 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 2 of the License, or (at your option)
# any later version.

# Author: kobayashi mao <kobamao.jp@gmail.com>

timestamp=`date +%y%m%d%H%M%S`
usr_name=`id -n -u`
local_dir="$HOME"

## 指定がない場合には標準でUbuntu One直下で共有作業を行う
## 複数人で./Ubuntu\ One/Shared\ With\ Me下で共有する場合は下をコメントアウトして、そのさらに下のコメントアウトを外す
{
if [ -f $local_dir/.dic_sync/online_storage.conf ]
	then
	read online_dir < $local_dir/.dic_sync/online_storage.conf
else
	online_dir="$HOME/Ubuntu One"
	## online_dir="$HOME/Ubuntu One/Shared With Me"
fi
}

## 複数ユーザーで同一ユーザー名を含むユーザー同士で辞書の共有を行っている場合、下の変数nicknameを任意の連続する英数字に変更する必要がある
## 指定がない場合には標準ではユーザー名が引かれる
{
if [ -f $local_dir/.dic_sync/nickname.conf ]
	then
	read nickname < $local_dir/.dic_sync/nickname.conf
else
	nickname="$usr_name"
fi
}

{
if [ -f "$online_dir"/.dic_sync/dic_log$nickname ]
	then
	{
	if [ ! -d $local_dir/.dic_sync ]
		then
		mkdir $local_dir/.dic_sync
		cp -f "$online_dir"/.dic_sync/dic_log$nickname $local_dir/.dic_sync/
	elif [ -d $local_dir/.dic_sync ] && [ ! -f $local_dir/.dic_sync/dic_log$nickname ]
		then
		cp -f "$online_dir"/.dic_sync/dic_log$nickname $local_dir/.dic_sync/
	fi
	}
else
	{
	if [ ! -d $local_dir/.dic_sync ]
		then 
		mkdir -p $local_dir/.dic_sync
		touch $local_dir/.dic_sync/dic_log$nickname
			echo "000000000000	$nickname	This is not log date." >> $local_dir/.dic_sync/dic_log$nickname
			echo "000000000000	$nickname	This is not log date." >> $local_dir/.dic_sync/dic_log$nickname
			echo "000000000000	$nickname	Make new dic_log file and new directory /.dic_sync ." >> $local_dir/.dic_sync/dic_log$nickname 
		{
		if [ ! -d "$online_dir"/.dir_sync ]
			then
			mkdir "$online_dir"/.dic_sync
		fi
		}
		cp -f $local_dir/.dic_sync/dic_log$nickname "$online_dir"/.dic_sync/
	elif [ -d $local_dir/.dic_sync ] && [ ! -f $local_dir/.dic_sync/dic_log$nickname ]
		then
		touch $local_dir/.dic_sync/dic_log$nickname
			echo "000000000000	$nickname	This is not log date." >> $local_dir/.dic_sync/dic_log$nickname
			echo "000000000000	$nickname	This is not log date." >> $local_dir/.dic_sync/dic_log$nickname
			echo "000000000000	$nickname	Make new dic_log file." >> $local_dir/.dic_sync/dic_log$nickname
		{
		if [ ! -d "$online_dir"/.dir_sync ]
			then
			mkdir "$online_dir"/.dic_sync
		fi
		}
		cp -f $local_dir/.dic_sync/dic_log$nickname "$online_dir"/.dic_sync/
	fi
	}
fi
}

user_anthy_dic_change=`grep -s -v -F -f "$online_dir"/.anthy/private_words_default $local_dir/.anthy/private_words_default | wc -l`
one_anthy_dic_change=`grep -s -v -F -f $local_dir/.anthy/private_words_default "$online_dir"/.anthy/private_words_default | wc -l`

lastest_backup=`grep -s -F $nickname $local_dir/.dic_sync/dic_log$nickname | tail -n 1 | cut -f1`
nouse_backup=`grep -s -F $nickname $local_dir/.dic_sync/dic_log$nickname | tail -n 2 | head -n 1 | cut -f1`

backup_log=`grep -s -v -F -f $local_dir/.anthy/private_words_default $local_dir/.dic_sync/private_words_default$lastest_backup | wc -l`

number_of_all_user=`cut -f2 $local_dir/.dic_sync/dic_log | sort | uniq | wc -l`
number_of_user=`expr $number_of_all_user - 1`
another_users="`grep -s -v -F "$nickname" $local_dir/.dic_sync/dic_log | cut -f2 |  sort | uniq | tr "[:space:]" " "`"
counter=0

rm_change_backup_flag=`find $local_dir/.dic_sync/ -name "change_backup*" | wc -l`

function rm_oldest_change_backup
{
	if [ $rm_change_backup_flag -gt 0 ]
		then
		oldest_change_backup=`find $local_dir/.dic_sync/ -name "change_backup*" | sort | head -n 1 | tr -d "$local_dir/.dic_sync/change_backup"`
	{
		for another_name in $another_users
		do
			lastest_another_sync=`grep -s -F "$another_name" $local_dir/.dic_sync/dic_log | tail -n 1 | cut -f1`
			{
				if [ $lastest_another_sync -ge $oldest_change_backup ]
					then
					counter=`expr $counter + 1`
					checker=1
				else
					checker=0
				fi
			}
			case $checker in
				1) continue
				;;
				0) break
				;;
			esac
		done
		if [ $counter -eq $number_of_user ]
			then
			rm -f $local_dir/.dic_sync/change_backup$oldest_change_backup "$online_dir"/.dic_sync/change_backup$oldest_change_backup
		fi
	}
	fi
}

function make_change_backup
{
	if [ -f $local_dir/.dic_sync/private_words_default$lastest_backup ] && [ $backup_log -gt 0 ]
		then
		grep -s -v -F -f $local_dir/.anthy/private_words_default $local_dir/.dic_sync/private_words_default$lastest_backup > $local_dir/.dic_sync/change_backup$timestamp
	fi
}



function cat_change_backup
{
	change_backup_cheker=`find "$online_dir"/.dic_sync/ -name "change_backup*"  | wc -l`
	if [ -f $local_dir/.dic_sync/change_backup$timestamp ]
		then
		cp -f $local_dir/.dic_sync/change_backup$timestamp "$online_dir"/.dic_sync/
		cat "$online_dir"/.dic_sync/change_backup* "$online_dir"/.dic_sync/change_backup* > $local_dir/.dic_sync/cat_change_backup
	elif [ $change_backup_cheker -gt 0 ]
		then
		cat "$online_dir"/.dic_sync/change_backup* "$online_dir"/.dic_sync/change_backup* > $local_dir/.dic_sync/cat_change_backup
	fi
}

search_dic_log=`find "$online_dir"/.dic_sync/ -name "dic_log*"  | wc -l`

function download_dic_log
{
	if [ $search_dic_log -gt 0 ]
		then
		cat "$online_dir"/.dic_sync/dic_log* | sort > $local_dir/.dic_sync/dic_log
	fi
}

function make_dic_log 
{
	tail -n 2 $local_dir/.dic_sync/dic_log$nickname > $local_dir/.dic_sync/dic_log$nickname$timestamp
	cat $local_dir/.dic_sync/dic_log$nickname$timestamp > $local_dir/.dic_sync/dic_log$nickname
	rm $local_dir/.dic_sync/dic_log$nickname$timestamp
	echo "$timestamp	$nickname	Synchronization user dictionary." >> $local_dir/.dic_sync/dic_log$nickname 
}

function make_one_dic_change 
{
	grep -s -v -F -f $local_dir/.anthy/private_words_default "$online_dir"/.anthy/private_words_default > $local_dir/.dic_sync/one_dic_change$timestamp
}

function dic_sync1
{
	if [ $user_anthy_dic_change -gt 0 ] && [ $one_anthy_dic_change -gt 0 ]
		then 
		make_dic_log
		make_one_dic_change				
		cat $local_dir/.anthy/private_words_default $local_dir/.dic_sync/one_dic_change$timestamp | sort > $local_dir/.dic_sync/private_words_default
		cp -f $local_dir/.dic_sync/private_words_default $local_dir/.anthy
		cp -f $local_dir/.dic_sync/private_words_default "$online_dir"/.anthy
		mv $local_dir/.dic_sync/private_words_default $local_dir/.dic_sync/private_words_default$timestamp
		rm -f $local_dir/.dic_sync/one_dic_change* $local_dir/.dic_sync/one_user_dic_change*
	elif [ $user_anthy_dic_change -gt 0 ] 
		then 
		make_dic_log
		cp -f $local_dir/.anthy/private_words_default $local_dir/.dic_sync/
		cp -f $local_dir/.dic_sync/private_words_default "$online_dir"/.anthy
		mv $local_dir/.dic_sync/private_words_default $local_dir/.dic_sync/private_words_default$timestamp
		rm -f $local_dir/.dic_sync/one_dic_change*
	elif [ $one_anthy_dic_change -gt 0 ]
		then 
		make_dic_log
		make_one_dic_change
		cat $local_dir/.anthy/private_words_default $local_dir/.dic_sync/one_dic_change$timestamp | sort > $local_dir/.dic_sync/private_words_default
		cp -f $local_dir/.dic_sync/private_words_default $local_dir/.anthy
		cp -f $local_dir/.dic_sync/private_words_default "$online_dir"/.anthy
		mv $local_dir/.dic_sync/private_words_default $local_dir/.dic_sync/private_words_default$timestamp
		rm -f $local_dir/.dic_sync/one_dic_change*
	fi
}

function log_from_user 
{
	if [ -d "$online_dir"/.dic_sync/ ]
		then
		cp -f $local_dir/.dic_sync/dic_log$nickname "$online_dir"/.dic_sync/dic_log$nickname
	else
		mkdir -p "$online_dir"/.dic_sync/
		cp -f $local_dir/.dic_sync/dic_log$nickname "$online_dir"/.dic_sync/dic_log$nickname
	fi
}

function dic_sync2
{
	if [ $user_anthy_dic_change -gt 0 ] && [ $one_anthy_dic_change -gt 0 ]
		then 
		make_dic_log
		make_one_dic_change				
		cat $local_dir/.anthy/private_words_default $local_dir/.dic_sync/one_dic_change$timestamp $local_dir/.dic_sync/cat_change_backup | sort | uniq -u > $local_dir/.dic_sync/private_words_default
		cp -f $local_dir/.dic_sync/private_words_default $local_dir/.anthy
		cp -f $local_dir/.dic_sync/private_words_default "$online_dir"/.anthy
		mv $local_dir/.dic_sync/private_words_default $local_dir/.dic_sync/private_words_default$timestamp
		rm -f $local_dir/.dic_sync/one_dic_change* $local_dir/.dic_sync/one_user_dic_change* $local_dir/.dic_sync/cat_change_backup
	elif [ $user_anthy_dic_change -gt 0 ] 
		then 
		make_dic_log
		cat $local_dir/.anthy/private_words_default $local_dir/.dic_sync/cat_change_backup | sort | uniq -u > $local_dir/.dic_sync/private_words_default
		cp -f $local_dir/.dic_sync/private_words_default $local_dir/.anthy
		cp -f $local_dir/.dic_sync/private_words_default "$online_dir"/.anthy
		mv $local_dir/.dic_sync/private_words_default $local_dir/.dic_sync/private_words_default$timestamp
		rm -f $local_dir/.dic_sync/one_dic_change*
	elif [ $one_anthy_dic_change -gt 0 ]
		then
		make_dic_log
		make_one_dic_change
		cat $local_dir/.anthy/private_words_default $local_dir/.dic_sync/one_dic_change$timestamp $local_dir/.dic_sync/cat_change_backup | sort | uniq -u > $local_dir/.dic_sync/private_words_default
		cp -f $local_dir/.dic_sync/private_words_default $local_dir/.anthy
		cp -f $local_dir/.dic_sync/private_words_default "$online_dir"/.anthy
		mv $local_dir/.dic_sync/private_words_default $local_dir/.dic_sync/private_words_default$timestamp
		rm -f $local_dir/.dic_sync/one_dic_change* $local_dir/.dic_sync/cat_change_backup
	fi
}

function dic_sync0
{
	if [ -f $local_dir/.dic_sync/cat_change_backup ]
		then
		dic_sync2
	else
		dic_sync1
	fi
}

function rm_nouse_backup
{
	if [ -f $local_dir/.dic_sync/private_words_default$nouse_backup ]
		then
		rm -f $local_dir/.dic_sync/private_words_default$nouse_backup
	fi
}

Ubuntu_One_checker0=`grep -s -F "server rescan" $local_dir/.cache/ubuntuone/log/syncdaemon.log | wc -l`
## オフラインとオンラインを頻繁に行き来する場合以下の変数とメインスクリプトの条件のコメントアウトを解除し、従来の条件文をコメントアウトする
## Ubuntu_One_checker1=`grep -s -F "date -d +"%Y-%m-%d %k:%M"" $local_dir/.cache/ubuntuone/log/syncdaemon.log | grep -s -F "state: IDLE" | wc -l`
## Ubuntu_One_checker2=`grep -s -F '1 minutes ago' "date +"%Y-%m-%d %k:%M"" $local_dir/.cache/ubuntuone/log/syncdaemon.log | grep -s -F "state: IDLE" | wc -l`
## Ubuntu_One_checker3=`grep -s -F "date -d  '2 minutes ago' +"%Y-%m-%d %k:%M"" $local_dir/.cache/ubuntuone/log/syncdaemon.log | grep -s -F "state: IDLE" | wc -l`

#
# main script
#

if [ $Ubuntu_One_checker0 -gt 0 ] && [ -d "$online_dir"/.anthy ] && [ -f $local_dir/.anthy/private_words_default ]
## if \( [ $Ubuntu_One_checker1 -gt 0 ] || [ $Ubuntu_One_checker2 -gt 0 ] || [ $Ubuntu_One_checker3 -gt 0 ] \) && [ $Ubuntu_One_checker0 -gt 0 ] && [ -d "$online_dir"/.anthy ] && [ -f $local_dir/.anthy/private_words_default ]
	then 
	download_dic_log
	rm_oldest_change_backup
	make_change_backup
	cat_change_backup
	dic_sync0
	rm_nouse_backup
	log_from_user
elif [ $Ubuntu_One_checker0 -gt 0 ] && [ -f "$online_dir"/.anthy/private_words_default ]
## elif \( [ $Ubuntu_One_checker1 -gt 0 ] || [ $Ubuntu_One_checker2 -gt 0 ] || [ $Ubuntu_One_checker3 -gt 0 ] \) && [ $Ubuntu_One_checker0 -gt 0 ] && [ -f "$online_dir"/.anthy/private_words_default ]
	then
	log_from_user
	cp -rf "$online_dir"/.anthy $local_dir/
elif [ $Ubuntu_One_checker0 -gt 0 ] && [ -d "$online_dir"/ ] && [ -f $local_dir/.anthy/private_words_default ]
## elif \( [ $Ubuntu_One_checker1 -gt 0 ] || [ $Ubuntu_One_checker2 -gt 0 ] || [ $Ubuntu_One_checker3 -gt 0 ] \) && [ $Ubuntu_One_checker0 -gt 0 ] && [ -d "$online_dir"/ ] && [ -f $local_dir/.anthy/private_words_default ]
	then
	# $local_dir/.anthy/private_words_default を$local_dir/Ubuntu One/.anthy を作成後にコピー
	mkdir -p "$online_dir"/.anthy
	cp -f $local_dir/.anthy/private_words_default "$online_dir"/.anthy
fi

exit 0

# [ End of File ]
