#!/usr/bin/env bash

# (c) Copyright 2014,2015 Hewlett-Packard Development Company, L.P.
#
# 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.

# Install the Freezer Web UI in Horizon

# add the following to localrc:
#   enable_service freezer-web_ui


# Save trace setting
XTRACE=$(set +o | grep xtrace)
set +o xtrace


# Functions
# ---------

function is_freezer_web_ui_enabled {
    [[ ,${ENABLED_SERVICES} =~ ,"freezer-web-ui" ]] && return 0
}


# executed during: clean
function cleanup_freezer_web_ui {
    :
}

# executed during: stack install
function install_freezer_web_ui {
    if is_service_enabled horizon; then
        git_clone $FREEZER_WEB_UI_REPO $FREEZER_WEB_UI_DIR $FREEZER_WEB_UI_BRANCH
        setup_develop $FREEZER_WEB_UI_DIR
    else
        echo "Horizon dashboard disabled. Freezer Web UI will not be installed"
    fi
}

# executed during: stack post-config
function configure_freezer_web_ui {
    :
}


# executed during: stack extra
function init_freezer_web_ui {
    if is_service_enabled horizon; then

        local dashboard_enabled_5050_freezer_py="$HORIZON_DIR/openstack_dashboard/enabled/_5050_freezer.py"

        sudo cp $FREEZER_WEB_UI_FILES/_5050_freezer.py.template $dashboard_enabled_5050_freezer_py

    fi
}


# executed during: stack extra
function start_freezer_web_ui {
    restart_apache_server
}


# executed during: stop
function stop_freezer_web_ui {
    :
}

# Setup python-freezerclient
function install_freezerclient {

    git_clone $FREEZERCLIENT_REPO $FREEZERCLIENT_DIR $FREEZERCLIENT_BRANCH
    setup_develop $FREEZERCLIENT_DIR
}

# Restore xtrace
$XTRACE
