#!/bin/sh
#
# Author: Aaron Voisine <aaron@voisine.org>
# Inkscape Modifications: Michael Wybrow <mjwybrow@users.sourceforge.net>

CWD="`(cd \"\`dirname \\\"$0\\\"\`\"; echo $PWD)`"
TOP="`dirname \"$CWD\"`"

export "DYLD_LIBRARY_PATH=$TOP/lib"
export "PATH=$CWD:$PATH"
export "PANGO_RC_FILE=$HOME/.inkscape-etc/pangorc"
export "FONTCONFIG_PATH=$TOP/etc/fonts"
export "GTK_IM_MODULE_FILE=$HOME/.inkscape-etc/gtk.immodules"
export "GDK_PIXBUF_MODULE_FILE=$HOME/.inkscape-etc/gdk-pixbuf.loaders"
export "GTK_DATA_PREFIX=$TOP"
export "GTK_EXE_PREFIX=$TOP"
export "GNOME_VFS_MODULE_CONFIG_PATH=$TOP/etc/gnome-vfs-2.0/modules"
export "GNOME_VFS_MODULE_PATH=$TOP/lib/gnome-vfs-2.0/modules"

export "INKSCAPE_SHAREDIR=$TOP"
export "INKSCAPE_PLUGINDIR=$TOP/lib/inkscape"
export "INKSCAPE_LOCALEDIR=$TOP/locale"

# TODO: Have to add ".UTF-8" to the LANG since ommiting causes Inkscape
#       to crash on startup in locale_from_utf8().
export "LANG=`grep \"\`defaults read .GlobalPreferences AppleCollationOrder \
	2>&1\`_\" /usr/share/locale/locale.alias | tail -n1 | sed 's/\./ /' | \
	awk '{print $2}'`.UTF-8"

# Handle the case where the directory storing Inkscape has a '#' in the name.
# This '#' needs to be escaped in pango.modules for Pango to work properly.
ESCAPEDTOP=`echo $TOP | sed 's/#/\\\\\\\\#/'`

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

exec "$CWD/inkscape-bin" "$@"
