# This file must be used with "source /opt/toolchain-v5/activate" *from bash*
# You can't run it directly!

env_error="You already have an active virtual environment!"

# zsh does not recognize the option -t of the command type
# therefore we use the alternative whence -w
if [[ "$ZSH_NAME" == "zsh" ]]; then
    # check for active virtual environments
    if [ "$( whence -w deactivate )" != "deactivate: none" ]; then
        echo $env_error
        return 0;
    fi
# any other shell
else
    # check for active virtual environments
    if [ "$( type -t deactivate )" != "" ]; then
        echo $env_error
        return 0
    fi
fi

# check that we aren't root
if [[ "$USER" == "root" ]]; then
    echo "You shouldn't use the toolchain as root!"
    return 0
fi

# save original environment
export ORIG_PATH=$PATH
export ORIG_PS1=$PS1
export ORIG_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
export ORIG_CXXFLAGS=$CXXFLAGS
export ORIG_CFLAGS=$CFLAGS

# activate new environment
export PATH=/opt/toolchain-v5:/opt/toolchain-v5/bin:$PATH
export PS1="(toolchain-v5) $PS1"
export LD_LIBRARY_PATH=/opt/toolchain-v5/lib:/opt/toolchain-v5/lib64
export CXXFLAGS=-isystem\ /opt/toolchain-v5/include\ $CXXFLAGS
export CFLAGS=-isystem\ /opt/toolchain-v5/include\ $CFLAGS

# disable root
function su () {
    echo "You don't want to use root functions while using the toolchain!"
    return 1
}
function sudo () {
    echo "You don't want to use root functions while using the toolchain!"
    return 1
}

# create deactivation function
function deactivate() {
    export PATH=$ORIG_PATH
    export PS1=$ORIG_PS1
    export LD_LIBRARY_PATH=$ORIG_LD_LIBRARY_PATH
    export CXXFLAGS=$ORIG_CXXFLAGS
    export CFLAGS=$ORIG_CFLAGS
    unset ORIG_PATH ORIG_PS1 ORIG_LD_LIBRARY_PATH ORIG_CXXFLAGS ORIG_CFLAGS
    unset -f su sudo deactivate
}
