#!/bin/sh
## ---------------------------------
## Wrapper script for OpenOffice.org
## ---------------------------------

OOVERSION=641
OOVERSIONRC="$HOME/.sversionrc"
OOHOME="$HOME/.openoffice"

# Remove any entry in .sversionrc if not already installed from MDK package
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
fi

# Issue a workstation-type installation for the user, if necessary
if [ ! -d "$OOHOME" ]; then
  /usr/lib/openoffice/program/setup -R:/etc/openoffice/autoresponse.conf
fi

# Fix up English wordbook (check dictionary.lst because user may have
# removed en_US dictionary afterward)
if [ ! -e "$OOHOME/user/wordbook/dictionary.lst" ]; then
(cd $OOHOME/user/wordbook;
  for file in /usr/lib/openoffice/share/wordbook/english/en_US.???; do
    ln -s $file .
  done
  echo "DICT en US en_US" > dictionary.lst
)
fi

# Register localized dictionary
# (dictionary.lst is bound to exist, see above)
lang=$(sed -n "/LC_MESSAGES=\([a-z]*_[A-Z]*\).*/s//\1/p" /etc/sysconfig/i18n)
if ! grep -q "$lang" "$OOHOME/user/wordbook/dictionary.lst"; then
  if [ -f "/usr/lib/myspell/$lang.aff" ]; then
  (cd $OOHOME/user/wordbook;
    for file in /usr/lib/myspell/$lang.???; do
      ln -sf $file .
    done
    echo "DICT ${lang%%_*} ${lang##*_} $lang" >> dictionary.lst
  )
  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/X11R6/lib/X11/fonts/drakfont/ttf"
  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_USER="$fontpath;$SAL_FONTPATH_USER"

# Get rid of broken Japanese fonts for now
SAL_FONTPATH_USER=$(echo "$SAL_FONTPATH_USER" | sed -e "s|/usr/share/fonts/ttf/japanese;||g")

export SAL_FONTPATH_USER
exec $OOHOME/soffice "$@"
