#!/bin/sh

    # OpenOffice.org 1.0ܸƳץ for Linux ver 0.5.0
    # (C) 2002-2003 OpenOffice.orgܥ桼. All Right Reserved.
    #
    # ΥץۡѤˡʤ餺READMEˤʤäƤ
    #ޤˡREADME˴ޤޤƤޤ

###########################################################################
# إפɽؿ                                                    #
###########################################################################
ShowHelp ()
{
cat <<EOF

OpenOffice.org 1.0ܸƳץ

ˡ: ./ja_config [-v|--version] [-h|--help] [-g|--gothic <gothic_font>] [-|--mincho <mincho_font>] [-u|--ui <ui_font>] [-net|-work] ooohome

    ooohome : OpenOffice.org 1.0򥤥󥹥ȡ뤷ǥ쥯ȥꤷޤ

    ץ 
	-v, --version
            СϤƽλޤ
	-h, --help
            ΥإפϤƽλޤ
	-g <gothic_font>, --gothic <gothicfont>
            <gothic_font>˻ꤵ줿եȤեȤִɸƥץ졼ȤɸեȤŬѤޤ
	    (ǥե : å)
        -m <mincho_font>, --mincho <mincho_font>
            <mincho_font>˻ꤵ줿եȤեȤִɸƥץ졼ȤΥեȤŬѤޤ
            (ǥե : ī)
        -u <ui_font>, --ui <ui_font>
            <ui_font>˻ꤵ줿եȤեȤִŬѤޤ
            (ǥե : <gothic_font>)
	-net
	    ͥåȥ󥹥ȡ뤷ǥ쥯ȥ򤹤٤ΥץǤ
	-work 
	    ơ󥤥󥹥ȡ뤷ǥ쥯ȥ򤹤٤ΥץǤ
EOF
}

###########################################################################
# Сɽؿ                                                #
###########################################################################
ShowVersion ()
{
cat <<EOF

OpenOffice.org 1.0ܸƳץ for Linux 0.5.0

(C) 2002-2003 OpenOffice.orgܥ桼
http://oooug.jp/

EOF
}

###########################################################################
# Windowsѥƥץ졼ȤΥե̾Ѵԡ                       #
#  $1 -> ˤʤWindowsѤΥǡ                                        #
#  $2 -> ѴեΥԡ                                       #
###########################################################################
ConvertTemplateFont ()
{ 
    # ѥǥ쥯ȥκ
    temp_dir="${script_dir}/temp"

    if [ -d ${temp_dir} ] ; then
	rm -rf ${temp_dir}
    fi

    mkdir $temp_dir
    cd "${script_dir}/temp"

    # ƥץ졼ȤβԤʤxmlեΥ󥳡ɤ
    # UTF-8EUC-JPѴ
    unzip -q -d ${temp_dir} $1
    iconv -c -f UTF-8 -t EUC-JP "${temp_dir}/content.xml" > "${temp_dir}/content.xml.euc" 2> /dev/null
    iconv -c -f UTF-8 -t EUC-JP "${temp_dir}/styles.xml" > "${temp_dir}/styles.xml.euc" 2> /dev/null
    iconv -c -f UTF-8 -t EUC-JP "${temp_dir}/meta.xml" > "${temp_dir}/meta.xml.euc" 2> /dev/null
    iconv -c -f UTF-8 -t EUC-JP "${temp_dir}/settings.xml" > "${temp_dir}/settings.xml.euc" 2> /dev/null

    # WindowsѥեȤִ
    sed -e s/"ͣ Хå"/"$gothic"/g -e s/"ͣ ī"/"$mincho"/g -e s/"ͣ å"/"$gothic"/g -e s/"ͣ ī"/"$mincho"/g -e s/"MS UI Gothic"/"$ui"/g content.xml.euc | iconv -f EUC-JP -t UTF-8 > content.xml
    sed -e s/"ͣ Хå"/"$gothic"/g -e s/"ͣ ī"/"$mincho"/g -e s/"ͣ å"/"$gothic"/g -e s/"ͣ ī"/"$mincho"/g -e s/"MS UI Gothic"/"$ui"/g styles.xml.euc | iconv -f EUC-JP -t UTF-8 > styles.xml
    sed -e s/"ͣ Хå"/"$gothic"/g -e s/"ͣ ī"/"$mincho"/g -e s/"ͣ å"/"$gothic"/g -e s/"ͣ ī"/"$mincho"/g -e s/"MS UI Gothic"/"$ui"/g meta.xml.euc | iconv -f EUC-JP -t UTF-8 > meta.xml
    sed -e s/"ͣ Хå"/"$gothic"/g -e s/"ͣ ī"/"$mincho"/g -e s/"ͣ å"/"$gothic"/g -e s/"ͣ ī"/"$mincho"/g -e s/"MS UI Gothic"/"$ui"/g settings.xml.euc | iconv -f EUC-JP -t UTF-8 > settings.xml

    # EUCե
    rm -f "${temp_dir}/content.xml.euc" "${temp_dir}/styles.xml.euc" "${temp_dir}/meta.xml.euc" "${temp_dir}/settings.xml.euc"

    # ɸΥե뤬¸ߤˤϥХååפȤ
    if [ -f $2 ] ; then
	mv -f $2 "$2~"
    fi    

    # zipɸ˥
    zip -rq $2 *

    # ȥǥ쥯ȥκ
    rm -rf ${temp_dir}

#    echo "$1 -> $2"
}

