#!/bin/sh
# -----------------------------------------------------
#      Red Hat wrapper script for OpenOffice.org
# -----------------------------------------------------
# (partially based on Mandrake wrapper script for 641c)

langCode="01"

ConvertLangStringToNum()
{
	case "$1" in
		en*)	langCode="01";;
		de*)	langCode="49";;
		it*)	langCode="39";;
		es*)	langCode="34";;
		fr*)	langCode="33";;
		nl*)	langCode="31";;
		sv*)	langCode="46";;
		pt*)	langCode="03";;
		ja*)	langCode="81";;
		ar*)	langCode="96";;
		tr*)	langCode="90";;
		fi*)	langCode="35";;
		ru*)	langCode="07";;
		ca*)	langCode="37";;
		cs*)	langCode="42";;
		sk*)	langCode="43";;
		da*)	langCode="45";;
		pl*)	langCode="48";;
		el*)	langCode="30";;
		ko*)	langCode="82";;
		zh-CN*) langCode="86";;
		zh-TW*) langCode="88";;
		*)	langCode="01";;
	esac
}

OOVERSION=1.1.0
OOVERSIONRC="$HOME/.sversionrc"
OOHOME="$HOME/.openoffice"
OOPROGRAM=/usr/lib/openoffice/program
oohome=

# grab current language
lang="$LC_ALL"
if [ -z "$lang" ]; then
	lang="$LC_MESSAGES"
	if [ -z "$lang" ]; then
		lang="$LANG"
	fi
fi
lang=$(echo "$lang" | sed -n "s/\([a-z]*_[A-Z]*\).*/\1/p")
lang=${lang/_/-}
if [ -z "$lang" ]; then
	lang=en-US
fi

# Remove any entry in .sversionrc if not already installed from RPM
if [ -f "$OOVERSIONRC" ]; then
	oohome=$(cat $OOVERSIONRC | tr '\r' '\n' | perl -n -e "m|^OpenOffice.org ${OOVERSION}=file://(.*)$| and print \$1")
	if [ -n "$oohome" -a "$oohome" != "$OOHOME" ]; then
		cp $OOVERSIONRC $OOVERSIONRC.orig
		perl -ni -e "m|file://${oohome}| or print" $OOVERSIONRC
	fi
	if grep -q "^OpenOffice.org ${OOVERSION}" $OOVERSIONRC; then
		:
	else
		# If we need to update an install of OOo 1.0.2,
		# synchronize the language between new install and old
		if [ -f "$HOME/.openoffice/instdb.ins" ]; then
			# Also remove the ~/.openoffice/user/work link.  It tends
			# to make people delete their home directories
			rm -f $HOME/.openoffice/user/work

			ConvertLangStringToNum "$lang"
			instdb_ins=$HOME/.openoffice/instdb.ins
			cp -a $instdb_ins $instdb_ins.tmp
			sed 's/^.*Languages[ \t]*=[ ]*"00";.*$/Languages=\"'$langCode'\";/' $instdb_ins.tmp > $instdb_ins
			rm -f $instdb_ins.tmp
		fi
	fi
fi

# Issue a workstation-type installation for the user, if necessary
if [ -f "$OOVERSIONRC" -a -d "$OOHOME" -a -e "$OOHOME/soffice" -a -e "$OOHOME/spadmin" -a -n "$oohome" ] ; then
  :
