#!/bin/sh

# library path
#PREF=/tmp # for DEBUG
PREF=~/Library/Preferences
ASKK=~/Library/AquaSKK

if [ ! -d $ASKK ]; then
    mkdir -p $ASKK
    chown $USER $ASKK
fi

###
### generate new user defaults
###

OLD_DOMAIN="org.ccm-software.AquaSKKServer"
NEW_DOMAIN="jp.sourceforge.AquaSKKServer"

OLD_PATH=$PREF/$OLD_DOMAIN
OLD_PLIST=$OLD_PATH.plist

NEW_PATH=$PREF/$NEW_DOMAIN
NEW_PLIST=$NEW_PATH.plist

# not exists ?
if [ ! -f $NEW_PLIST ]; then
    # copy old user defaults
    cp $OLD_PLIST $NEW_PLIST

    # rename keys
    defaults rename $NEW_PATH dic.FontName.Window			candidates_font_name
    defaults rename $NEW_PATH dic.FontSize.Window			candidates_font_size
    defaults rename $NEW_PATH dic.WindowAlpha.kotoeri			candidates_window_alpha
    defaults rename $NEW_PATH dic.WindowColor.kotoeri			candidates_window_color
    defaults rename $NEW_PATH dic.skk-egg-like-newline.kotoeri		egg_like_newline
    defaults rename $NEW_PATH ForceASCIImodeStartup			force_ascii_mode_startup
    defaults rename $NEW_PATH KeyboardLayoutIdentifier			keyboard_layout_id
    defaults rename $NEW_PATH dic.NumericKeypad_HalfWidth.kotoeri	numkeypad_use_halfwidth
    defaults rename $NEW_PATH pref.skkserv.enabled			skkserv_enabled
    defaults rename $NEW_PATH pref.skkserv.local-only			skkserv_localonly
    defaults rename $NEW_PATH pref.skkserv.port				skkserv_port

    # delete unused keys
    defaults delete $NEW_PATH dic.path.kotoeri
    defaults delete $NEW_PATH dic.path.main
    defaults delete $NEW_PATH dic.path.sub

    # change owner
    chown $USER $NEW_PLIST
fi

###
### generate DictionarySet.plist
###

DIC_PATH=$ASKK/DictionarySet
DIC_PLIST=$DIC_PATH.plist

# already exists ?
if [ -f $DIC_PLIST ]; then
    echo $DIC_PLIST already exists.
    exit 0;
fi

# create initial plist
cat >$DIC_PLIST<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
EOF

# read old paths
SUB_PATH=`defaults read $OLD_PATH dic.path.sub`
MAIN_PATH=`defaults read $OLD_PATH dic.path.main`
KOTOERI_PATH=`defaults read $OLD_PATH dic.path.kotoeri|sed 's/[,()"]//g'`

# skk dictionary
if [ -z $SUB_PATH ]; then
    SUB_PATH=~/.skk-jisyo
fi
cat >>$DIC_PLIST<<EOF
	<dict>
		<key>active</key>
		<true/>
		<key>location</key>
		<string>${SUB_PATH}</string>
		<key>type</key>
		<integer>10</integer>
	</dict>
EOF

if [ -z $MAIN_PATH ]; then
    MAIN_PATH=SKK-JISYO.L
    DICT_TYPE=11
else
    DICT_TYPE=10
fi
cat >>$DIC_PLIST<<EOF
	<dict>
		<key>active</key>
		<true/>
		<key>location</key>
		<string>${MAIN_PATH}</string>
		<key>type</key>
		<integer>${DICT_TYPE}</integer>
	</dict>
EOF

# kotoeri dictionary
for path in $KOTOERI_PATH; do 
cat >>$DIC_PLIST<<EOF
	<dict>
		<key>active</key>
		<true/>
		<key>location</key>
		<string>${path}</string>
		<key>type</key>
		<integer>20</integer>
	</dict>
EOF
done

cat >>$DIC_PLIST<<EOF
</array>
</plist>
EOF

# change owner
chown $USER $DIC_PLIST

exit 0
