#!/bin/sh

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

# $0 is possibly relative path to this script.
SCRIPT_PATH="$( cd "$( dirname "$0" )" && pwd )"
PKGBASEPATH="$( cd "${SCRIPT_PATH}/../.." && pwd )"

echo "PKGBASE: $PKGBASEPATH"

APPLICATIONS_FONTFORGE="/Applications/FontForge.app"

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_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"


WRAPPER=


    if [ x"$1" == "x--debug-clearenv" ]; then
       # the user has instructed us to flush their environment
       echo "flushing some python related environment variables for this script..."
       unset PYTHONHOME
       unset LD_LIBRARY_PATH
       unset PYTHONPATH
       alias python=/usr/bin/python
       export PATH="/usr/bin:$PATH"
       echo "Also selected the default python executable."
       type -all python
    fi

    if command -v lldb 2>/dev/null; then

        export PYTHONPATH="/Applications/FontForge.app/Contents/MacOS/Even.app/Contents/Resources/lib/"
        export PATH="$PATH:$bundle_bin"
        export PYTHON="$bundle_bin/Python.framework.*/bin/python"
        export PYTHONHOME="$bundle_bin/Python.framework.2.7"

        echo "have set up python stuff... PYTHONHOME: $PYTHONHOME "
        echo "have set up python stuff... PYTHON:     $PYTHON "

        PID=$(ps aux|grep '[0-9] /Applications/FontForge.app/Contents/Resources/opt/local/bin/fontforge' | sed -E 's/[^0-9]+([0-9]+).*/\1/g')
	echo "fontforge pid $PID "

        lldb --debug -p $PID 
        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

