#! /bin/sh
# Copyright (c) 2001 SuSE GmbH Nuernberg, Germany.  All rights reserved.
#               2002-2003 SuSE Linux AG, Nuernberg, Germany
#               2005      SUSE Linux Products GmbH, Nuernberg, Germany
#               2007      Wolfgang Rosenauer <wr@rosenauer.org>
#

# check if we are started as root
# only one of UID and USER must be set correctly
if test "$UID" != 0 -a "$USER" != root; then
    echo "You must be root to start $0."
    exit 1
fi

PREFIX="%PROGDIR"
MOZ_APP="%APPNAME"
JAVA="%JAVA"

get_arch ()
{
  file "$1" | sed -n 's/.*: ELF [^,]*, \([^,]*\),.*/\1/p'
}

mozilla_arch=`get_arch $PREFIX/$MOZ_APP-bin`
mozilla_lib=`file $PREFIX/$MOZ_APP-bin | awk '{ print $3 }'`

JAVA_CHECK_PATH="/usr/lib /usr/java"
JAVA_ORDER="mozilla ns7 ns610 [Ss]un IBM BEA"
case $mozilla_lib in
  64-bit)
        LIB=lib64
        ;;
  *)
        LIB=lib
        ;;
esac

find_java ()
{
  TMP=$(find $JAVA_CHECK_PATH -name "$1" -type f)
  for i in $TMP; do
    if [ "`get_arch $i`" = "$mozilla_arch" ]; then
      PLUGIN="$PLUGIN $i"
    fi
  done
  TMP=""
}

# JAVA
if [ $JAVA -eq 1 ]; then
  if [ ! -L $PREFIX/plugins/libjavaplugin_oji.so ] ||
     [ ! -f $PREFIX/plugins/libjavaplugin_oji.so ] ; then # link is here and valid
    PLUGIN=""
    find_java *javaplugin_oji.so
    find_java *javaplugin_ojigcc3.so # IBMJava
    if [ "$PLUGIN" ]; then
        for i in $PLUGIN; do
          if [[ $i == *mozilla* ]] || [[ $i == *gcc3* ]] || [[ $i == *ns7/* ]]; then
            TMP=$i
            break
          fi
        done
    fi
    if [ "$TMP" ]; then
      PLUGIN=$TMP
      ln -sf $PLUGIN $PREFIX/plugins/libjavaplugin_oji.so
      echo "-> added Java2 plugin ($PLUGIN)"
    else
      echo "-> Java2: no change (no suitable JRE available)"
    fi
  else
    echo "-> Java2: no change (existing link is valid)"
  fi
fi

# MySPELL
MYSPELL=/usr/share/myspell
MOZ_SPELL=$PREFIX/dictionaries
if [ -d $MOZ_SPELL ] ; then
  if [ -d $MYSPELL ] ; then
    for dict in $MYSPELL/??[-_]??.aff ; do

      # check is it is really the file or it is a string which contain '??_??'
      if ! [ -e $dict ] ; then
        continue
      fi

      # the dict file name
      dict_file=`echo ${dict##*/}`

      # the dict file has a valid name
      lang=`echo ${dict_file:0:2}`
      country=`echo ${dict_file:3:2}`
      
      # check for .dic file
      if [ ! -r $MYSPELL/${lang}[-_]${country}.dic ] ; then
        continue
      fi

      # create links
      if [ ! -r $MOZ_SPELL/${lang}[-_]${country}.aff ] ; then
        ln -sf $MYSPELL/${lang}[-_]${country}.aff \
               $MOZ_SPELL/${lang}-${country}.aff
      fi      
      if [ ! -r $MOZ_SPELL/${lang}[-_]${country}.dic ] ; then
        ln -sf $MYSPELL/${lang}[-_]${country}.dic \
               $MOZ_SPELL/${lang}-${country}.dic
      fi      
    done
    echo "-> added myspell dictionaries"
  fi

  # remove broken links
  for dict in $MOZ_SPELL/*.{aff,dic} ; do
    if ! [ -r $dict ] ; then
      rm -f $dict
    fi
  done
fi
