#!/bin/bash -i
# By Thomas Vander Stichele (thomas at apestaart dot org)
# Stolen from his puppet project, MOAP: https://thomas.apestaart.org/moap/trac
# Licensed under GPL
#
# set up environment to use and develop uninstalled
#
# set up PYTHONPATH, PGM_PLUGIN_PATH, LD_LIBRARY_PATH, GST_PLUGIN_PATH
#
# prefer uninstalled versions, but also put installed ones on the path
#
# symlink this script in a directory in your path (for example $HOME/bin)
# to a name that reflects the version of your checkout

# this script is run -i so that PS1 doesn't get cleared


self=$0
absolute=$self
if test -L $absolute
then
  # find what the symlink points to
  absolute=`stat $absolute -c %N -t | sed 's/^.* -> .//g' | sed 's/.$//g'`
fi
MISCDIR=`dirname $absolute`
BASEDIR=`cd $MISCDIR/.. && pwd`

PROJECT='pigment'

options=

# set up a bunch of paths
#export PATH=$BASEDIR/bin${PATH:+:$PATH}
export PYTHONPATH=$BASEDIR/libs/pypgm/pgm/.libs:$BASEDIR/libs/pypgm:$BASEDIR/libs${PYTHONPATH:+:$PYTHONPATH}
export LD_LIBRARY_PATH=$BASEDIR/pgm/.libs${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
export DYLD_LIBRARY_PATH=$BASEDIR/pgm/.libs${DYLD_LIBRARY_PATH:+:$DYLD_LIBRARY_PATH}
export GST_PLUGIN_PATH=$BASEDIR/pgm/.libs${GST_PLUGIN_PATH:+:$GST_PLUGIN_PATH}

export PGM_PLUGIN_PATH=$BASEDIR/plugins

# set up bash completion if we have it
tmp=
if echo $SHELL | grep bash > /dev/null
then
  if test -d $BASEDIR/etc/bash_completion.d
  then
    # we need to create our own temporary bashrc replacement from the user's
    # and our completion stuff
    tmp=`mktemp -t bashrc.XXXXXXXX`
    if test -e $HOME/.bashrc
    then
      cat $HOME/.bashrc > $tmp
    fi
    cat $BASEDIR/etc/bash_completion.d/$PROJECT >> $tmp
    SHELL_OPTIONS="--init-file $tmp"
    options="(with bash completion)"
  fi
fi

# if we got a command, run it, else start a shell
if test ! -z "$1";
then
  $@
  exit $?
fi

# set up prompt to help us remember we're in a subshell, cd to
# the base directory and start $SHELL
#cd $BASEDIR
shell=$SHELL
if test "x$SHELL" == "x/bin/bash"
then
  # debian/ubuntu resets our PS1.  bastards.
  if test -e /etc/debian_version
  then
    SHELL_OPTIONS="$SHELL_OPTIONS --noprofile"
  fi
fi
echo Entering interactive $PROJECT shell $SHELL $options
PS1="[$PROJECT] $PS1" $SHELL $SHELL_OPTIONS

if test ! -z "$tmp"
then
  rm $tmp
fi
echo Thank you for using $PROJECT.
