#!/bin/bash

# Uncomment the following line if you're having trouble with gnome-keyring lockups.
# This will cause passwords to be stored only temporarily for the session.
#WB_NO_GNOME_KEYRING=1

# Looks like WB expects gnome-keyring to be present as soon as XDG_RUNTIME_DIR
# environment variable is set. Do an extra check here...
if ! type gnome-keyring-daemon >/dev/null 2>/dev/null; then
	export WB_NO_GNOME_KEYRING=1
fi

# force disable the Mac style single menu hack in Ubuntu Unity
export UBUNTU_MENUPROXY=0

# another Ubuntu bug, this this one causes modal dialogs to not work as intended
# https://bugs.launchpad.net/ubuntu/+source/overlay-scrollbar/+bug/903302
export LIBOVERLAY_SCROLLBAR=0

# Set the destdir=<some_dir> when ever you install using DESTDIR=<some_dir>. 
destdir="$WB_DEST_DIR"

wblibdir="$destdir/usr/lib/mysql-workbench"


# Set the PROJSO env variable so gdal can find proj cause it's using dlopen instead ld 
if test -f "/usr/lib/libproj.so.0"; then
  export PROJSO="/usr/lib/libproj.so.0"
elif test -f "/usr/lib64/libproj.so.0"; then
  export PROJSO="/usr/lib64/libproj.so.0"
fi

if test -f $wblibdir/libsqlite3.so; then
  if test "$LD_PRELOAD" != ""; then
    export LD_PRELOAD="$LD_PRELOAD:$wblibdir/libsqlite3.so"
  else
    export LD_PRELOAD="$wblibdir/libsqlite3.so"
  fi
fi


# if libcairo and pixman are in the wb libraries dir, force them to be preloaded
if test -f $wblibdir/libcairo.so.2; then
	if test "$LD_PRELOAD" != ""; then
		export LD_PRELOAD="$LD_PRELOAD:$wblibdir/libcairo.so.2"
	else
		export LD_PRELOAD="$wblibdir/libcairo.so.2"
	fi
fi
if test -f $wblibdir/libpixman-1.so.0; then
	if test "$LD_PRELOAD" != ""; then
		export LD_PRELOAD="$LD_PRELOAD:$wblibdir/libpixman.so.0"
	else
		export LD_PRELOAD="$wblibdir/libpixman.so.0"
	fi
fi


if test "$LD_LIBRARY_PATH" != ""; then
    export LD_LIBRARY_PATH="$wblibdir:$LD_LIBRARY_PATH"
else
    export LD_LIBRARY_PATH="$wblibdir"
fi

if test "$PYTHONPATH" != ""; then
    export PYTHONPATH="$wblibdir:$PYTHONPATH"
else
    export PYTHONPATH="$wblibdir"
fi

export MWB_DATA_DIR="$destdir/usr/share/mysql-workbench"
export MWB_LIBRARY_DIR="$destdir/usr/share/mysql-workbench/libraries"
export MWB_MODULE_DIR="$destdir/usr/lib/mysql-workbench/modules"
export MWB_PLUGIN_DIR="$destdir/usr/lib/mysql-workbench/plugins"
export DBC_DRIVER_PATH="$destdir/usr/lib/mysql-workbench"
export MWB_BASE_DIR="$destdir/usr"
export MWB_BINARIES_DIR="$destdir/usr/bin"

if test "$WB_DEBUG" != ""; then
  $WB_DEBUG $MWB_BINARIES_DIR/mysql-workbench-bin "$@"
else
  if type -p catchsegv > /dev/null; then
  catchsegv $MWB_BINARIES_DIR/mysql-workbench-bin "$@"
  else
  $MWB_BINARIES_DIR/mysql-workbench-bin "$@"
  fi
fi