###########################################################################
# ᥤ롼                                                  #
###########################################################################

# ץȥե뤬ǥ쥯ȥ˰ư
cd `dirname $0`

# ץȥե뤬ǥ쥯ȥХѥǼ
script_dir=`pwd`


#ץΥǥեȤ
gothic=å
mincho=ī
ui=$gothic
single=true
net=false
work=false

#ν
while [ $# -gt 0 ] ; do
    case "$1" in
	-h | --help)
	    ShowHelp
	    exit 1;;
	-v | --version)
	    ShowVersion
	    exit 1;;
	-g | --gothic)
	    gothic=$2
	    shift 2;;
	-m | --mincho)
	    mincho=$2
	    shift 2;;
        -u | --ui)
            ui=$2
            shift 2;;
	-net)
	    single=false
	    net=true
	    work=false
	    shift 1;;
	-work)
	    single=false
	    net=false
	    work=true
	    shift 1;;
	*)
	    lastarg=$1
	    shift;;
    esac
done

#OpenOffice.org 1.0Υۡǥ쥯ȥ
ooohome=$lastarg
#桼ꤷOpenOffice.orgΥۡǥ쥯ȥνü"/"ǤˤϺ
ooohome=`echo ${ooohome} | sed -e s/\\\/$//`

#ꤷѥγǧ
echo "OpenOffice.orgΥ󥹥ȡϡ${ooohome}פǤǤ?(y/n)"
read YesNo
while [ "${YesNo}" = "n" ];do
    echo "OpenOffice.org򥤥󥹥ȡ뤷ѥϤƤ:"
    read ooohome
    echo "OpenOffice.orgΥ󥹥ȡϡ${ooohome}פǤǤ?(y/n)"
    read YesNo
done

#ꤵ줿OpenOffice.org 1.0Υۡǥ쥯ȥ꤬¸ߤʤϽλ
if [ ! -d ${ooohome}/user ] ; then
    echo "顼 : ꤵ줿ǥ쥯ȥ ${ooohome} ¸ߤޤ"
    exit 1
fi

# 桼ꤷOpenOffice.orgΥۡǥ쥯ȥνü"/"ǤäˤϺ
ooohome=`echo ${ooohome} | sed -e s/\\\/$//`
# "/"Ф륨ץ󥹤ղ
Eooohome=`echo ${ooohome} | sed -e s/\\\//\\\\\\\\\\\//g`

#OpenOffice.orgγƼƥץ졼Ȥ֤ǥ쥯ȥ
template_dir=${ooohome}/user/template

# 󥰥桼󥹥ȡ뤫ơ󥤥󥹥ȡξ
if [ ${single} = true -o ${work} = true ] ; then

# dataǥ쥯ȥΤ󥰥桼󥹥ȡʤsg
# ơ󥤥󥹥ȡʤwsǥ쥯ȥ
if [ ${single} = true ] ; then
    sg_ws=sg
else
    sg_ws=ws
fi

# եȤִꥹȤ򥳥ԡ
if [ -f "${ooohome}/user/config/registry/instance/org/openoffice/Office/Common.xml" ] ; then
    mv -f "${ooohome}/user/config/registry/instance/org/openoffice/Office/Common.xml" "${ooohome}/user/config/registry/instance/org/openoffice/Office/Common.xml~"
