#!/bin/bash
#
# Copyright (c) 2008-2010 by nexB, Inc. http://www.nexb.com/ - All rights reserved.
# This software is NOT RELEASED YET and NOT LICENSED unless agreed with nexB.
#

# Fix for ticket #728:
# always run from the folder where dpl is. 
# Note that this is far from perfect (it will not survive a rename of the executable, for example)
COMMAND=$0
cd ${COMMAND:0:${#COMMAND}-4}

# Gather core dumps by defaults, to investigate #3944
ulimit unlimited

WORKBENCH_PYTHON=../../python2.5/Resources/Python.app/Contents/MacOS/Python

if [ -e "$WORKBENCH_PYTHON" ]; then
  # If in the workbench, use the Python shipped with it
  PYTHON=$WORKBENCH_PYTHON
  # Make sure we don't load *anything else*. This is probably overkill
  export PATH=
  export DYLD_LIBRARY_PATH=../../python2.5
fi

PYTHON_VERSION=2.5

VERSION_COMMAND="import sys; sys.exit(sys.stdin.readline().find('$PYTHON_VERSION') == -1)"

if [ "$PYTHON" == "" ]; then
  for PYTHON in `which python`               \
                `which python2.5`            \
                '/usr/bin/python'            \
                '/usr/local/bin/python'      \
                '/usr/bin/python2.5'         \
                '/usr/local/bin/python2.5'
  do
    if [ -x $PYTHON ]; then
      $PYTHON -V 2>&1 | $PYTHON -c "$VERSION_COMMAND" &> /dev/null
      if [ $? == 0 ]; then
        break
      fi
    fi
    PYTHON=
  done
fi

if [ "$PYTHON" == "" ]; then
  echo
  echo "The DejaCode Toolkit requires Python $PYTHON_VERSION, which was not found at any usual location."
  echo 
  echo "Please install Python by downloading version Python 2.5.4 from http://www.python.org"
  echo 
  echo "Alternatively, you can specify a Python installation location by setting" 
  echo "the environment variable PYTHON to the location of your already installed" 
  echo "Python version $PYTHON_VERSION executable and restart DejaCode."
  echo 
  exit -1
fi

unset PYTHONPATH
$PYTHON -c "import sys, os; sys.path.remove(''); sys.path.insert(0, os.path.abspath('.')); from dejacode import interpreter; interpreter.main()" "$@"
