#!/bin/sh
# Zope debug prompt for IPython
set -e
# Assume instance home is cwd
INSTANCE_HOME=`pwd`
# unless we specified a path on the command line
[ $# -ne 0 ] && INSTANCE_HOME="$@"

# Get Zope paths from the zope configuration file
ZOPE_CONFIG=$INSTANCE_HOME/etc/zope.conf
ZOPE_HOME=`grep '%define ZOPE' $ZOPE_CONFIG | cut -d ' ' -f 3`
SOFTWARE_HOME=$ZOPE_HOME/lib/python
PYTHONPATH="$SOFTWARE_HOME:$INSTANCE_HOME/lib/python"

export PYTHONPATH ZOPE_CONFIG SOFTWARE_HOME INSTANCE_HOME

# Set up Zope within IPython
# Imports
STARTUP="import __builtin__, sys"
# Zope wants to mark 'quit' as safe for scripting, but IPython lacks it
# Create a dummy
STARTUP="$STARTUP; setattr(__builtin__, 'quit', None)"
# Import Zope and store the database root in the app variable
STARTUP="$STARTUP; import Zope2"
STARTUP="$STARTUP; app=Zope2.app()"
# Make sure stdin is correctly pointing to the default, otherwise readline is
# borked. At least WingDBG will redirect it, possibly others
STARTUP="$STARTUP; sys.stdin = sys.__stdin__"
# Remove our imports
STARTUP="$STARTUP; del __builtin__, sys" 
# And print a banner
MESSAGE="Starting debugger (the name \"app\" is bound to the top-level Zope object)"
STARTUP="$STARTUP; __IP.write('$MESSAGE')"

/usr/bin/python2.4-ipython -c "$STARTUP"
