#!/bin/sh

# Use this if you don't want to install Opera to any special location,
# but execute it from the extracted package or current location.

# Location of the Opera binary
OPERA=./bin/opera

if test ! -f "${OPERA}"; then
    echo "The Opera binary is not located at \"${OPERA}\"."
    exit 1
elif test ! -x "${OPERA}"; then
    echo "You do not have execute rights on \"${OPERA}\"."
    echo "Please do a \"chmod u+x ${OPERA}\"."
    exit 1
fi

# Opera enviroment
if test "${OPERA_DIR}" = ''; then
    OPERA_DIR="${PWD}"
fi

# 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

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

    for SUNJAVA in \
	j2re1.4.1_01 \
	j2re1.4.1 \
	j2re1.4.0_01 \
	j2sdk1.4.0_01/jre \
	j2re1.4.0 \
	jre1.4.0 \
	j2se/jre \
	jre1.3.1_02 \
	jre1.3.1_01 \
	j2re1.3.1 \
	jre1.3.1 \
	j2re1.3 \
	; 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-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}"

    # Workaround for the "preloaded libXt" problem.
    # Comment the next line to disable the workaround.
    JAVA_WORKAROUND=$(LD_PRELOAD="${OPERA_JAVA_DIR}/libawt.so" /bin/echo 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

    if test ! -f "${OPERA_DIR}/java/opera.policy"; then
	if test -w "${OPERA_DIR}/java/"; then
	    policy_contain="
// Standard extensions get all permissions by default

grant codeBase \"file:\${java.home}/lib/ext/*\" {
	permission java.security.AllPermission;
};

// Opera package classes get all permissions
grant codebase \"file://${OPERA_DIR}/java//opera.jar\" {
	permission java.security.AllPermission;
};

grant {
	permission java.lang.RuntimePermission \"accessClassInPackage.sun.*\";
};

// default permissions granted to all domains

grant { 
	// Allows any thread to stop itself using the java.lang.Thread.stop()
	// method that takes no argument.
	// Note that this permission is granted by default only to remain
	// backwards compatible.
	// It is strongly recommended that you either remove this permission
	// from this policy file or further restrict it to code sources
	// that you specify, because Thread.stop() is potentially unsafe.
	// See \"http://java.sun.com/notes\" for more information.
	permission java.lang.RuntimePermission \"stopThread\";

	// allows anyone to listen on un-privileged ports
	permission java.net.SocketPermission \"localhost:1024-\", \"listen\";

	// \"standard\" properies that can be read by anyone

	permission java.util.PropertyPermission \"java.version\", \"read\";
	permission java.util.PropertyPermission \"java.vendor\", \"read\";
	permission java.util.PropertyPermission \"java.vendor.url\", \"read\";
	permission java.util.PropertyPermission \"java.class.version\", \"read\";
	permission java.util.PropertyPermission \"os.name\", \"read\";
	permission java.util.PropertyPermission \"os.version\", \"read\";
	permission java.util.PropertyPermission \"os.arch\", \"read\";
	permission java.util.PropertyPermission \"file.separator\", \"read\";
	permission java.util.PropertyPermission \"path.separator\", \"read\";
	permission java.util.PropertyPermission \"line.separator\", \"read\";

	permission java.util.PropertyPermission \"java.specification.version\", \"read\";
	permission java.util.PropertyPermission \"java.specification.vendor\", \"read\";
	permission java.util.PropertyPermission \"java.specification.name\", \"read\";

	permission java.util.PropertyPermission \"java.vm.specification.version\", \"read\";
	permission java.util.PropertyPermission \"java.vm.specification.vendor\", \"read\";
	permission java.util.PropertyPermission \"java.vm.specification.name\", \"read\";
	permission java.util.PropertyPermission \"java.vm.version\", \"read\";
	permission java.util.PropertyPermission \"java.vm.vendor\", \"read\";
	permission java.util.PropertyPermission \"java.vm.name\", \"read\";
};
"
	    echo "${policy_contain}" > "${OPERA_DIR}/java/opera.policy"
	    _CLEAN_OPERA_POLICY_='YES'
	fi
    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 \
    "${PWD}/plugins" \
    "${HOME}/.opera/plugins" \
    /usr/lib/opera/plugins \
    /usr/local/Acrobat4/Browsers/intellinux \
    /usr/java/jre1.3.1/plugin/i386/ns4 \
    /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

# Acrobat Reader
if test -d /usr/local/Acrobat4/bin ; then 
    PATH="${PATH}:/usr/local/Acrobat4/bin"
fi

OPERA_PLUGIN_PATH="${OPERA_PLUGIN_PATH}":"${NPX_PLUGIN_PATH}"

# Setting the enviroment
LD_LIBRARY_PATH="${PWD}/lib:${LD_LIBRARY_PATH}"
export OPERA_DIR OPERA_PLUGIN_PATH LD_LIBRARY_PATH

# Running Opera
${OPERA} "${@}"

# Cleanup
if test "${_CLEAN_OPERA_POLICY_}" = 'YES'; then
    if test -w "${OPERA_DIR}/java/opera.policy"; then
	rm "${OPERA_DIR}/java/opera.policy"
    fi
fi