else
	echo "Checking for existing user installation ...   ...   NOT FOUND"
	echo -n "Performing first-time installation for user ...   "
	if grep -q application/vnd.s $HOME/.mime.types 2>/dev/null; then
		# setup adds .mime.types unconditionally
		sed -e '/^application\/vnd.stardivision.calc sdc$/d' \
		-e '/^application\/vnd.stardivision.draw sda$/d' \
		-e '/^application\/vnd.stardivision.impress sdd sdp$/d' \
		-e '/^application\/vnd.stardivision.math smf$/d' \
		-e '/^application\/vnd.stardivision.writer-global sgl$/d' \
		-e '/^application\/vnd.stardivision.writer sdw vor$/d' \
		-e '/^application\/vnd.sun.xml.calc sxc$/d' \
		-e '/^application\/vnd.sun.xml.calc.template stc$/d' \
		-e '/^application\/vnd.sun.xml.draw sxd$/d' \
		-e '/^application\/vnd.sun.xml.draw.template std$/d' \
		-e '/^application\/vnd.sun.xml.impress sxi$/d' \
		-e '/^application\/vnd.sun.xml.impress.template sti$/d' \
		-e '/^application\/vnd.sun.xml.math sxm$/d' \
		-e '/^application\/vnd.sun.xml.writer.global sxg$/d' \
		-e '/^application\/vnd.sun.xml.writer sxw$/d' \
		-e '/^application\/vnd.sun.xml.writer.template stw$/d' \
		$HOME/.mime.types > $HOME/.mime.types.tmp \
		&& mv -f $HOME/.mime.types.tmp $HOME/.mime.types
	fi
	# rename old .sversonrc
	[ -f $HOME/.sversionrc ] && mv -f $HOME/.sversionrc $HOME/.sversionrc.old
	# run the automated setup
	if /usr/lib/openoffice/program/setup -r:/etc/openoffice/autoresponse.conf; then
		case `LC_ALL=C xdpyinfo | grep dimensions | head -1` in
			*640x*pixels*|*800x*pixels*) scale=120;;
			*1152x*pixels*|*1024x*pixels*|1280x*pixels*) scale=110;;
			*) scale=100;;
		esac
		if [ ! -f $HOME/.openoffice/user/psprint/psprint.conf ]; then
			# Default to paper size from locale setting
			case `/usr/bin/locale -k LC_PAPER | grep width` in
				*216) paper="Letter";;
				*) paper="A4";;
			esac

			# Printer font substitution looks horrible for ISO-8859-2
			# languages, since some letters are taken from the printer
			# fonts and accented letters in between are downloaded
			case "$lang" in
				bs*|cs*|hr*|hu*|pl*|ro*|sk*|sl*|sr*) subst=false;;
				*) subst=true;;
			esac
			sed -n -e '/^\[Generic Printer/,${s/\(PerformFontSubstitution=\)\(false\|true\)/\1'$subst'/;s/Command=/Command=lpr/;/^[[A-Za-z]/p;/^Command=/a\
Features=\
Copies=1\
Scale=100\
Orientation=Portrait\
PSLevel=0\
ColorDevice=0\
ColorDepth=24\
MarginAdjust=-1,-1,-1,-1\
PPD_PageSize='$paper'
;}' /usr/lib/openoffice/share/psprint/psprint.conf > $HOME/.openoffice/user/psprint/psprint.conf
			echo >> $HOME/.openoffice/user/psprint/psprint.conf
	
			sed -n -e '/^\[Generic Printer/,${s/\(PerformFontSubstitution=\)\(false\|true\)/\1'$subst'/;s/Command=/Command=\/usr\/bin\/gs -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile="(OUTFILE)" -/;s/Generic Printer/PDF converter/;s/\(DefaultPrinter\)=1/\1=0/;/^[[A-Za-z]/p;/^Command=/a\
Features=pdf=\
Copies=1\
Scale=0\
Orientation=Portrait\
PSLevel=0\
ColorDevice=0\
ColorDepth=24\
MarginAdjust=0,0,0,0\
PPD_PageSize='$paper'
;}' /usr/lib/openoffice/share/psprint/psprint.conf >> $HOME/.openoffice/user/psprint/psprint.conf
		fi
		echo "...   DONE"
	else
		echo "...   FAILED"
		exit 1
	fi
fi

# Expand TrueType and Type1 font paths
chkfontpath="/usr/sbin/chkfontpath"
if [ -x "$chkfontpath" ]; then
	fontpath=$($chkfontpath | perl -ne '/[0-9]+: (.*)/ and print "$1;"')
else
	ttf_fontpath="/usr/X11R6/lib/X11/fonts/TTF;/usr/share/fonts/default/TrueType"
	for fontdir in /usr/share/fonts/ttf/*; do
		ttf_fontpath="$ttf_fontpath;$fontdir"
	done
	type1_fontpath="/usr/X11R6/lib/X11/fonts/Type1;/usr/share/fonts/default/Type1"
	fontpath="$ttf_fontpath;$type1_fontpath"
fi
SAL_FONTPATH_PRIVATE="$fontpath;$SAL_FONTPATH_PRIVATE;/usr/share/fonts/monotype"
export SAL_FONTPATH_PRIVATE

# Synchronize OOo UI language with LANG
cat >>SetupXcu.temp <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Setup" oor:package="org.openoffice">
 <node oor:name="L10N">
  <prop oor:name="ooLocale" oor:type="xs:string">
   <value>'$lang'</value>
  </prop>
 </node>
</oor:component-data>
EOF
$OOPROGRAM/configimport.bin SetupXcu.temp
rm -f SetupXcu.temp

# Turn off embedded bitmaps for Kochi Mincho/Kochi Gothic fonts
case "$lang" in
	ja*) SAL_EMBEDDED_BITMAP_PRIORITY=1;;
	*) SAL_EMBEDDED_BITMAP_PRIORITY=2;;
esac
export SAL_EMBEDDED_BITMAP_PRIORITY

echo "Starting OpenOffice.org ..."
case "$0" in
	*oosetup) exec $OOHOME/setup "$@";;
	*oopadmin) exec $OOHOME/spadmin "$@";;
	*) # exec $OOHOME/soffice "$@"
	case "$@" in
		*writer*) pagein=@pagein-writer;;
		*draw*) pagein=@pagein-draw;;
		*calc*) pagein=@pagein-calc;;
		*impress*) pagein=@pagein-impress;;
		*) pagein=;;
	esac
	unset XENVIRONMENT
	$OOPROGRAM/pagein -L$OOPROGRAM @pagein-common $pagein
	SO_MODE=local OPENOFFICE_MOZILLA_FIVE_HOME=$OOPROGRAM PATH=$OOPROGRAM:$PATH exec $OOPROGRAM/soffice.bin "$@";;
esac
