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

OOVERSION=<OOVERSION>
OOVERSIONRC="$HOME/.sversionrc"
OOHOME="$HOME/.openoffice"

# 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
  elif grep -q "^OpenOffice.org 1.0.1=file://$OOHOME" $OOVERSIONRC; then
    perl -pi -e 's|^OpenOffice.org 1.0.1|OpenOffice.org 1.0.2|' $OOVERSIONRC
    if [ -f $OOHOME/user/config/registry/instance/org/openoffice/Office/Linguistic.xml ]; then
      # Check whether the expected set of Hyphenators is there
      add=yes
      for lang in cs-CZ da-DK de-DE en-US fr-CA fr-FR it-IT nl-NL ru-RU; do
	if ! grep -q '<cfg:any cfg:type="string" state="replaced" cfg:name="'$lang'">org.openoffice.lingu.LibHnjHyphenator</cfg:any>' \
	  $OOHOME/user/config/registry/instance/org/openoffice/Office/Linguistic.xml; then
	  add=no
	  break
	fi
      done
      if [ "$add" = yes ]; then
	for lang in de-AT de-CH en-CA en-GB es-ES pt-BR pt-PT sk-SK sl-SI uk-UA; do
	  if ! grep -q '<cfg:any cfg:type="string" state="replaced" cfg:name="'$lang'">org.openoffice.lingu.LibHnjHyphenator</cfg:any>' \
	    $OOHOME/user/config/registry/instance/org/openoffice/Office/Linguistic.xml; then
	    perl -pi -e 's|^([ 	]*<cfg:any cfg:type="string" state="replaced" cfg:name=")ru-RU(">org.openoffice.lingu.LibHnjHyphenator</cfg:any>[ 	]*$)|\1ru-RU\2\n\1'$lang'\2|' \
	      $OOHOME/user/config/registry/instance/org/openoffice/Office/Linguistic.xml
	  fi
	done
      fi
    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" ] ; 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
    # make the user/work link and use "My Documents" directory
    #mkdir -p $HOME/"My Documents"
    #ln -s $HOME/"My Documents" $OOHOME/user/work
    ln -s $HOME $OOHOME/user/work
    setup_xml=$HOME/.openoffice/user/config/registry/instance/org/openoffice/Setup.xml
    if ! grep -q '<ooLocale\|<L10N>' $setup_xml; then
      lang="$LC_ALL"
      if [ -z "$lang" ]; then
	lang="$LANG"
      fi
      lang=$(echo "$lang" | sed -n "s/\([a-z]*_[A-Z]*\).*/\1/p")
      if [ -z "$lang" ]; then
	lang=en_US
      fi
      cp -a $setup_xml $setup_xml.tmp
      sed -e '/^<Setup/a\
 <L10N>\
  <ooLocale cfg:type="string">en-US</ooLocale>\
  <ooSetupSystemLocale cfg:type="string">'${lang/_/-}'</ooSetupSystemLocale>\
 </L10N>
' $setup_xml.tmp > $setup_xml
      rm -f $setup_xml.tmp
      linguistic_xml=$HOME/.openoffice/user/config/registry/instance/org/openoffice/Office/Linguistic.xml
      if [ ! -f $linguistic_xml ]; then
	cat > $linguistic_xml <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<Linguistic state="modified" cfg:package="org.openoffice.Office" xmlns="http://openoffice.org/2000/registry/components/Linguistic" xmlns:cfg="http://openoffice.org/2000/registry/instance" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
 <General>
  <DefaultLocale cfg:type="string">${lang/_/-}</DefaultLocale>
 </General>
 <ServiceManager>
EOF
	sed -n 's/^.*\(<HyphenatorList\)/  \1/;s/cfg:derivedBy="list"/state="replaced"/;/<HyphenatorList/,/<\/HyphenatorList>/p' \
	  /usr/lib/openoffice/share/config/registry/instance/org/openoffice/Office/Linguistic.xml \
	  >> $linguistic_xml
	cat >> $linguistic_xml <<EOF
 </ServiceManager>
