#!/bin/sh

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

ShowHelp ()
{
cat <<EOF

OpenOffice.org 1.0ܸƳץ

ˡ: ./ja_config [-v|--version] [-h|--help] [-f|--font <font>] ooohome

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

    ץ 
	-v, --version
            СϤƽλޤ
	-h, --help
            ΥإפϤƽλޤ
	-f <font>, --font <font>
            <font>˻ꤵ줿եȤOpenOffice.org 1.0ɸեȤˤޤ
	    (ǥե : å)

EOF
}

ShowVersion ()
{
cat <<EOF

OpenOffice.org 1.0ܸƳץ for Linux 0.1.1

(C) 2002 OpenOffice.orgܥ桼
http://blow-away.net/openoffice/

EOF
}

cd `dirname $0`

#ץΥǥեȤ
font_name=å

#ν
while [ $# -gt 0 ] ; do
    case "$1" in
	-h | --help)
	    ShowHelp
	    exit 1;;
	-v | --version)
	    ShowVersion
	    exit 1;;
	-f | --font)
	    font_name=$2
	    shift 2;;
	*)
	    lastarg=$1
	    shift;;
    esac
done

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

#桼ꤷOpenOffice.orgΥۡǥ쥯ȥνü"/"ǤäˤϺ
    ooohome=`echo ${ooohome} | sed -e s/\\\/$//g`
#OpenOffice.orguserǥ쥯ȥ
    user_dir=${ooohome}/user
#OpenOffice.orguserǥ쥯ȥǡ"/"Фƥץ"\\/"ղäʸ
    Euser_dir=`echo ${user_dir} | sed -e s/\\\//\\\\\\\\\\\//g`
#OpenOffice.orgCommon.xml֤Ƥǥ쥯ȥ
    common_dir=${user_dir}/config/registry/instance/org/openoffice/Office
#OpenOffice.orgSetup.xml֤Ƥǥ쥯ȥ
    setup_dir=${user_dir}/config/registry/instance/org/openoffice
#OpenOffice.orgγƼƥץ졼Ȥ֤ǥ쥯ȥ
    template_dir=${user_dir}/template
#OpenOffice.orgγƼƥץ졼Ȥ֤ǥ쥯ȥǡ"/"Фƥץ"\\/"ղäʸ
    Etemplate_dir=`echo ${template_dir} | sed -e s/\\\//\\\\\\\\\\\//g`

#Common.xmlν
#
#Common.xmlοǤCommon.xml.tmp"FONT_NAME"ȤʸɸǻѤեִ̾OpenOffice.orgCommon.xml֤ǥ쥯ȥCommon.xmlȤưưǤˤCommon.xml񤭤Ƥޤ
#ޤܸޤե̾ꤵ줿ȤΤᡢiconvˤäʸɤUTF-8ѴƤ
    rm -f Common.xml
    iconv -f UTF-8 -t EUC-JP Common.xml.tmp | sed -e s/FONT_NAME/"$font_name"/g | iconv -f EUC-JP -t UTF-8 -o Common.xml

    cp -f Common.xml ${common_dir}
    echo "Common.xml  ${common_dir} ˥ԡޤ"

#Setup.xmlν
#
#Setup.xmlοǤSetup.xml.tmp"TEMPLATE_DIR"ȤʸƼƥץ졼Ȥ֤ǥ쥯ȥ̾ˡ"SETUP_INSTALL_DIR"ȤʸOpenOffice.orguserǥ쥯ȥִ̾OpenOffice.orgSetup.xml֤ǥ쥯ȥSetup.xmlȤưưǤˤSetup.xml˾񤭤Ƥޤ
#ޤܸޤǥ쥯ȥ̾ꤵ줿ȤΤᡢiconvˤäʸɤUTF-8ѴƤ
    rm -f Setup.xml
    iconv -f UTF-8 -t EUC-JP Setup.xml.tmp | sed -e s/TEMPLATE_DIR/"${Etemplate_dir}"/g -e s/SETUP_INSTALL_DIR/"${Euser_dir}"/g | iconv -f EUC-JP -t UTF-8 -o Setup.xml

    cp -f Setup.xml ${setup_dir}
    echo "Setup.xml  ${setup_dir} ˥ԡޤ"

#Ƽƥץ졼ȤΥԡ
#
#normal.stw,normal.stc,normal.std,normal.sxm,normalinmpress.stiOpenOffice.orgγƼƥץ졼Ȥ֤ǥ쥯ȥ˥ԡ롣 

