#!/bin/bash
# wrap-juju - execs an interactive shell with juju wrapped
# Copyright 2012 Canonical Ltd. All Rights Reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if [ "$SHELL" != "/bin/bash" ] ; then
    echo "WARNING: wrap-juju only works with bash. SHELL=$SHELL" >&2
fi

# For running in source tree
juju_wrapper_home=`dirname $0`
juju_wrapper_home=`readlink -f $juju_wrapper_home`
juju_wrapper=$juju_wrapper_home/../bin/jitsu
if ! [ -x $juju_wrapper ] ; then
    juju_wrapper=`which jitsu`
    if [ -z "$juju_wrapper" ] ; then
        echo "No jitsu found in $juju_wrapper_home or $PATH" >&2
        exit 1
    fi
fi

echo Aliasing juju to $juju_wrapper...

cleanup() {
    [ -z "$rcfile" ] || rm -f $rcfile
}
trap cleanup EXIT TERM QUIT

rcfile=`mktemp /tmp/wrap-juju.XXXXX`
cat > $rcfile <<EOF
[ -f /etc/bash.bashrc ] && . /etc/bash.bashrc
[ -f ~/.bashrc ] && . ~/.bashrc
alias juju=$juju_wrapper
PS1="(juju-jitsu) \$PS1"
EOF
/bin/bash --rcfile $rcfile -i