</Linguistic>
EOF
      fi
    fi
    case `LC_ALL=C xdpyinfo | grep dimensions | head -1` in
      *640x*pixels*|*800x*pixels*) scale=120;;
      *1152x*pixels*|*1024x*pixels*) scale=110;;
      *) scale=100;;
    esac
    common_xml=$HOME/.openoffice/user/config/registry/instance/org/openoffice/Office/Common.xml
    if ! grep -q '<Font\|<Substitution\|<FontPairs\|<FontReplacement\|<View' $common_xml; then
      cp -a $common_xml $common_xml.tmp
      sed -e '/^<Common/a\
 <Font>\
  <Substitution>\
   <FontPairs cfg:element-type="FontReplacement">\
    <FontReplacement state="replaced" cfg:name="_0">\
     <Always cfg:type="boolean">true</Always>\
     <OnScreenOnly cfg:type="boolean">true</OnScreenOnly>\
     <ReplaceFont cfg:type="string">Andale Sans UI</ReplaceFont>\
     <SubstituteFont cfg:type="string">Luxi Sans</SubstituteFont>\
    </FontReplacement>\
   </FontPairs>\
   <Replacement cfg:type="boolean">true</Replacement>\
  </Substitution>\
 </Font>\
 <View>\
  <FontAntiAliasing>\
   <MinPixelHeight cfg:type="short">8</MinPixelHeight>\
  </FontAntiAliasing>\
  <FontScaling cfg:type="short">'$scale'</FontScaling>\
 </View>
' $common_xml.tmp > $common_xml
      rm -f $common_xml.tmp
    fi
    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
      lang="$LC_ALL"
      if [ -z "$lang" ]; then
	lang="$LANG"
      fi
      lang=$(echo "$lang" | sed -n "s/\([a-z]*_[A-Z]*\).*/\1/p")
      if [ -z "$lang" ]; then
	lang=en_US
      fi
      # 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=1\
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_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/ja/TrueType;||g")

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

export SAL_FONTPATH_USER

# Adjust what user interface language we want to use
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 [ -n "$lang" ] && \
   [ -e "/usr/lib/openoffice/help/${lang%%-*}" -o \
     -e "/usr/lib/openoffice/help/$lang" ]; then
  setup_xml=$HOME/.openoffice/user/config/registry/instance/org/openoffice/Setup.xml
  oldlang=`sed -n 's|^.*<ooLocale cfg:type="string">\([a-z]*-[A-Z]*\)</ooLocale>.*$|\1|p' $setup_xml`
  if [ -n "$oldlang" -a "$lang" != "$oldlang" ]; then
    cp -a $setup_xml $setup_xml.tmp
    sed 's|\(<ooLocale cfg:type="string">\)'$oldlang'\(</ooLocale>\)|\1'$lang'\2|' \
      $setup_xml.tmp > $setup_xml
    rm -f $setup_xml.tmp
    common_xml=$HOME/.openoffice/user/config/registry/instance/org/openoffice/Office/Common.xml
    case "$lang" in
      ja-JP) font="Kochi Gothic";;
      zh-TW) font="AR PL Mingti2L Big5";;
      zh-CN) font="ZYSong18030";;
      ko-KR) font="Baekmuk Dotum";;
      ru-RU) font="Nimbus Sans L";;
      *) font="Luxi Sans";;
    esac
    case "$oldlang" in
      ja-JP) oldfont="Kochi Gothic";;
      zh-TW) oldfont="AR PL Mingti2L Big5";;
      zh-CN) oldfont="ZYSong18030";;
      ko-KR) oldfont="Baekmuk Dotum";;
      ru-RU) oldfont="Nimbus Sans L";;
      *) oldfont="Luxi Sans";;
    esac
    if [ "$font" != "$oldfont" ]; then
      cp -a $common_xml $common_xml.tmp
      sed '/<Font>/,/<\/Font>/{/<Substitution>/,/<\/Substitution>/{/<FontPairs/,/<\/FontPairs>/{/<FontReplacement state="replaced"/,/<\/FontReplacement>/{/<ReplaceFont cfg:type="string">Andale Sans UI<\/ReplaceFont>/,/<\/FontReplacement>/{s/^\([ 	]*<SubstituteFont cfg:type="string">\)'"$oldfont"'\(<\/SubstituteFont>[ 	]*\)$/\1'"$font"'\2/;};};};};}' \
	$common_xml.tmp > $common_xml
      rm -f $common_xml.tmp
    fi
  fi
fi

echo "Starting OpenOffice.org ..."
case "$0" in
  *oosetup) exec $OOHOME/setup "$@";;
  *oopadmin) exec $OOHOME/spadmin "$@";;
  *) exec $OOHOME/soffice "$@";;
esac
