# Rock-specific shell functions
# 
# The bundle support requires the base/scripts package to be installed
function rock-bundle-sel() {
    if ! test -f $AUTOPROJ_CURRENT_ROOT/.bundle_env.sh; then
        echo "something's wrong: the $AUTOPROJ_CURRENT_ROOT/.env_bundle.sh does not exist."
        echo "have you successfully installed autoproj the base/scripts package ?"
        return 1
    fi

    name=$1
    bundle_path=$(rock-bundle-find $name)
    if test "x$?" != "x0"; then
        echo "cannot find bundle $name, run bundle-info to get the list of available bundles"
        return 1
    elif test -z "$name" || test -d "$name"; then
        bundle_path=$(echo $bundle_path | tail -n1)
        export ROCK_BUNDLE=$bundle_path
        rock-bundle-info
        return 0
    else
        export ROCK_BUNDLE=$name
        rock-bundle-info
        return 0
    fi
}
function rock-bundle-default() {
    if ! test -f $AUTOPROJ_CURRENT_ROOT/.bundle_env.sh; then
        echo "something's wrong: the $AUTOPROJ_CURRENT_ROOT/.env_bundle.sh does not exist."
        echo "have you successfully installed autoproj the base/scripts package ?"
        return 1
    fi

    if test -z "$1"; then
        echo "# File generated by the bundle-default command" > $AUTOPROJ_CURRENT_ROOT/.bundle_env.sh
        echo "# Do not change ! Your changes would be overriden !" >> $AUTOPROJ_CURRENT_ROOT/.bundle_env.sh
        echo "unset ROCK_BUNDLE" >> $AUTOPROJ_CURRENT_ROOT/.bundle_env.sh
    elif rock-bundle-sel $1; then
        echo "# File generated by the bundle-default command" > $AUTOPROJ_CURRENT_ROOT/.bundle_env.sh
        echo "# Do not change ! Your changes would be overriden !" >> $AUTOPROJ_CURRENT_ROOT/.bundle_env.sh
        echo "export ROCK_BUNDLE=$ROCK_BUNDLE" >> $AUTOPROJ_CURRENT_ROOT/.bundle_env.sh
        return 0
    fi
}
function rock-bundle-unsel() {
    unset ROCK_BUNDLE
}

function rock-log-level() {
    if test "x$#" != "x1" || test "$1" = "--help" || test "$1" = "-h"; then
        echo "usage: $0 <log-level>"
        echo "Allows setting of logging level for Orocos and Rock in parallel."
        echo ""
        echo "Available general logging levels:"
        echo "    DEBUG, INFO, WARN, ERROR, FATAL"
        echo "Applied to Orocos only"
        echo "    REALTIME, CRITICAL"
        echo ""
        echo "Current setting of environmental variables:"
        echo "    BASE_LOG_LEVEL=$BASE_LOG_LEVEL"
        echo "    ORO_LOGLEVEL=$ORO_LOGLEVEL"
        return
    fi

    if test "$1" = "REALTIME"; then
        export ORO_LOGLEVEL=7
        return
    elif test "$1" = "CRITICAL"; then
        export ORO_LOGLEVEL=2
        return
    fi

    if test "$1" = "DEBUG"; then
        export ORO_LOGLEVEL=6
    elif test "$1" = "INFO"; then
        export ORO_LOGLEVEL=5
    elif test "$1" = "WARN" || test "$1" = "WARNING"; then
        export ORO_LOGLEVEL=4
    elif test "$1" = "ERROR"; then
        export ORO_LOGLEVEL=3
    elif test "$1" = "FATAL"; then
        export ORO_LOGLEVEL=1
    else
        echo "Unknown logging level: $1"
        return
    fi

    export BASE_LOG_LEVEL=$1
}

alias bundle-sel=rock-bundle-sel
alias bundle-unsel=rock-bundle-unsel
alias bundle-default=rock-bundle-default
alias bundle-find=rock-bundle-find
alias bundle-info=rock-bundle-info
