#!/bin/sh
#
# Author: Aaron Voisine <aaron@voisine.org>
# Portable Author: Carlo Gandolfi <http://www.freesmug.org>

CWD="`(cd \"\`dirname \\\"$0\\\"\`\"; echo $PWD)`"
TOP="`dirname \"$CWD\"`"
ETC="/tmp/$UID/TemporaryItems"

export "DYLD_LIBRARY_PATH=$TOP/lib"
export "PATH=$CWD:$PATH"
export "PANGO_RC_FILE=$ETC/pangorc"
export "FONTCONFIG_PATH=$TOP/etc/fonts"
export "GTK_IM_MODULE_FILE=$ETC/gtk.immodules"
export "GDK_PIXBUF_MODULE_FILE=$ETC/gdk-pixbuf.loaders"
export "GTK_DATA_PREFIX=$TOP"
export "GTK_EXE_PREFIX=$TOP"
export "GIMP2_SYSCONFDIR=$TOP/etc/gimp/2.0"
export "GIMP2_DATADIR=$TOP/share/gimp/2.0"
export "GIMP2_PLUGINDIR=$TOP/lib/gimp/2.0"
export "GIMP2_LOCALEDIR=$TOP/share/locale"
export "GIMP2_DIRECTORY=$TOP/gimp-2.2" # Portable Gimp.app
export "GS_LIB=$TOP/share/ghostscript/8.51/lib:$TOP/share/ghostscript/fonts"
export LANG=`grep '\b'\`defaults read -g AppleLanguages | sed 's/.\(..\).*/\1/'\
\`_ /usr/share/locale/locale.alias | sed -n 's/.*\(.._..\)\..*/\1/p;1q'`
#export LANG=`defaults read -g AppleLanguages | sed 's/.\(..\).*/\1/'`

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

exec "$CWD/gimp-2.2" "$@"