#!/bin/sh
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

set -e

HERE=$(pwd)
VENVDIR=$HERE/venv
SOURCESDIR=$HERE/sources
MANUALSREPO=$SOURCESDIR/openstack-manuals
AUTOHELP="python $HERE/autohelp.py"
EXTRACT_SWIFT="python $HERE/extract_swift_flags.py"
GITBASE=git://git.openstack.org/openstack
PROJECTS="ceilometer cinder glance heat keystone neutron nova sahara swift trove"
BRANCH=master
FAST=0

usage() {
    echo "Wrapper for autohelp.py"
    echo "Usage:"
    echo "  $(basename $0) [ OPTIONS ] update|docbook|setup [ project1 ... ]"
    echo
    echo "Subcommands:"
    echo "  create:    Create an initial flagmapping file"
    echo "  update:    Update the flagmapping files"
    echo "  docbook:   Generate the options tables"
    echo "  setup:     Install the environment only"
    echo
    echo "Options:"
    echo "  -b BRANCH: Work on this branch (defaults to master)"
    echo "  -c:        Recreate the virtual environment"
    echo "  -f:        Work offline: Do not change environment or sources"
    echo "  -e PATH:   Create the virtualenv in PATH"
}

setup_venv() {
    if [ ! -e $VENVDIR/bin/activate ]; then
        virtualenv $VENVDIR
    fi
    . $VENVDIR/bin/activate
}

get_project() {
    project=$1

    if [ ! -e $SOURCESDIR/$project ]; then
        git clone $GITBASE/$project $SOURCESDIR/$project
    else
        if [ $project != openstack-manuals ]; then
            (cd $SOURCESDIR/$project && git pull)
        fi
    fi
}

setup_tools() {
    get_project oslo-incubator
    get_project openstack-manuals

    (cd $SOURCESDIR/oslo-incubator && python setup.py install)
    pip install "GitPython>=0.3.2.RC1"

    # autohelp.py requires this
    pip install oslo.i18n

    # For some reason the ceilometer installation fails without these 2
    # packages pre-installed
    pip install setuptools pbr

    # Some projects don't have explicit dependencies on some less used packages.
    # Without these packages installed some modules can't be imported and
    # configuration options are not discovered. We install these packages to
    # make sure that we get the options.

    # Ceilometer
    pip install swift

    # Cinder
    # hp3parclient is needed for icehouse, but not for juno
    pip install hp3parclient hplefthandclient

    # Neutron
    pip install ryu

    # This is a workaround to avoid pulling in new configuration options brought
    # by python-keystoneclient >= 0.8. This version also deprecates some options
    # still expected by the projects. The release of version 0.8 happened too
    # late in the icehouse release cycle to let the doc team handle the changes
    # properly in the documentation.
    if echo $BRANCH | grep -q stable/; then
        pip install "python-keystoneclient==0.7"
    fi
}

while getopts :b:e:cf opt; do
    case $opt in
    b)
        BRANCH=$OPTARG
        ;;
    c)
        rm -rf $VENVDIR
        ;;
    e)
        VENVDIR=$OPTARG
        ;;
    f)
        FAST=1
        ;;
    \?)
        usage
        exit 1
        ;;
    esac
done
shift $(($OPTIND - 1))

if [ $# -lt 1 ]; then
    usage
    exit 1
fi

ACTION=$1
shift

case $ACTION in
    create|update|docbook|setup) ;;
    *)
        usage
        exit 1
        ;;
esac

if [ ! -e autohelp.py ]; then
    echo "Execute this script in the autogenerate_config_docs directory."
    exit 1
fi

[ $# != 0 ] && PROJECTS="$*"

setup_venv
if [ "$FAST" -eq 0 ] ; then
    setup_tools

    for project in $PROJECTS; do
        get_project $project

        (
            cd $SOURCESDIR/$project
            find $project -name "*.pyc" -delete
            GIT_CMD="git show-ref --verify --quiet refs/heads/$BRANCH"
            if $GIT_CMD; then
                git checkout $BRANCH
            else
                git checkout -b $BRANCH remotes/origin/$BRANCH
            fi
            python setup.py install
        )
    done
fi

for project in $PROJECTS; do
    if [ "$ACTION" = "setup" ]; then
        break
    fi

    if [ -d $MANUALSREPO/tools/autogenerate-config-flagmappings ]; then
        cd $MANUALSREPO/tools/autogenerate-config-flagmappings
    else
        # for havana
        $MANUALSREPO/tools/autogenerate-config-docs
    fi

    case $ACTION in
    create)
        [ "$project" = "swift" ] && continue
        file=$MANUALSREPO/tools/autogenerate-config-flagmappings/$project.flagmappings
        if [ -e $file ]; then
            echo "$project.flagmappings already exists, ignoring."
            continue
        fi
        $AUTOHELP create $project -i $SOURCESDIR/$project
        ;;
    update)
        [ "$project" = "swift" ] && continue
        $AUTOHELP update $project -i $SOURCESDIR/$project
        mv $project.flagmappings.new $project.flagmappings
        ;;
    docbook)
        if [ "$project" = "swift" ]; then
            $EXTRACT_SWIFT docbook -m $MANUALSREPO -s $SOURCESDIR/swift
        else
            $AUTOHELP docbook $project -i $SOURCESDIR/$project
        fi
        ;;
    esac
done
