#!/bin/bash
# 
# @author Alexander Dreyer
# @date 2007-10-19
#
# This file is a wrapper which enables the use of the PolyBoRi profile for
# IPython without the need of installing the profile and setting the PolyBoRi
# directory.
#
# @par Copyright:
#   (c) 2007 by
#   Dep. of Mathematics, Kaiserslautern University of Technology and @n
#   Fraunhofer Institute for Industrial Mathematics (ITWM)
#   D-67663 Kaiserslautern, Germany
#
# @version \$Id$
#
# $Log$
# Revision 1.3  2009/03/26 23:40:29  dreyer
# Fix: ipbori is executed in current path
#
# Revision 1.2  2007/10/22 08:19:15  dreyer
# ADD: evaluation of IPYTHONPREFIX, -ipythonprefix
#
# Revision 1.1  2007/10/19 12:48:48  dreyer
# ADD: ipython functionality and wrapper
#


if [ "$1" = "-ipythonprefix" ]; then
    IPYTHONPREFIX=$2
    shift
    shift
fi

OLDPWD=${PWD}
# First determine the directory of this script 
THIS="$0"

# Follow all symbolic links to get the real file name and directory
while [ -L ${THIS} ]; do
    THIS_DIR=`dirname "${THIS}"`
    # emulating readlink, which is not available everywhere
    ls_l=`LC_ALL=C ls -l "${THIS}" 2> /dev/null`
    THIS="${ls_l/* -> }"
    cd "${THIS_DIR}"
done
THIS_DIR=`dirname "${THIS}"`


# Assuming parent directory is PolyBoRi directory
export PBDIR=`cd "${THIS_DIR}/.."; pwd`

# Get program name
export IPBNAME=`basename $THIS`

# Get directory of ipython profile (assuming it is in same directory)
IPBDIR="${PBDIR}/ipbori"

cd ${OLDPWD}

# Calling ipython with PolyBoRi profile
#
# Note: directly calling the profile as rcfile saves us the trouble of
# installing the profile, but custom settings like IPYTHONDIR/ipythonrc are
# still available. 
# (For the latter, ipythonrc-polybori needs to import ipythonrc.)
#${IPYTHONPREFIX}ipython -rcfile ${IPBDIR}/ipythonrc-polybori  $@

export PBORI_SITE="pyroot/"

# Note: ../../lib/python2.7/site-packages is replaced on installation with relative path
# to the PolyBoRi python site
export IPBORI_INIT_CMDS="import sys; sys.path.insert(0, '$PBDIR/../../lib/python2.7/site-packages');\
from polybori.frontend import *; polybori_start(globals())"

IPYTHON=${IPYTHONPREFIX}ipython

# RCfiles are supported up to ipython 0.11
if [[ `$IPYTHON --c="import IPython; \
    ([eval(elt) for elt in IPython.__version__.split('.')[:2] ]< [0, 11]) "\
    -quick` =~ "True" ]]
then
    ${IPYTHON} -rcfile ${IPBDIR}/ipythonrc-polybori $@
else
    ${IPYTHON} --c="print'';$IPBORI_INIT_CMDS" --i
fi
