#!/bin/sh


PKGNAME="chinese_pack"


insert_patch() {
    [ -z "$2" ] && return 1
    local PATCH="${3#*
}"
    PATCH="${PATCH%
*}"
    TEMP="`head -n $2 "$1"; echo "$PATCH"; tail -n +$(($2+1)) "$1"; echo PATCH`"
    echo "${TEMP%PATCH}" >"$1"
    return 0
}


insert_patch_grep() {
	insert_patch "$1" \
		"`grep -n -m 1 "$2" "$1" | awk -F: '{ print $1 }'`" \
		"$3"
}
	

insert_patch_append() {
	insert_patch "$1" \
    	"`wc -l "$1" | awk '{ print $1 }'`" \
    	"$2"
}
	

remove_patch() {
    local PATCH="${2#*
}"
    PATCH="${PATCH%
*}"
	[ -z "$PATCH" ] && return
	PATCH="${PATCH//\\/\\\\}"
    local TEMP="`awk -v p="$PATCH" '{ b=b"\n"$0; i=index(b"\n","\n"p"\n"); if (i!=0) b=substr(b,1,i-1) } END { print substr(b,2) }' "$1"; echo PATCH`"
    echo "${TEMP%?PATCH}" >"$1"
}


# fix chinese_pack-0.4 uninstall bug
PATCH='

## insert by chinese_pack-0.4
locale | grep "LC_CTYPE=\"\?zh_CN\." || export LC_CTYPE="zh_CN.UTF-8"
export XMODIFIERS="@im=fcitx"
export GTK_XIM_MODULES="fcitx"
export QT_XIM_MODULES="fcitx"
#config creation bug-fix for fcitx
if [ ! -e /root/.fcitx/config ] || [ "`md5sum /root/.fcitx/config`" = "eb8edb73159e0a6b228de1af183b87c9  /root/.fcitx/config" ]; then
 cp /usr/share/fcitx/data/config /root/.fcitx/config
fi
fcitx &
##########

'
remove_patch "/root/.xinitrc" "$PATCH"
remove_patch "/root/.xinitrc" "$PATCH"
remove_patch "/root/.xinitrc" "$PATCH"
remove_patch "/root/.xinitrc" "$PATCH"
remove_patch "/root/.xinitrc" "$PATCH"


# insert fcitx booting code
PATCH='

## insert by '"$PKGNAME"' ##
locale | grep "LC_CTYPE=\"\?zh_CN\." || export LC_CTYPE="zh_CN.UTF-8"
export XMODIFIERS="@im=fcitx"
export GTK_XIM_MODULES="fcitx"
export QT_XIM_MODULES="fcitx"
#config creation bug-fix for fcitx
if [ ! -e /root/.fcitx/config ] || [ "`md5sum /root/.fcitx/config`" = "eb8edb73159e0a6b228de1af183b87c9  /root/.fcitx/config" ]; then
 cp /usr/share/fcitx/data/config /root/.fcitx/config
fi
fcitx &
##########

'
insert_patch_grep "./root/.xinitrc" '^. /etc/rc.d/PUPSTATE$' "$PATCH"


# automount patch
PATCH="

## insert by $PKGNAME ##
awk '"'$1 !~ /^#/ && $1 != "none" && $2 != "none" { system("mkdir -p "$2) }'"' /etc/fstab
mount -a
##########

"
insert_patch_append "./etc/rc.d/rc.local" "$PATCH"


# seamonkey patch
PATCH='
user_pref("accessibility.typeaheadfind.autostart", false);
'
for i in `find ./root/.mozilla -wholename "./root/.mozilla/default/[^/]*/prefs.js"`; do
	insert_patch_grep "$i" '^user_pref("' "$PATCH"
done


# replace console icon on desktop
PATCH='
<rule match="/usr/bin/sakura">
<icon>/usr/local/lib/X11/pixmaps/console48.png</icon>
</rule>
'
insert_patch_grep "./root/Choices/ROX-Filer/globicons" '</rule>$' "$PATCH"

sed -i 's@>/usr/bin/rxvt</icon>@>/usr/bin/sakura</icon>@' ./root/Choices/ROX-Filer/PuppyPin


# set default terminal for rox
sed -i 's@<Option name="menu_xterm">xterm</Option>@<Option name="menu_xterm">sakura</Option>@' ./root/.config/rox.sourceforge.net/ROX-Filer/Options


sed -i '/^pref_editor_default_new_encoding=/ s/=.*$/=UTF-8/' ./root/.geany/geany.conf 


[ ! -e /usr/bin/xterm ] && ln -s rxvt /usr/bin/xterm


mkfontdir ./usr/share/fonts/default/misc
mkfontscale ./usr/share/fonts/default/misc


# bug-fix for didiwiki
chown spot:spot ./root/spot/.didiwiki