#normal.stwοǤnormal.stw.tmpʲγƥե"FONT_NAME"ȤʸɸǻѤե̾ѴʸɤUTF-8ѴγƼեzipǰ̤normal.stwΤޤޥƥץ졼Ȥ֤ǥ쥯ȥ˥ԡ
    cd "normal.stw.tmp"
    sed -e s/FONT_NAME/"${font_name}"/g content.xml.euc | iconv -f EUC-JP -t UTF-8 -o content.xml
    sed -e s/FONT_NAME/"${font_name}"/g meta.xml.euc | iconv -f EUC-JP -t UTF-8 -o meta.xml
    sed -e s/FONT_NAME/"${font_name}"/g settings.xml.euc | iconv -f EUC-JP -t UTF-8 -o settings.xml
    sed -e s/FONT_NAME/"${font_name}"/g styles.xml.euc | iconv -f EUC-JP -t UTF-8 -o styles.xml

    zip -rq normal.stw content.xml meta.xml settings.xml styles.xml META-INF
    cp -f normal.stw ${template_dir}
    echo "normal.stw  ${template_dir} ˥ԡޤ"   

#normal.stcοǤnormal.stc.tmpʲγƥե"FONT_NAME"ȤʸɸǻѤե̾ѴʸɤUTF-8ѴγƼեzipǰ̤normal.stcΤޤޥƥץ졼Ȥ֤ǥ쥯ȥ˥ԡ
    cd "../normal.stc.tmp"
    sed -e s/FONT_NAME/"${font_name}"/g content.xml.euc | iconv -f EUC-JP -t UTF-8 -o content.xml
    sed -e s/FONT_NAME/"${font_name}"/g meta.xml.euc | iconv -f EUC-JP -t UTF-8 -o meta.xml
    sed -e s/FONT_NAME/"${font_name}"/g settings.xml.euc | iconv -f EUC-JP -t UTF-8 -o settings.xml
    sed -e s/FONT_NAME/"${font_name}"/g styles.xml.euc | iconv -f EUC-JP -t UTF-8 -o styles.xml

    zip -rq normal.stc content.xml meta.xml settings.xml styles.xml META-INF
    mv -f normal.stc ${template_dir}
    echo "normal.stc  ${template_dir} ˥ԡޤ"

#normal.stdοǤnormal.std.tmpʲγƥե"FONT_NAME"ȤʸɸǻѤե̾ѴʸɤUTF-8ѴγƼեzipǰ̤normal.stdΤޤޥƥץ졼Ȥ֤ǥ쥯ȥ˥ԡ
    cd "../normal.std.tmp"
    sed -e s/FONT_NAME/"${font_name}"/g content.xml.euc | iconv -f EUC-JP -t UTF-8 -o content.xml
    sed -e s/FONT_NAME/"${font_name}"/g meta.xml.euc | iconv -f EUC-JP -t UTF-8 -o meta.xml
    sed -e s/FONT_NAME/"${font_name}"/g settings.xml.euc | iconv -f EUC-JP -t UTF-8 -o settings.xml
    sed -e s/FONT_NAME/"${font_name}"/g styles.xml.euc | iconv -f EUC-JP -t UTF-8 -o styles.xml

    zip -rq normal.std content.xml meta.xml settings.xml styles.xml META-INF Pictures
    mv -f normal.std ${template_dir}
    echo "normal.std  ${template_dir} ˥ԡޤ"

#normal.sxm򤽤Τޤޥԡ
    cd ".."
    cp -f normal.sxm ${template_dir}
    echo "normal.sxm  ${template_dir} ˥ԡޤ"

#normalimpress.stiοǤnormalimpress.sti.tmpʲγƥե"FONT_NAME"ȤʸɸǻѤե̾ѴʸɤUTF-8ѴγƼեzipǰ̤normalimpress.stiΤޤޥƥץ졼Ȥ֤ǥ쥯ȥ˥ԡ
    cd "normalimpress.sti.tmp"
    sed -e s/FONT_NAME/"${font_name}"/g content.xml.euc | iconv -f EUC-JP -t UTF-8 -o content.xml
    sed -e s/FONT_NAME/"${font_name}"/g meta.xml.euc | iconv -f EUC-JP -t UTF-8 -o meta.xml
    sed -e s/FONT_NAME/"${font_name}"/g settings.xml.euc | iconv -f EUC-JP -t UTF-8 -o settings.xml
    sed -e s/FONT_NAME/"${font_name}"/g styles.xml.euc | iconv -f EUC-JP -t UTF-8 -o styles.xml

    zip -rq normalimpress.sti content.xml meta.xml settings.xml styles.xml META-INF Pictures
    mv -f normalimpress.sti ${template_dir}
    echo "normalimpress.sti  ${template_dir} ˥ԡޤ"

#꡼󥢥å
cd ".."
rm -f Common.xml
rm -f Setup.xml
cd "normal.stw.tmp"
rm -f normal.stw content.xml meta.xml settings.xml styles.xml
cd "../normal.stc.tmp"
rm -f normal.stc content.xml meta.xml settings.xml styles.xml
cd "../normal.std.tmp"
rm -f normal.std content.xml meta.xml settings.xml styles.xml
cd "../normalimpress.sti.tmp"
rm -f normalimpress.sti content.xml meta.xml settings.xml styles.xml

echo "λ"