#!/bin/sh

# Location of the Opera binaries
OPERA_BINARYDIR=/mit/opera/lib/opera/7.22-20031103.1

# Parse commandline parameters
passflags=''
while test "$#" -gt 0; do
    case $1 in
	--binarydir*|-binarydir*)
	    OPERA_BINARYDIR="$2"
	    shift
	    ;;
	*)
	    passflags="${passflags} '${1}'"
	    ;;
    esac
    shift
done

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 /mit/opera/share/opera ; then 
    OPERA_DIR=/mit/opera/share/opera
  else
    echo "OPERA_DIR unset and not found at expected location (/mit/opera/share/opera)"
    exit 1
  fi
fi

OPERA_LD_PRELOAD="${LD_PRELOAD}"
export OPERA_LD_PRELOAD

# Native Java enviroment
if test -f "${HOME}/.opera/javapath.txt"; then
    INIJAVA=`cat ${HOME}/.opera/javapath.txt`
    if test -f "${INIJAVA}/libjava.so"; then OPERA_JAVA_DIR="${INIJAVA}"; fi
fi

if test ! "${OPERA_JAVA_DIR}"; then
    if test -f "${JAVA_HOME}/lib/i386/libjava.so"; then OPERA_JAVA_DIR="${JAVA_HOME}/lib/i386"; fi
fi

if test ! "${OPERA_JAVA_DIR}"; then
    if test -f "${JAVA_HOME}/jre/lib/i386/libjava.so"; then OPERA_JAVA_DIR="${JAVA_HOME}/jre/lib/i386"; fi
fi

if test ! "${OPERA_JAVA_DIR}"; then

    PREFIXES="
	/usr
	/usr/java
	/usr/lib
	/usr/local
	/opt"

    for SUNJAVA in \
	j2re1.4.2_01 \
	j2sdk1.4.2_01/jre \
	j2re1.4.2 \
	j2sdk1.4.2/jre \
	j2re1.4.1_01 \
	j2re1.4.1 \
	SUNJava2-1.4.1 \
	BlackdownJava2-1.4.1/jre \
	j2re1.4.0_01 \
	j2sdk1.4.0_01/jre \
	j2re1.4.0 \
	jre1.4.0 \
	j2se/1.4/jre \
	j2se/1.3/jre \
	j2se/jre \
	jre1.3.1_02 \
	jre1.3.1_01 \
	j2re1.3.1 \
	jre1.3.1 \
	j2re1.3 \
	j2se/1.3/jre \
	java2re \
	jdk1.2.2/jre \
	jdk1.2/jre \
	jre \
	; do
	for PREFIX in ${PREFIXES}; do
	    if test -f "${PREFIX}/${SUNJAVA}/lib/i386/libjava.so"; then OPERA_JAVA_DIR="${PREFIX}/${SUNJAVA}/lib/i386" && break; fi
	done
	if test "${OPERA_JAVA_DIR}"; then break; fi
    done
    if test ! "${OPERA_JAVA_DIR}"; then
	for IBMJAVA in \
	    IBMJava2-141/jre \
	    IBMJava2-14/jre \
	    IBMJava2-131/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}"
    export LD_LIBRARY_PATH

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

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

# Opera Plug-in enviroment, Add more plugin search paths here
# If OPERA_PLUGIN_PATH is set NPX_PLUGIN_PATH will be ignored

for DIR in \
    "${HOME}/.opera/plugins" \
    /mit/opera/lib/opera/plugins \
    /usr/lib/opera/plugins \
    /usr/local/lib/opera/plugins \
    /opt/lib/opera/plugins \
    /usr/local/Acrobat[45]/Browsers/intellinux \
    /usr/lib/Acrobat[45]/Browsers/intellinux \
    /usr/X11R6/lib/Acrobat[45]/Browsers/intellinux \
    /opt/Acrobat[45]/Browsers/intellinux \
    /usr/Acrobat[45]/Browsers/intellinux \
    /usr/lib/RealPlayer8/Plugins \
    /usr/lib/realplay/plugins \
    /usr/lib/RealPlayer8 \
    /usr/lib/realplay \
    "${HOME}/.netscape/plugins" \
    /opt/netscape/plugins \
    /usr/lib/netscape/plugins \
    /usr/local/netscape/plugins \
    /usr/local/lib/netscape/plugins \
    ; do
    if test -d ${DIR} ; then OPERA_PLUGIN_PATH="${OPERA_PLUGIN_PATH}":"${DIR}"; fi
done

# Opera does not support Mozilla plugins, only Opera and Netscape plugins.
# Since Mozilla supports NS plugins, there might be some in the Mozilla plugin
# directory. Perhaps add below paths to list above
# /usr/X11R6/lib/mozilla/plugins


# Acrobat Reader
for BINDIR in \
    /usr/local/Acrobat[45]/bin \
    /usr/lib/Acrobat[45]/bin \
    /usr/X11R6/lib/Acrobat[45]/bin \
    /opt/Acrobat[45]/bin \
    /usr/Acrobat[45]/bin \
    ; do
    if test -d ${BINDIR} ; then PATH=${PATH}:${BINDIR}; fi
done

OPERA_PLUGIN_PATH="${OPERA_PLUGIN_PATH}":"${NPX_PLUGIN_PATH}"
 
# Exporting the enviroment
export OPERA_DIR OPERA_PLUGIN_PATH PATH

# Running Opera
eval exec "'${OPERA_BINARYDIR}/opera' --binarydir '${OPERA_BINARYDIR}' $passflags"

