#!/bin/sh
if test "x$GTK_DEBUG_LAUNCHER" != x; then
    set -x
fi

if test "x$GTK_DEBUG_GDB" != x; then
    EXEC="gdb --args"
else
    EXEC=exec
fi

SOURCE="${BASH_SOURCE[0]}"
while [ -h "${SOURCE}" ]
    do # resolve $SOURCE until the file is no longer a symlink
        DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )"
        SOURCE="$(readlink "${SOURCE}")"
        [[ "${SOURCE}" != /* ]] && SOURCE="${DIR}/${SOURCE}" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
    done
CURRDIR="$( cd -P "$( dirname "$SOURCE" )" && cd ../.. && pwd )"

name=`basename "$0"`
bundle="${CURRDIR}"
bundle_contents="${bundle}/Contents"
bundle_macos="${bundle}/Contents/MacOS"
bundle_res="${bundle_contents}/Resources"
bundle_lib="${bundle_res}/lib"
bundle_bin="${bundle_res}/bin"
bundle_data="${bundle_res}/share"
bundle_etc="${bundle_res}/etc"

export DYLD_FALLBACK_LIBRARY_PATH="${bundle_lib}"

if [ ! -d "${HOME}/Library/Preferences/org.deluge-2.0" ]; then
	mkdir -p "${HOME}/Library/Preferences/org.deluge-2.0"
fi

if [ ! -d "${HOME}/Library/Logs/org.deluge-2.0" ]; then
	mkdir -p "${HOME}/Library/Logs/org.deluge-2.0"
fi

if [ -d "${HOME}/.config/deluge" ]; then
	echo "Found Deluge config data at '${HOME}/.config/deluge'..."
	if [ ! -d "${HOME}/Library/Preferences/org.deluge-2.0/gtk3ui_state" ]; then
		echo "Copying config data to '${HOME}/Library/Preferences/org.deluge-2.0'."
		rsync -r --exclude "deluged.pid" --exclude "deluged.log" --exclude "plugins" --exclude "gtkui*" "${HOME}/.config/deluge/" "${HOME}/Library/Preferences/org.deluge-2.0/"
	else
		echo "We'll just ignore it since there is Deluge 2.x config data already at '${HOME}/Library/Preferences/org.deluge-2.0'"
	fi
fi

export XDG_DOWNLOAD_DIR="${HOME}/Downloads"
export XDG_CONFIG_HOME="${HOME}/Library/Preferences/org.deluge-2.0"
export XDG_CONFIG_DIRS="${XDG_CONFIG_HOME}"
export XDG_DATA_DIRS="${bundle_data}"
export GTK_DATA_PREFIX="${bundle_res}"
export GTK_EXE_PREFIX="${bundle_res}"
export GTK_PATH="${bundle_res}"
export GI_TYPELIB_PATH="${bundle_lib}/girepository-1.0"

export DELUGE_IS_RUNNING_BUNDLE=1
export DELUGE_BUNDLE_I18N="${bundle_data}/locale"

if [ "$(defaults read -g AppleInterfaceStyle 2>/dev/null)" ]; then
    GTK_THEME="McOS-CTLina-Dark"
else
    GTK_THEME="McOS-CTLina"
fi
export GTK_THEME

#Set $PYTHON to point inside the bundle
export PYTHON="${bundle_macos}/python"
export PYTHONHOME="${bundle_res}"
#Add the bundle's python modules
export PYTHONPATH="${bundle_lib}/python/zope:${bundle_lib}/python:${bundle_lib}:${bundle_lib}/python/lib-dynload:${PYTHONPATH}"

# Ensure deluged is available by adding macos dir to path.
export PATH="${bundle_macos}":$PATH

export GDK_PIXBUF_MODULEDIR="${bundle_lib}/gdk-pixbuf-2.0/2.10.0/loaders/"
export GDK_PIXBUF_MODULE_FILE="${bundle_lib}/gdk-pixbuf-2.0/2.10.0/loaders.cache"
if [ `uname -r | cut -d . -f 1` -ge 10 ]; then
    export GTK_IM_MODULE_FILE="${bundle_lib}/gtk-3.0/3.0.0/immodules.cache"
fi

# We need a UTF-8 locale.
lang=`defaults read .GlobalPreferences AppleLocale 2>/dev/null`
if test "$?" != "0"; then
    lang=`defaults read .GlobalPreferences AppleCollationOrder 2>/dev/null | sed 's/_.*//'`
fi
LANG=""
if test "$lang" != ""; then
    LANG="`grep \"\`echo $lang\`_\" /usr/share/locale/locale.alias | \
           tail -n1 | sed 's/\./ /' | awk '{print $2}'`"
fi
if test "$LANG" == ""; then
    export LANG="C"
else
    export LANG="$LANG.utf8"
fi

if test -f "$bundle_lib/charset.alias"; then
    export CHARSETALIASDIR="$bundle_lib"
fi

export LC_ALL=C
export LANG=$(echo $(defaults read -g AppleLanguages | sed '/"/!d;s/["[:space:]]//g;s/-/_/') | cut -d',' -f1).UTF-8

# Extra arguments can be added in environment.sh.
EXTRA_ARGS=
if test -f "${bundle_res}/environment.sh"; then
  source "${bundle_res}/environment.sh"
fi

# Strip out the argument added by the OS.
if /bin/expr "x${1}" : '^x-psn_' > /dev/null; then
    shift 1
fi
LOGTIMESTAMP=$(date +'%Y-%m-%d_%H-%M-%S')
${EXEC} "${PYTHON}" "${bundle_macos}"/${name}-bin --config="${XDG_CONFIG_HOME}" --logfile="${HOME}/Library/Logs/org.deluge-2.0/${name}-${LOGTIMESTAMP}.log" --loglevel info "$@" ${EXTRA_ARGS}