#!/bin/sh

if test "x$DEBUG_LAUNCHER" != x; then
    set -x
fi

name=`basename "$0"`
tmp="$0"
tmp=`dirname "$tmp"`
tmp=`dirname "$tmp"`
bundle=`dirname "$tmp"`
bundle=/Applications/FontForge.app
scriptdir=/Applications/FontForge.app/Contents/MacOS/
bundle_contents="$bundle"/Contents
bundle_res="$bundle_contents"/Resources
bundle_fw="$bundle_contents"/Frameworks
bundle_lib="$bundle_res"/opt/local/lib
bundle_bin="$bundle_res"/opt/local/bin
bundle_data="$bundle_res"/opt/local/share
bundle_etc="$bundle_res"/opt/local/etc
bundle_share="$bundle_res"/opt/local/share

#export DYLD_LIBRARY_PATH="$bundle_lib"
export XDG_CONFIG_DIRS="$bundle_etc"/xdg
export XDG_DATA_DIRS="$bundle_data"
export GTK_DATA_PREFIX="$bundle_res"
export GTK_EXE_PREFIX="$bundle_res"
export GTK_PATH="$bundle_res"

export GTK2_RC_FILES="$bundle_etc/gtk-2.0/gtkrc"
export GTK_IM_MODULE_FILE="$bundle_etc/gtk-2.0/gtk.immodules"
export GDK_PIXBUF_MODULE_FILE="$bundle_etc/gtk-2.0/gdk-pixbuf.loaders"
export PANGO_RC_FILE="$bundle_etc/pango/pangorc"
export FONTCONFIG_FILE="$bundle_etc/fonts/fonts.conf"

export LANG=en_US.UTF-8
export XLOCALEDIR="$bundle_share/X11/locale"

export PYTHONHOME="$bundle_bin/Python.framework.2.7"

WRAPPER=

if [ x"$1" == "x--debug" ]; then
    if command -v lldb 2>/dev/null; then
        WRAPPER="gdb --args"
        WRAPPER="script $HOME/FontForge-Debug-Output.txt lldb --debug --source /Applications/FontForge.app/Contents/MacOS/debug-script -- "
        $WRAPPER
        # $WRAPPER $bundle_bin/fontforge "$2" "$3" "$4" "$5" "$6"
        exit
    else
        echo "You must installed the XCode Command Lines Tools to run FontForge"
        echo "in debug mode. For 10.7 they are available in the App Store. For"
        echo "10.6, install XCode from the App Store then install them by running"
        echo "XCode and looking in Preferences."
        exit
    fi
fi

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f FontForge.app
$scriptdir/fcprogress.pl >|/tmp/zz 2>&1

export PATH="$PATH:$bundle_bin"
echo $WRAPPER $bundle_bin/fontforge -new "$@" > /tmp/oo
$WRAPPER $bundle_bin/fontforge -new "$@"
