#!/bin/bash
##	Guillaume DURR
#	gdurr@free.fr
#
#				Merge 2 dictionaries on Mac OS X
###############################		History		####################################
#		7 august 07			0.1 			first release, tested with tiger only
#		21 august 07		0.2 			Fix several bugs, make some arrangement
####################################################################################


clear;


###############################		Configure		###################################
#name of the dict in ~/Library/spelling/*
LANG=fr

#URL on webbsite
REMOTESPELLING=http://....../..../$LANG


# FTP info for upload
FTPHOST=
FTPUSER=
FTPPASSWORD=

# 
#LANG allows you to choose which language file to use.
#REMOTESPELLING is the path of the dictionary on the web site : http://somewhere/folder/$LANG
#The four variables are for uploding to the web.
#FTPHOST is the FTP site URL; FTPUSER is the admin user; FTPPASSWORD is your password; and FTPSPELLING is the path of the spelling file on FTP server (www/somewhere/spelling/$LANG).
FTPSPELLING=www/echange/spelling/$LANG

###############################		End Configure		###################################
SPELLINGFILE=~/Library/Spelling/$LANG
###########################################################################################
###############################		Function		###################################
function download
{
ftp -n $FTPHOST <<END_SCRIPT
quote USER $FTPUSER
quote PASS $FTPPASSWORD
get $FTPSPELLING /tmp/temp_remotebin
quit
END_SCRIPT
}

function upload
{
ftp -n $FTPHOST <<END_SCRIPT
quote USER $FTPUSER
quote PASS $FTPPASSWORD
put $SPELLINGFILE $FTPSPELLING
quit
END_SCRIPT
}

###########################################################################################
rm /tmp/result /tmp/temp_local /tmp/temp_remotebinascii /tmp/temp_remotebin


tr "\000" "\n" < $SPELLINGFILE > /tmp/temp_local

echo "#remote"

# get the remote file by FTP
download


# first time, uploading file
# file does not exist on remote (or file is empty, we uplod the local spelling file)	
if [ ! -f /tmp/temp_remote ]  || [ ! -s /tmp/temp_remote ]; then
	echo "1st Time uploading"
	upload
	# juste one file, synced
	exit
fi

echo "merging"

tr "\000" "\n" < /tmp/temp_remotebin > /tmp/temp_remotebinascii

#merging...
cat /tmp/temp_local /tmp/temp_remotebinascii | sort | uniq > /tmp/result


tr "\n" "\000" < /tmp/result > $SPELLINGFILE

# uploding merged file
upload


echo "end"
###	end

