#!/bin/bash

set -eux

export INSTACK_ROOT=${INSTACK_ROOT:-"/usr/share"}
export ELEMENTS_PATH=${ELEMENTS_PATH:-"\
/usr/share/tripleo-image-elements:\
/usr/share/diskimage-builder/elements:\
/usr/share/instack-undercloud"}
export JSONFILE=${JSONFILE:-/usr/share/instack-undercloud/json-files/fedora-20-undercloud-packages.json}
source "$INSTACK_ROOT"/instack-undercloud/instack-sourcerc

sudo yum -y install git python-pip ccache

source /etc/profile.d/ccache.sh

mkdir -p $PIP_DOWNLOAD_CACHE

git config --global user.email "instack@example.com"
git config --global user.name "instack"

function cherry_pick {
    local PROJ_NAME=$1
    local REFSPEC=$2

    pushd $PROJ_NAME
    git fetch https://review.openstack.org/openstack/$PROJ_NAME "$REFSPEC"
    git cherry-pick FETCH_HEAD
    popd
}

function do_tripleo_source_installs {
    if [ ! -d $INSTACK_ROOT/tripleo-image-elements/elements ]; then
        git clone https://git.openstack.org/openstack/tripleo-image-elements

        pushd tripleo-image-elements

        if [ "$LKG" = "1" ]; then
            git fetch origin $DIB_REPOREF_tripleo_image_elements
            git reset --hard FETCH_HEAD
        fi

        # This patch is RDO specific, as such there is no upstream review for it.
        # Change default swift ports
        git fetch https://github.com/redhat-openstack/tripleo-image-elements
        git cherry-pick 705ebd4fb9ea2061303598368fa1f913cb408b0e

        popd

    fi

    if [ ! -d $INSTACK_ROOT/diskimage-builder ]; then
        git clone https://git.openstack.org/openstack/diskimage-builder

        pushd diskimage-builder

        if [ "$LKG" = "1" ]; then
            git fetch origin $DIB_REPOREF_diskimage_builder
            git reset --hard FETCH_HEAD
        fi

        popd

        if [ "$LKG" = "1" ]; then
            echo
        fi

        # Move busybox binary-dep to ramdisk element
        # https://review.openstack.org/#/c/130034/
        cherry_pick diskimage-builder refs/changes/34/130034/1
        # Unset trap before dracut ramdisk build script exits
        # https://review.openstack.org/#/c/130632/
        cherry_pick diskimage-builder refs/changes/32/130632/1
        # Install lsb_release from package
        # https://review.openstack.org/#/c/133452/
        cherry_pick diskimage-builder refs/changes/52/133452/1

    fi

    # tar is required if we want to run diskimage-builder
    sudo yum install -y tar

    if [ ! -d "$INSTACK_ROOT/dib-utils" ]; then
        git clone https://git.openstack.org/openstack/dib-utils
    fi


    if [ ! -d $INSTACK_ROOT/instack-undercloud ]; then
        git clone https://github.com/agroup/instack-undercloud
    fi

    if [ ! -d $INSTACK_ROOT/instack ]; then
        git clone https://github.com/agroup/instack
    fi

    if [ ! -d $INSTACK_ROOT/os-cloud-config ]; then
        git clone https://git.openstack.org/openstack/os-cloud-config

        pushd os-cloud-config

        if [ "$LKG" = "1" ]; then
            git fetch origin $DIB_REPOREF_os_cloud_config
            git reset --hard FETCH_HEAD
        fi

        popd

    fi


    if [ ! -d $INSTACK_ROOT/tripleo-incubator ]; then
        git clone https://git.openstack.org/openstack/tripleo-incubator

        pushd tripleo-incubator

        if [ "$LKG" = "1" ]; then
            git fetch origin $DIB_REPOREF_tripleo_incubator
            git reset --hard FETCH_HEAD
        fi

        popd

    fi

    if [ ! -d $INSTACK_ROOT/tripleo-heat-templates ]; then
        git clone https://git.openstack.org/openstack/tripleo-heat-templates

        pushd tripleo-heat-templates

        if [ "$LKG" = "1" ]; then
            git fetch origin $DIB_REPOREF_tripleo_heat_templates
            git reset --hard FETCH_HEAD
        fi

        popd
    fi

    # Create a useful symlink for openstack-tripleo-heat-templates
    # This simplifies the source vs package installs a bit so we can just use one path
    ln -f -s $INSTACK_ROOT/tripleo-heat-templates $INSTACK_ROOT/openstack-tripleo-heat-templates

    export DIB_REPOLOCATION_tripleo_heat_templates=$INSTACK_ROOT/tripleo-heat-templates
    export DIB_INSTALLTYPE_tripleo_heat_templates=source
    export TUSKAR_ROLE_DIRECTORY=/opt/stack/tripleo-heat-templates

    # pip installs netaddr package as one of dependencies when
    # installing instack-related projects bellow. Then later
    # python-netaddr rpm is being installed and it fails on RHEL
    # because it conflicts with pip's netaddr. Make sure python-netaddr
    # rpm is installed first.
    sudo yum -y install python-netaddr

    pushd $INSTACK_ROOT/dib-utils
    sudo pip install -U .
    popd

    pushd $INSTACK_ROOT/diskimage-builder
    sudo pip install -U .
    popd

    pushd $INSTACK_ROOT/instack
    sudo pip install -e .
    popd

    # Needed to compile os-cloud-config
    sudo yum install -y libffi-devel gcc python-devel openssl-devel libxml2-devel libxslt-devel
    pushd $INSTACK_ROOT/os-cloud-config
    sudo pip install -e .
    popd

    # Must uninstall cmd2 and netaddr otherwise, the package install will fail later on
    sudo pip uninstall -y cmd2
    sudo pip uninstall -y netaddr

}

