#!/bin/bash
set -eux
TUSKAR_CONF=/etc/tuskar/tuskar.conf
TUSKAR_ROLE_DIRECTORY=${TUSKAR_ROLE_DIRECTORY:-}

function load_roles() {
    TUSKAR_LOAD_ROLE_BASE="tuskar-load-role --config-file $TUSKAR_CONF "
    roles=( $(echo "$1") )
    dir="${2}" ;
    suffix="${3}"
    extra_data="${4}"
    for ROLE in ${roles[@]}; do
        lowercase=`echo $ROLE | tr '[:upper:]' '[:lower:]'`
        if [ -f $dir/$lowercase$suffix ]; then
            TUSKAR_LOAD_ROLE="$TUSKAR_LOAD_ROLE_BASE -n $ROLE
                            -f $dir/$lowercase$suffix $extra_data"
            $TUSKAR_LOAD_ROLE            # only need to load this once
            # only need to load this once, so clear variable
            extra_data=
        fi
    done
}

if [ -z "$TUSKAR_ROLE_DIRECTORY" ]; then
  if [ -d /usr/share/openstack-tripleo-heat-templates/ ]; then
      # Fedora/RHEL RPMs store the templates here
      TUSKAR_ROLE_DIRECTORY='/usr/share/openstack-tripleo-heat-templates/'
  else
      # Default to the source installed version
      TUSKAR_ROLE_DIRECTORY='/opt/stack/tripleo-heat-templates'
  fi
fi

if os-is-bootstrap-host ; then
    su -s /bin/bash -c "tuskar-dbsync --config-file $TUSKAR_CONF" tuskar

    # pop trailing / from role directory if it is there
    TUSKAR_ROLE_DIRECTORY=`echo $TUSKAR_ROLE_DIRECTORY | sed 's/\/$//'`

    # Load the master seed and puppet or non-puppet roles, accordingly
    PUPPET_DIR="$TUSKAR_ROLE_DIRECTORY/puppet"
    OVERCLOUD_PLAN="$TUSKAR_ROLE_DIRECTORY/overcloud-without-mergepy.yaml"
    TUSKAR_LOAD_SEED="tuskar-load-seed --config-file $TUSKAR_CONF
                         --master-seed $OVERCLOUD_PLAN "
    if [ -d $PUPPET_DIR ]; then
        TUSKAR_RESOURCE_REGISTRY=${TUSKAR_RESOURCE_REGISTRY:-"$TUSKAR_ROLE_DIRECTORY/overcloud-resource-registry-puppet.yaml"}
        TUSKAR_LOAD_SEED="$TUSKAR_LOAD_SEED --resource-registry $TUSKAR_RESOURCE_REGISTRY"
        $TUSKAR_LOAD_SEED

        # Collect any role extra-data
        ROLE_EXTRA_DATA=""
        for puppet_extra_dir in hieradata manifests; do
            if [ -d "$PUPPET_DIR/$puppet_extra_dir" ]; then
                for i in `ls -1 "$PUPPET_DIR/$puppet_extra_dir"` ; do
                    ROLE_EXTRA_DATA="$ROLE_EXTRA_DATA --extra-data
                                     $PUPPET_DIR/$puppet_extra_dir/$i "
                done
            fi
        done

        # extraconfig/tasks includes package update script/template
        # also grab validation scripts
        for i in `find "$TUSKAR_ROLE_DIRECTORY/extraconfig/tasks" \
                       "$TUSKAR_ROLE_DIRECTORY/validation-scripts/" -type f`; do
            ROLE_EXTRA_DATA="$ROLE_EXTRA_DATA --extra-data $i "
        done

        ROLES=( "Compute" "Controller" "Swift-Storage" "Cinder-Storage" "Ceph-Storage" )
        # we are dropping the -puppet.yaml extension upstream
        if ls $PUPPET_DIR/*-puppet.yaml &>/dev/null; then
            load_roles "$(echo ${ROLES[@]})" $PUPPET_DIR "-puppet.yaml" "$ROLE_EXTRA_DATA"
        else
            load_roles "$(echo ${ROLES[@]})" $PUPPET_DIR ".yaml" "$ROLE_EXTRA_DATA"
        fi
    else
        TUSKAR_RESOURCE_REGISTRY=${TUSKAR_RESOURCE_REGISTRY:-"$TUSKAR_ROLE_DIRECTORY/overcloud-resource-registry.yaml"}
        TUSKAR_LOAD_SEED="$TUSKAR_LOAD_SEED --resource-registry $TUSKAR_RESOURCE_REGISTRY"
        $TUSKAR_LOAD_SEED

        # Load the non-puppet roles if there:
        ROLES=( "Compute" "Controller" "Swift-Storage" "Cinder-Storage" "Ceph-Storage" )
        load_roles "$(echo ${ROLES[@]})" $TUSKAR_ROLE_DIRECTORY ".yaml"
    fi
fi
