#!/bin/bash
#
# Shared functions for shell scripts
#

# Make sure custom grep options don't get in the way
unset GREP_OPTIONS


function lp_project_to_repo {
    typeset proj="$1"

    if [[ $proj == python-*client* ]]; then
        echo $proj
    elif [[ $proj == glance-store ]]; then
        echo glance_store
    elif [[ $proj == django-openstack-auth ]]; then
        echo django_openstack_auth
    else
        # Some of the repository names don't match the launchpad names, e.g.
        # python-stevedore and python-cliff.
        echo $proj | sed -e 's|^python-||'
    fi
}


function _cleanup_tmp {
    rm -rf $MYTMPDIR
    return 0
}


function setup_temp_space {
    MYTMPDIR=`mktemp -d _tmp-${1}-XXX`
    mkdir -p "$MYTMPDIR"
    trap _cleanup_tmp EXIT
    cd "$MYTMPDIR"
    # NOTE(dhellmann): On some platforms mktemp returns a short name
    # instead of a full path, so expand the full path by looking at
    # where we ended up after the cd operation.
    MYTMPDIR="$(pwd)"
}



function get_last_tag {
    # Print the most recent tag for a ref. If no ref is specified, the
    # currently checked out branch is examined.
    local ref="$1"
    if ! git describe --abbrev=0 --first-parent ${ref} >/dev/null 2>&1; then
        echo ""
    else
        git describe --abbrev=0 --first-parent ${ref}
    fi
}


function update_gitreview {
    typeset branch="$1"

    echo "Updating .gitreview"
    git checkout $branch
    # Remove a trailing newline, if present, to ensure consistent
    # formatting when we add the defaultbranch line next.
    typeset grcontents="$(echo -n "$(cat .gitreview | grep -v defaultbranch)")
defaultbranch=$branch"
    echo "$grcontents" > .gitreview
    git add .gitreview
    git commit -m "Update .gitreview for $branch"
    git show
    local shortbranch=$(basename $branch)
    git review -t "create-${shortbranch}" --yes
}


function update_upper_constraints {
    typeset branch="$1"
    typeset uc_server='git.openstack.org'
    typeset uc_path='cgit/openstack/requirements/plain/upper-constraints.txt'
    typeset uc_url="https://${uc_server}/${uc_path}?h=${branch}"

    echo "Updating tox.ini for upper-constraints"
    git checkout $branch
    if [[ ! -f tox.ini ]]; then
        echo "No tox.ini file, skipping constraints"
        return 0
    fi
    sed -i~ -e "s,{\(env:UPPER_CONSTRAINTS_FILE\)[^ ]*},{\1:$uc_url}," tox.ini
    if ! git diff --exit-code >/dev/null 2>&1 ; then
        git add tox.ini
        git commit -m "Update UPPER_CONSTRAINTS_FILE for $branch"
        git show
        local shortbranch=$(basename $branch)
        git review -t "create-${shortbranch}" --yes
    fi
}


function clone_repo {
    typeset repo="$1"
    typeset branch="$2"
    typeset zc

    set -e

    if [[ ! -z "$branch" ]]; then
        branch="--branch $branch"
    fi

    # Find zuul-cloner. We look first in the spot where it is
    # installed on the CI node, but that is not likely to be in
    # $PATH. It won't be in that location on other systems where the
    # tools are run by hand, so fall back to looking in $PATH
    # otherwise.
    if [[ -f /usr/zuul-env/bin/zuul-cloner ]]; then
        zc=/usr/zuul-env/bin/zuul-cloner
    else
        zc=zuul-cloner
    fi

    # Clone the repository using the given branch, if possible.
    $zc $branch git://git.openstack.org $repo

    # Configure git-review so we can propose patches to the repo if
    # needed. Some of the release scripts are still run by hand to
    # recover from failure, in which case we don't want to set the git
    # review user. So try to make git review work by itself first, and
    # only if that fails update the git configuration.
    #
    # NOTE(dhellmann): configure_git_review is in ../common.sh, which
    # must be sourced by the script calling this function.
    if ! (cd $repo && git review -s); then
        (cd $repo && configure_git_review && git review -s)
    fi

    # Make sure we have all of the existing tags so we can refer to
    # them for building the release notes.
    (cd $repo && git fetch -v --tags)

    return 0
}