#!/bin/sh

# Required to check on shm_allow_removed if it is disable then set the
# OPERA_NUM_XSHM to 0 or it will causing Opera to halt or/and give strange
# behavior.
if [ `/sbin/sysctl -n kern.ipc.shm_allow_removed` -eq 0 ]; then
	OPERA_NUM_XSHM="0"
	export OPERA_NUM_XSHM
	echo "shm_allow_removed is disable, set OPERA_NUM_XSHM to 0 to disable shared memory."
fi

# Required for Session Management
case $0 in /*) OPERA_SCRIPT_PATH=$0;; *) OPERA_SCRIPT_PATH=/work/a/ports/www/linux-opera/work/opera-9.64-2480.gcc4-static-qt3.i386/$0;; esac
export OPERA_SCRIPT_PATH

# Location of the Opera binaries
OPERA_BINARYDIR=/usr/local/share/linux-opera/bin
export OPERA_BINARYDIR

# Parse commandline parameters
toset=
_QTSTYLE_FROM_CMDLINE_='NO'
for arg
do
  if test "$toset"
  then
      eval $toset="$arg"
      export $toset
      toset=
  else
      case $arg in
          --binarydir*|-binarydir*) toset=OPERA_BINARYDIR ;;
          --personaldir*|-personaldir*|--pd*|-pd*) toset=OPERA_PERSONALDIR ;;
          -style)
              _QTSTYLE_FROM_CMDLINE_='YES'
              ;;
      esac
  fi
done
test -z "$toset" || die Missing parameter for $arg

test "${_QTSTYLE_FROM_CMDLINE_}" = 'NO' && test -f "$HOME/.qt/qtrc" && _OPERA_QTSTYLE=`sed -n 's/^style=//p' $HOME/.qt/qtrc` && test "$_OPERA_QTSTYLE" && set -- "$@" -style "$_OPERA_QTSTYLE"


# Add ending path divider "/" if missing, so execution won't fail
if test `echo ${OPERA_BINARYDIR} | sed -e 's/.*[^\/]$/NO/'` = 'NO'; then
OPERA_BINARYDIR="${OPERA_BINARYDIR}/"
fi
if test ! -f ${OPERA_BINARYDIR}opera; then
    echo "The Opera binary is not located at \"${OPERA_BINARYDIR}opera\"."
    echo "Please modify the wrapper script at \"${0}\"."
    exit 1
elif test ! -x "${OPERA_BINARYDIR}opera"; then
    echo "You do not have execute rights on \"${OPERA_BINARYDIR}opera\", please ask the sysadmin to chmod +x it."
    exit 1
fi

# Opera enviroment
if test "${OPERA_DIR}" = '' ; then
  if test -d /usr/local/share/linux-opera ; then
    OPERA_DIR=/usr/local/share/linux-opera
  else
    echo "OPERA_DIR unset and not found at expected location (/usr/local/share/linux-opera)"
    exit 1
  fi
fi

OPERA_LD_PRELOAD="${LD_PRELOAD}"
export OPERA_LD_PRELOAD

# ~/.linux-opera
OPERA_PERSONALDIR=${HOME}/.linux-opera
export OPERA_PERSONALDIR

# Native Java enviroment
for d in "$OPERA_PERSONALDIR" "$HOME/.linux-opera"
do if test -f "$d/javapath.txt"
   then
      INIJAVA=`cat "$d/javapath.txt"`
      test -f "$INIJAVA/libjava.so" || continue
      OPERA_JAVA_DIR="$INIJAVA"
      break
   fi
done

if test ! "$OPERA_JAVA_DIR"
then for j in lib jre/lib
     do d="$JAVA_HOME/$j/i386"
        test -f "$d/libjava.so" || continue
	OPERA_JAVA_DIR="$d"
	break
     done
fi

if test ! "${OPERA_JAVA_DIR}"
then
    PREFIXES="/usr/local"

    for SUNJAVA in \
	linux-blackdown-jdk1.3.1/jre \
	linux-blackdown-jdk1.4.1/jre \
	linux-blackdown-jdk1.4.2/jre \
	linux-sun-jdk1.3.1/jre \
	linux-sun-jdk1.4.1/jre \
	linux-sun-jdk1.4.2/jre \
	linux-sun-jdk1.5.0/jre \
	linux-sun-jdk1.6.0/jre \
	; do
	for PREFIX in ${PREFIXES}
	do d="$PREFIX/$SUNJAVA/lib/i386"
	   test -f "$d/libjava.so" || continue
	   OPERA_JAVA_DIR="$d"
	   break
	done
	if test "${OPERA_JAVA_DIR}"; then break; fi
    done
    if test ! "${OPERA_JAVA_DIR}"; then
	for IBMJAVA in \
	    linux-ibm-jdk1.3.1/jre \
	    linux-ibm-jdk1.4.0/jre \
	    linux-ibm-jdk1.4.1/jre \
	    ; do
	    for PREFIX in ${PREFIXES}; do
		if test -f "${PREFIX}/${IBMJAVA}/bin/libjava.so"; then OPERA_JAVA_DIR="${PREFIX}/${IBMJAVA}/bin" && break; fi
	    done
	if test "${OPERA_JAVA_DIR}"; then break; fi
	done
    fi
fi

if test "${OPERA_JAVA_DIR}"; then
    LD_LIBRARY_PATH="${OPERA_JAVA_DIR}:${OPERA_JAVA_DIR}/native_threads:${OPERA_JAVA_DIR}/client:${OPERA_JAVA_DIR}/classic${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH}"
    export LD_LIBRARY_PATH OPERA_JAVA_DIR
fi

# Workarounds for the "preloaded libXt" problem.
LD_PRELOAD="libjvm.so:libawt.so:${OPERA_LD_PRELOAD}"
export LD_PRELOAD
# To disable the first workaround, comment the next line.
JAVA_WORKAROUND=`${OPERA_BINARYDIR}/works 2>/dev/null`

if test "${JAVA_WORKAROUND}" = 'works'; then
    OPERA_FORCE_JAVA_ENABLED="1"
else
    LD_PRELOAD="libjava.so:${LD_PRELOAD}"
    # To disable the second workaround, comment the next line.
    IBMJAVA131_WORKAROUND=`${OPERA_BINARYDIR}/works 2>/dev/null`
    if test "${IBMJAVA131_WORKAROUND}" = 'works'; then
	OPERA_FORCE_JAVA_ENABLED="1"
    else
	LD_PRELOAD="${OPERA_LD_PRELOAD}"
    fi
fi
export LD_PRELOAD OPERA_FORCE_JAVA_ENABLED

# Acrobat Reader
for BINDIR in \
    /usr/local/Acrobat5/bin \
    ; do
    if test -d ${BINDIR} ; then PATH=${PATH}:${BINDIR}; fi
done

# Exporting the enviroment
export OPERA_DIR PATH

LD_LIBRARY_PATH="${OPERA_BINARYDIR}${LD_LIBRARY_PATH:+:}${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH

# Spellchecker needs to find libaspell.so.15
for LIBASPELL_DIR in \
    /compat/linux/usr/lib \
; do
    if ls $LIBASPELL_DIR/libaspell.so.1[5-9] >/dev/null 2>&1
    then LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${LIBASPELL_DIR}"
    fi
done

# Running Opera
exec "${OPERA_BINARYDIR}opera" "$@"

