#!/bin/bash
OPEN_PROJECT_NAME="hug"

if [ "$PROJECT_NAME" = "$OPEN_PROJECT_NAME" ]; then
    return
fi

if [ ! -f ".env" ]; then
    return
fi

export PROJECT_NAME=$OPEN_PROJECT_NAME
export PROJECT_DIR="$PWD"
export PROJECT_VERSION="2.6.0"

if [ ! -d "venv" ]; then
     if ! hash pyvenv 2>/dev/null; then
        function pyvenv()
        {
            if hash python3.7 2>/dev/null; then
                python3.7 -m venv $@
            elif hash pyvenv-3.6 2>/dev/null; then
                pyvenv-3.6 $@
            elif hash pyvenv-3.5 2>/dev/null; then
                pyvenv-3.5 $@
            elif hash pyvenv-3.4 2>/dev/null; then
                pyvenv-3.4 $@
            elif hash pyvenv-3.3 2>/dev/null; then
                pyvenv-3.3 $@
            elif hash pyvenv-3.2 2>/dev/null; then
                pyvenv-3.2 $@
            else
                python3 -m venv $@
            fi
        }
    fi

    echo "Making venv for $PROJECT_NAME"
    pyvenv venv
    . venv/bin/activate
    pip install -r requirements/development.txt
    python setup.py install
fi

. venv/bin/activate

# Let's make sure this is a hubflow enabled repo
yes | git hf init >/dev/null 2>/dev/null

# Quick directory switching
alias root="cd $PROJECT_DIR"
alias project="root; cd $PROJECT_NAME"
alias tests="root; cd tests"
alias examples="root; cd examples"
alias requirements="root; cd requirements"
alias run_tests="_test"


function open {
    (root
     $CODE_EDITOR hug/*.py setup.py tests/*.py examples/*.py examples/*/*.py README.md tox.ini .gitignore CHANGELOG.md setup.cfg .editorconfig .env .coveragerc .travis.yml requirements/*.txt)
}


function clean {
    (root
     isort hug/*.py setup.py tests/*.py
     black -l 100 hug)
}


function check {
    (root
     frosted hug/*.py)
}


function _test {
    (root
     tox)
}


function coverage {
    (root
     $BROWSER htmlcov/index.html)
}


function load {
    (root
     python setup.py install)
}


function unload {
    (root
     pip uninstall hug)
}


function install {
    (root
     sudo python setup.py install)
}


function update {
    (root
     pip install -r requirements/development.txt -U)
}


function distribute {
    (root
     pip install pypandoc
     python -c "import pypandoc; pypandoc.convert('README.md', 'rst')" || exit 1
     python setup.py sdist upload)
}


function version()
{
    echo $PROJECT_VERSION
}


function new_version()
{
    (root
     if [ -z "$1" ]; then
         echo "You must supply a new version to replace the old version with"
         return
     fi

    sed -i "s/$PROJECT_VERSION/$1/" .env setup.py hug/_version.py)
    export PROJECT_VERSION=$1
}


function new_version_patch()
{
    (root
     bumpversion --allow-dirty patch)
}


function new_version_minor()
{
    (root
     bumpversion --allow-dirty minor)
}


function new_version_major()
{
    (root
     bumpversion --allow-dirty major)
}


function leave {
    export PROJECT_NAME=""
    export PROJECT_DIR=""

    unalias root
    unalias project
    unalias tests
    unalias examples
    unalias requirements
    unalias test

    unset -f _start
    unset -f _end


    unset -f open
    unset -f clean
    unset -f _test
    unset -f coverage
    unset -f load
    unset -f unload
    unset -f install
    unset -f update
    unset -f distribute
    unset -f version
    unset -f new_version

    unset -f leave

    deactivate
}