fi
iconv -f UTF-8 -t EUC-JP "${script_dir}/data/${sg_ws}/common.xml" | sed -e s/"ͣ Хå"/"${gothic}"/g -e s/"ͣ å"/"${gothic}"/g -e s/"ͣ ī"/"${mincho}"/g -e s/"ͣ ī"/"${mincho}"/g -e s/"MS UI Gothic"/"${ui}"/g | iconv -f EUC-JP -t UTF-8 > "${ooohome}/user/config/registry/instance/org/openoffice/Office/Common.xml"

# CalcΥץ򥳥ԡ
if [ -f "${ooohome}/user/config/registry/instance/org/openoffice/Office/Calc.xml" ] ; then
    mv -f "${ooohome}/user/config/registry/instance/org/openoffice/Office/Calc.xml" "${ooohome}/user/config/registry/instance/org/openoffice/Office/Calc.xml~"
fi
cp "${script_dir}/data/${sg_ws}/calc.xml" "${ooohome}/user/config/registry/instance/org/openoffice/Office/Calc.xml"

# ɸƥץ졼ȤΥԡ
ConvertTemplateFont "${script_dir}/data/template/normalwriter.stw" "${template_dir}/normalwriter.stw"
ConvertTemplateFont "${script_dir}/data/template/normalcalc.stc" "${template_dir}/normalcalc.stc"
ConvertTemplateFont "${script_dir}/data/template/normalimpress.sti" "${template_dir}/normalimpress.sti"
ConvertTemplateFont "${script_dir}/data/template/normaldraw.std" "${template_dir}/normaldraw.std"
ConvertTemplateFont "${script_dir}/data/template/normalmath.sxm" "${template_dir}/normalmath.sxm"

# ɸƥץ졼Ȥե֤
if [ -f "${ooohome}/user/config/registry/instance/org/openoffice/Setup.xml" ] ; then
    mv -f "${ooohome}/user/config/registry/instance/org/openoffice/Setup.xml" "${ooohome}/user/config/registry/instance/org/openoffice/Setup.xml~"
    iconv -f UTF-8 -t EUC-JP "${script_dir}/data/${sg_ws}/setup.xml" | sed -e s/"%%OOOINSTALLDIR%%"/"${Eooohome}"/g | iconv -f EUC-JP -t UTF-8 > "${ooohome}/user/config/registry/instance/org/openoffice/Setup.xml"
fi

# ¾Υƥץ졼ȤΥԡ
mkdir -p "${template_dir}/calc"
for calc_template in `ls ${script_dir}/data/template/calc`
do
    ConvertTemplateFont "${script_dir}/data/template/calc/${calc_template}" "${template_dir}/calc/${calc_template}"
done

mkdir -p "${template_dir}/draw"
for draw_template in `ls ${script_dir}/data/template/draw`
do
    ConvertTemplateFont "${script_dir}/data/template/draw/${draw_template}" "${template_dir}/draw/${draw_template}"
done

mkdir -p "${template_dir}/impress"
for impress_template in `ls ${script_dir}/data/template/impress`
do
    ConvertTemplateFont "${script_dir}/data/template/impress/${impress_template}" "${template_dir}/impress/${impress_template}"
done

mkdir -p "${template_dir}/impress_layout"
for impress_layout_template in `ls ${script_dir}/data/template/impress_layout`
do
    ConvertTemplateFont "${script_dir}/data/template/impress_layout/${impress_layout_template}" "${template_dir}/impress_layout/${impress_layout_template}"
done

if [ ${single} = true ] ; then
	mkdir -p ${ooohome}/share/samples/japanese
    # ɲååץȤ򥳥ԡ
    cp ${script_dir}/data/sample/draw/* ${ooohome}/share/samples/japanese/
fi

# ٥ǡ򥳥ԡ
cp ${script_dir}/data/etc/labels.xml ${ooohome}/user/config/registry/instance/org/openoffice/Office/labels.xml

fi # 󥰥桼󥹥ȡ뤫ơ󥤥󥹥ȡξ

# ͥåȥ󥹥ȡξ
if [ ${net} = true ] ; then
	mkdir -p ${ooohome}/share/samples/japanese
	# ɲååץȤ򥳥ԡ
	cp ${script_dir}/data/sample/draw/* ${ooohome}/share/samples/japanese/

fi # ͥåȥ󥹥ȡξ

echo "λ"

