#!/bin/bash

APP_ROOT="$PWD/$(dirname "$0")"
for ((i=0; i<2; i++)); do APP_ROOT="$(dirname "$APP_ROOT")"; done
APP_ROOT="${APP_ROOT/\/\/}"

# Bundle directories
CONTENTS_DIR="$APP_ROOT/Contents"
RES_DIR="$CONTENTS_DIR/Resources"
ETC_DIR="$RES_DIR/etc"
LIB_DIR="$RES_DIR/lib"
BIN_DIR="$RES_DIR/bin"
DATA_DIR="$RES_DIR/share"

# Shell environment
export PATH="/bin:/usr/bin:$BIN_DIR"
export BUNDLE_BIN_DIR="$BIN_DIR"
export BUNDLE_LIB_DIR="$LIB_DIR"

# XDG environment
export XDG_CONFIG_DIRS="$ETC_DIR/xdg"
export XDG_DATA_DIRS="$DATA_DIR"
export XDG_CONFIG_HOME="$HOME/.config"

# GTK environment
export GTK_PATH="$RES_DIR"
export GTK_EXE_PREFIX="$RES_DIR"
export GTK_DATA_PREFIX="$RES_DIR"
export GTK2_RC_FILES="$ETC_DIR/gtk-2.0/gtkrc"
export GTK_IM_MODULE_FILE="$ETC_DIR/gtk-2.0/gtk.immodules"
export GDK_PIXBUF_MODULE_FILE="$ETC_DIR/gtk-2.0/gdk-pixbuf.loaders"
export PANGO_RC_FILE="$ETC_DIR/pango/pangorc"

# Banshee/Mono/GStreamer environment
export DYLD_LIBRARY_PATH="$LIB_DIR:$LIB_DIR/banshee-1:$LIB_DIR/banshee-1/Extensions"
export GST_PLUGIN_SYSTEM_PATH="$LIB_DIR/gstreamer-0.10"
export BANSHEE_CONFIG_DIR="$XDG_CONFIG_HOME/banshee-1"
BANSHEE_OPTIONS=""
BANSHEE_EXEC_NAME=Banshee
MONO_BIN="$BIN_DIR/mono"
MONO_EXE="$LIB_DIR/banshee-1/Nereid.exe"
MONO_OPTIONS=""

# We are testing the SGen compacting GC
export MONO_ENV_OPTIONS="--gc=sgen"

for arg in $*; do
	if [ "$arg" = --debug ]; then
		MONO_OPTIONS="$MONO_OPTIONS --debug"
		BANSHEE_OPTIONS="$BANSHEE_OPTIONS --debug"
	fi
done

# FIXME
# pango.modules sucks - it doesn't like paths with spaces,
# so even though everything else works fine, we still
# will explode, so instead I'm just showing this lame dialog.
if [[ "$APP_ROOT" =~ [[:space:]] ]]; then
	osascript &>/dev/null <<EOF
	tell application "System Events"
		activate
		display dialog "A space was detected in the path to Banshee. Currently this is unsupported.\n\nRename or move Banshee.app to a path without spaces.\n\nPath: $APP_ROOT" with title "Error" with icon caution buttons { "OK" } default button 1
	end tell
EOF
	exit 1
fi

mkdir -p "$BANSHEE_CONFIG_DIR"
if [ -f "$RES_DIR/first-run" ]; then
	echo "Appears to be first run of this application bundle."
	rm -f -- "$RES_DIR/first-run"
	PREVIOUS_APP_ROOT=""
else
	PREVIOUS_APP_ROOT="$(cat "$BANSHEE_CONFIG_DIR/app-bundle-location" 2>/dev/null)"
fi

if [ ! "$APP_ROOT" = "$PREVIOUS_APP_ROOT" ]; then
	echo "Application bundle has moved. Adjusting bundle..."

	# Mono Addins doesn't like being relocated - its cache contains
	# references to the previous location of this bundle
	find "$BANSHEE_CONFIG_DIR" -name addin-db-\* \
		-exec rm -rf -- {} \; &>/dev/null

	# Adjust config files that need absolute paths to things
	# in the bundle (e.g. pango, GDK pixbuf loaders...)
	IFS=$'\n'
	for file in $(find "$ETC_DIR" -name \*.in); do
		sed "s|\${APP_RESOURCES}|${RES_DIR}|g" \
			< "${file}" \
			> "${file%.in}"
	done
	unset IFS

	# Store the new location of the bundle
	echo "$APP_ROOT" > "$BANSHEE_CONFIG_DIR/app-bundle-location"
fi

exec -a $BANSHEE_EXEC_NAME "$MONO_BIN" $MONO_OPTIONS "$MONO_EXE" $BANSHEE_OPTIONS
