#!/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 binaries
OPERA_BINARYDIR=usr/lib/opera

die () {
    for arg
    do echo "$arg"
    done >&2
    exit 1
}

# Parse commandline parameters
toset=
_PERSONALDIR_FROM_CMDLINE_=NO
_QTSTYLE_FROM_CMDLINE_=NO

for arg
do
  if [ "$toset" ]
  then
      eval $toset='$arg'
      export $toset
      toset=
  else
      case $arg in
          --binarydir*|-binarydir*) toset=OPERA_BINARYDIR ;;
          --personaldir*|-personaldir*|--pd*|-pd*)
              _PERSONALDIR_FROM_CMDLINE_=YES
              toset=OPERA_PERSONALDIR
              ;;
          --display|-display) toset=DISPLAY ;;
          --style|-style) _QTSTYLE_FROM_CMDLINE_=YES ;;
      esac
  fi
done
[ -z "$toset" ] || die "Missing parameter for $arg"


[ "$PWD" ] || PWD=`/bin/pwd`
if [ "$_PERSONALDIR_FROM_CMDLINE_" = NO ]
then
  OPERA_PERSONALDIR="$PWD/profile"
  export OPERA_PERSONALDIR
fi

# Required for Session Management
case $0 in /*) OPERA_SCRIPT_PATH=$0;; *) OPERA_SCRIPT_PATH=`/bin/pwd`/$0;; esac
export OPERA_SCRIPT_PATH

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

OPERA_SYSTEM_UNAME=`uname -s`
export OPERA_SYSTEM_UNAME

OPERA_BINARYDIR=`echo $OPERA_BINARYDIR | sed -e 's:/*/$::'`
export OPERA_BINARYDIR
[ -f "$OPERA_BINARYDIR/opera" ] || die "The Opera binary is not located at \"$OPERA_BINARYDIR/opera\"." "Please modify the wrapper script at \"$0\"."
[ -x "$OPERA_BINARYDIR/opera" ] || die "You do not have execute rights on \"$OPERA_BINARYDIR/opera\", please ask the sysadmin to chmod +x it."

# Opera enviroment
if [ -z "$OPERA_DIR" ]
then
    OPERA_DIR="$PWD/usr/local/share/opera"
fi
export OPERA_DIR

# Java tramples this - using %D ensures the default stays in (X11R6 and later):
if [ -z "$XFILESEARCHPATH" ]
then
    XFILESEARCHPATH='%D'
    export XFILESEARCHPATH
fi

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

# Exploit JRE_HOME, if set:
[ "$OPERA_JAVA_DIR" -o -z "$JRE_HOME" -o ! -d "$JRE_HOME" ] || \
for j in lib/i386 lib/amd64
do d="$JRE_HOME/$j"
   [ -f "$d/libjava.so" ] || continue
   OPERA_JAVA_DIR="$d"
   break
done

# Exploit JAVA_HOME, if set:
[ "$OPERA_JAVA_DIR" -o -z "$JAVA_HOME" -o ! -d "$JAVA_HOME" ] || \
for j in lib jre/lib
do d="$JAVA_HOME/$j/i386"
   [ -f "$d/libjava.so" ] || continue
   OPERA_JAVA_DIR="$d"
   break
done

if [ -z "$OPERA_JAVA_DIR" ]
then
    PREFIXES="/usr /usr/java /usr/lib64 /usr/lib64/jvm /usr/lib /usr/lib/jvm /usr/local /opt"

    for JAVA in \
	java-6-sun \
	java-6-sun/jre \
	java-6-sun-1.6.0.03 \
	java-6-sun-1.6.0.03/jre \
	java-6-sun-1.6.0.02 \
	java-6-sun-1.6.0.02/jre \
	java-6-sun-1.6.0.01 \
	java-6-sun-1.6.0.01/jre \
	java-6-sun-1.6.0.00 \
	java-6-sun-1.6.0.00/jre \
	java-1.5.0-sun \
	java-1.5.0-sun/jre \
	java-1.5.0-sun-1.5.0.09 \
	java-1.5.0-sun-1.5.0.09/jre \
	java-1.5.0-sun-1.5.0.08 \
	java-1.5.0-sun-1.5.0.08/jre \
	java-1.5.0-sun-1.5.0.07 \
	java-1.5.0-sun-1.5.0.07/jre \
	java-1.5.0-sun-1.5.0.06 \
	java-1.5.0-sun-1.5.0.06/jre \
	jre1.5.0_06 \
	jdk1.5.0_06/jre \
	java-1.5.0-sun-1.5.0.05 \
	java-1.5.0-sun-1.5.0.05/jre \
	jre1.5.0_05 \
	jdk1.5.0_05/jre \
	java-1.5.0-sun-1.5.0.04 \
	java-1.5.0-sun-1.5.0.04/jre \
	jre1.5.0_04 \
	jdk1.5.0_04/jre \
	jre1.5.0_03 \
	jdk1.5.0_03/jre \
	jre1.5.0_02 \
	jdk1.5.0_02/jre \
	jre1.5.0_01 \
	jdk1.5.0_01/jre \
	jdk1.5.0/jre \
	java-gcj \
	j2re1.4.2_09 \
	j2skd1.4.2_09/jre \
	j2re1.4.2_08 \
	j2skd1.4.2_08/jre \
	j2re1.4.2_07 \
	j2skd1.4.2_07/jre \
	j2re1.4.2_06 \
	j2skd1.4.2_06/jre \
	j2re1.4.2_04 \
	j2skd1.4.2_04/jre \
	j2re1.4.2_03 \
	j2skd1.4.2_03/jre \
	j2re1.4.2_02 \
	j2skd1.4.2_02/jre \
	j2re1.4.2_01 \
	j2skd1.4.2_01/jre \
	jdk1.4.2/jre \
	j2re1.4.2 \
	j2sdk1.4.2/jre \
	j2re1.4.1_01 \
	j2re1.4.1 \
	SUNJava2-1.4.1 \
	BlackdownJava2-1.4.1/jre \
	jdk1.4.1/jre \
	j2re1.4.0_01 \
	j2sdk1.4.0_01/jre \
	j2re1.4.0 \
	jre1.4.0 \
	jdk1.4.0/jre \
	j2se/1.4/jre \
	j2se/jre \
	jre1.3.1_15 \
	jre1.3.1_04 \
	jre1.3.1_02 \
	jre1.3.1_01 \
	jdk1.3.1/jre \
	j2re1.3.1 \
	jre1.3.1 \
	j2re1.3 \
	j2se/1.3/jre \
	SunJava2-1.3/jre \
	java2re \
	jdk1.2.2/jre \
	jdk1.2/jre \
	jre \
	java \
    ; do
	for PREFIX in $PREFIXES
	do d="$PREFIX/$JAVA/lib/i386"
	   [ -f "$d/libjava.so" ] || continue
	   OPERA_JAVA_DIR="$d"
	   break
	done
	if [ "$OPERA_JAVA_DIR" ]; then break; fi
    done

    if [ -z "$OPERA_JAVA_DIR" ]
    then
	for JAVA in \
	    IBMJava2-142/jre \
	    IBMJava2-141/jre \
	    IBMJava2-14/jre \
	    IBMJava2-131/jre \
	    IBMJava2-jre1.3.0/jre \
	; do for PREFIX in $PREFIXES
	     do if [ -f "$PREFIX/$JAVA/bin/libjava.so" ]
		then OPERA_JAVA_DIR="$PREFIX/$JAVA/bin"
		     break
		fi
	     done
	     if [ "$OPERA_JAVA_DIR" ]; then break; fi
	  done
    fi
