#!/bin/sh
#
# Author: Aaron Voisine <aaron@voisine.org>
# Dia Modifications:
#	Michael Wybrow <mjwybrow@users.sourceforge.net>
#	Jean-Olivier Irisson <jo.irisson@gmail.com>
#

CWD="`(cd \"\`dirname \\\"$0\\\"\`\"; echo \"$PWD\")`"
# e.g. /Applications/Dia.app/Contents/Resources/bin
TOP="`dirname \"$CWD\"`"
# e.g. /Applications/Dia.app/Contents/Resources


# Brutally add many things to the PATH. If the directories do not exist, they won't be used anyway. 
# People should really use ~/.macosx/environment.plist to set environment variables as explained by Apple:
#	http://developer.apple.com/qa/qa2001/qa1067.html
# but since no one does, we correct this by making the 'classic' PATH additions here:
#	/usr/local/bin which, though standard, doesn't seem to be in the PATH
# 	newer python as recommended by MacPython http://www.python.org/download/mac/
#	Fink
#	MacPorts (former DarwinPorts)
#	LaTeX distribution for Mac OS X
#export PATH="/usr/texbin:/opt/local/bin:/sw/bin/:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/local/bin:$CWD:$PATH"

# Setup PYTHONPATH to use python modules shipped with Dia
ARCH=`arch`
PYTHON_VERS=`python -V 2>&1 | cut -c 8-10`
export PYTHONPATH="$TOP/python/site-packages/$ARCH/$PYTHON_VERS"
# NB: we are only preprending some stuff to the default python path so if the directory does not exist it should not harm the rest

# No longer required if path rewriting has been conducted.
export DYLD_FALLBACK_LIBRARY_PATH="$TOP/lib"

export DIA_BASE_PATH="$TOP"
export DIA_LIB_PATH="$TOP/dia"
export DIA_SHEET_PATH="$TOP/sheets"
export DIA_SHAPE_PATH="$TOP/shapes"
export DIA_XSLT_PATH="$TOP/xslt"
export DIA_LOCALE_PATH="$TOP/locale"

mkdir -p "${HOME}/.dia-etc"

export FONTCONFIG_PATH="$TOP/etc/fonts"
export PANGO_RC_FILE="$HOME/.dia-etc/pangorc"
export GTK_IM_MODULE_FILE="$HOME/.dia-etc/gtk.immodules"
export GDK_PIXBUF_MODULE_FILE="$HOME/.dia-etc/gdk-pixbuf.loaders"
export GTK_DATA_PREFIX="$TOP"
export GTK_EXE_PREFIX="$TOP"
export XDG_DATA_DIRS="$TOP/share"

# Handle the case where the directory storing Dia has special characters
# ('#', '&', '|') in the name.  These need to be escaped to work properly for 
# various configuration files.
ESCAPEDTOP=`echo "$TOP" | sed 's/#/\\\\\\\\#/' | sed 's/&/\\\\\\&/g' | sed 's/|/\\\\\\|/g'`

# If the AppleCollationOrder preference doesn't exist, we fall back to using
# the AppleLocale preference.
LANGSTR=`defaults read .GlobalPreferences AppleCollationOrder 2>/dev/null`
if [ "x$LANGSTR" == "x" ]
then
    echo "Warning: AppleCollationOrder setting not found, using AppleLocale." 1>&2
    LANGSTR=`defaults read .GlobalPreferences AppleLocale 2>/dev/null | \
            sed 's/_.*//'`
fi

# NOTE: Have to add ".UTF-8" to the LANG since omitting causes Dia
#       to crash on startup in locale_from_utf8().
export LANG="$LANGSTR.UTF8"
echo "Setting Language: $LANG" 1>&2

sed 's|${HOME}|'"$HOME|g" "$TOP/etc/pango/pangorc" > "${HOME}/.dia-etc/pangorc"
sed 's|${CWD}|'"$ESCAPEDTOP|g" "$TOP/etc/pango/pango.modules" \
    > "${HOME}/.dia-etc/pango.modules"
cp -f "$TOP/etc/pango/pangox.aliases" "${HOME}/.dia-etc/"
sed 's|${CWD}|'"$ESCAPEDTOP|g" "$TOP/etc/gtk-2.0/gtk.immodules" \
    > "${HOME}/.dia-etc/gtk.immodules"
sed 's|${CWD}|'"$ESCAPEDTOP|g" "$TOP/etc/gtk-2.0/gdk-pixbuf.loaders" \
    > "${HOME}/.dia-etc/gdk-pixbuf.loaders"

exec "$CWD/dia-bin" --integrated
