#!/bin/bash

function enable_tox {
    if [ ! -d .tox/venv ]; then
        if ! tox -e venv --notest; then
            echo "ERROR: Failed to create tox environment 'venv'"
            exit 1
        fi
    fi
    source .tox/venv/bin/activate
    if [ -z "$VIRTUAL_ENV" ]; then
        echo "ERROR: The virtual environment was not activated."
        echo "Check .tox/venv/bin/activate"
        exit 1
    fi
}

function normalize_team {
   echo "$@" | sed -e 's/ /-/g'
}

function get_team_dir {
    local workdir="$1"
    local team="$2"

    echo "$workdir/$team" | sed -e 's/ /-/g'
}

function log_output {
    local workdir="$1"
    local slug="$2"

    LOGFILE="$workdir/${slug}.$(date --iso-8601=seconds).log"
    echo "Logging to $LOGFILE"
    # Set fd 1 and 2 to write the log file
    exec 1> >( tee "${LOGFILE}" ) 2>&1
    date
    echo $0 $@
}

function list_stable_branches {
    local repodir="$1"

    (cd $repodir &&
            git branch --list -a |
                grep origin/stable |
                sed -e 's|remotes/origin/||g')
}