fi

if [ "$OPERA_JAVA_DIR" ]
then
    LD_LIBRARY_PATH="$OPERA_JAVA_DIR:$OPERA_JAVA_DIR/native_threads:$OPERA_JAVA_DIR/client:$OPERA_JAVA_DIR/server:$OPERA_JAVA_DIR/classic${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH"
    export LD_LIBRARY_PATH OPERA_JAVA_DIR
fi

if [ -z "$AWT_TOOLKIT" ]
then # Prefer raw X over Motif, to avoid libXm version/location complications:
    AWT_TOOLKIT=XToolkit
    export AWT_TOOLKIT
fi

OPERA_LD_PRELOAD="$LD_PRELOAD"
export OPERA_LD_PRELOAD

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

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

if [ "$OPERA_JAVA_DIR" -o "$OPERA_FORCE_JAVA_ENABLED" = 1 ] && [ ! -f "$OPERA_DIR/java/opera.policy" ] && [ -w "$OPERA_DIR/java/" ]
then cat <<EOF > "$OPERA_DIR/java/opera.policy"
// 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;
};

// 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\";
};
EOF
     _CLEAN_OPERA_POLICY_=YES
fi

# Acrobat Reader
for ACROBATBINDIR in \
	/usr/local/Adobe/Reader8/bin \
	/usr/lib64/Adobe/Reader8/bin \
	/usr/lib/Adobe/Reader8/bin \
	/usr/X11R6/lib/Adobe/Reader8/bin \
	/opt/Adobe/Reader8/bin \
	/usr/Adobe/Reader8/bin \
	/usr/local/Adobe/Acrobat7.0/bin \
	/usr/lib64/Acrobat7.0/bin \
	/usr/lib/Acrobat7.0/bin \
	/usr/X11R6/lib/Acrobat7.0/bin \
	/opt/Acrobat7.0/bin \
	/usr/Acrobat7.0/bin \
	/usr/local/Acrobat7/bin \
	/usr/lib64/Acrobat7/bin \
	/usr/lib/Acrobat7/bin \
	/usr/X11R6/lib/Acrobat7/bin \
	/opt/Acrobat7/bin \
	/usr/Acrobat7/bin \
	/usr/local/Acrobat5/bin \
	/usr/lib64/Acrobat5/bin \
	/usr/lib/Acrobat5/bin \
	/usr/X11R6/lib/Acrobat5/bin \
	/opt/Acrobat5/bin \
	/usr/Acrobat5/bin \
	/opt/acroread-5.07/bin \
	/usr/local/Acrobat4/bin \
	/usr/lib64/Acrobat4/bin \
	/usr/lib/Acrobat4/bin \
	/usr/X11R6/lib/Acrobat4/bin \
	/opt/Acrobat4/bin \
	/usr/Acrobat4/bin \
    ; do
    if [ -d "$ACROBATBINDIR" ]
    then PATH="$PATH:$ACROBATBINDIR"
    fi
done

# Setting environment relative to current working directory
# Bundled Qt will be found here unless preloaded by the stance above
LD_LIBRARY_PATH="$PWD/usr/lib/opera${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH
# Make sure the compat libraries are found
[ -d /usr/local/lib/compat ] && LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib/compat"

# Run Opera:
"$OPERA_BINARYDIR/opera" "$@"

# Cleanup
if [ "$_CLEAN_OPERA_POLICY_" = YES ] && [ -w "$OPERA_DIR/java" ]
then rm "$OPERA_DIR/java/opera.policy"
fi