if [ ! -d $INSTACK_ROOT/tuskar-ui ]; then
    sudo git clone https://git.openstack.org/openstack/tuskar-ui $INSTACK_ROOT/tuskar-ui
fi
export DIB_REPOLOCATION_tuskar_ui=$INSTACK_ROOT/tuskar-ui
export DIB_INSTALLTYPE_tuskar_ui=source
export DIB_INSTALLTYPE_horizon=source

if [ ! -d $INSTACK_ROOT/tuskar ]; then
    sudo git clone https://git.openstack.org/openstack/tuskar $INSTACK_ROOT/tuskar
fi
export DIB_REPOLOCATION_tuskar=$INSTACK_ROOT/tuskar
export DIB_INSTALLTYPE_tuskar=source

if [ "$INSTACK_TRIPLEO_SOURCE" = "1" ]; then
    do_tripleo_source_installs
fi

# needed for semanage
sudo yum install -y policycoreutils-python

# ensure latest policy
sudo yum install -y selinux-policy

if [ "$RUN_INSTACK" -eq 0 ]; then
    echo "Exiting without running instack."
    exit 0
fi

if [ "$PACKAGES" = "1" -a "$DELOREAN" = "0" ]; then
    if ! rpm -q rdo-release; then
        sudo yum -y install $RDO_RELEASE_RPM
    fi
fi

instack-install-undercloud

# Reset ownership of cache directory
# This is needed in order for instack-build-images to make changes to the cache
# $USER is not always set in a live cd building enviroment, so use || true
sudo -E chown -R $USER: $HOME/.cache/image-create || true

# Must erase NetworkManager, otherwise ifup called on the provisioning
# interface tries to use nmcli and that fails for some yet undetermined reason,
# so just delete NetworkManager for now.
# It also causes issues with the undercloud dropping its IP, so we need to
# disable it and restart networking too.
if systemctl status NetworkManager; then
    sudo systemctl stop NetworkManager
    sudo systemctl restart network
fi
sudo yum -y erase NetworkManager

# Must reinstall cmd2 as there is a package conflict with the pip install, so
# we have to reinstall it. Otherwise, "neturon net-show" during
# instack-deploy-overcloud fails.
sudo pip install -U cmd2

echo "$COMPLETION_MESSAGE"
