#!/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"

name=`basename "$0"`
tmp="$0"
tmp=`dirname "$tmp"`
tmp=`dirname "$tmp"`
bundle=`dirname "$tmp"`
bundle=$PKGBASEPATH
scriptdir=$PKGBASEPATH/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 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 LANG=en_US.UTF-8

WRAPPER=

if [ x"$1" == "x--debug" -o x"$1" == "x--debug-clearenv" ]; then

    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="$PKGBASEPATH/Contents/MacOS/Even.app/Contents/Resources/lib/"
        export PATH="$PATH:$bundle_bin"
        export PYTHON="$PKGBASEPATH/Contents/Frameworks/Python.framework/Versions/Current/bin/python"

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

        WRAPPER="gdb --args"
        WRAPPER="script $HOME/FontForge-Debug-Output.txt lldb --debug --source $PKGBASEPATH/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

#
# only change these here to the local bundle ones so the scripts
# can run.
export PYTHONPATH="$bundle_lib/python2.7:$PKGBASEPATH/Contents/MacOS/Even.app/Contents/Resources/lib/"
# This line allows python to find the compiled libraries so for example the gdraw
# module can use _libraries['libgdraw.so.5'] = CDLL('libgdraw.so.5')
export LD_LIBRARY_PATH="$bundle_lib"


#/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f FontForge.app

# This ensures that File Open and File Save (etc) dialogs open in Home
# when FontForge is launched via normal OS X methods (double-clicking
# on the application or a SFD file, spotlight, etc.). If this script is
# invoked via command line, assume the user knows what they are doing,
# and don't change the current directory.
if [ "$( ps -o comm= $PPID)" = "/sbin/launchd" ] ; then
    cd ~/ || true
fi

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

if [ $# -eq 0 ] ; then
    ( exec $WRAPPER $bundle_bin/fontforge -new  )
else
    ( exec $WRAPPER $bundle_bin/fontforge "$@"  )
fi
