#!/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
        sudo -H -E pip install -e git+${FREEZER_WEB_UI_REPO}@${FREEZER_WEB_UI_BRANCH}#egg=freezer_web_ui-origin_${FREEZER_WEB_UI_BRANCH}
    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_50_freezer_py="$HORIZON_DIR/openstack_dashboard/enabled/_50_freezer.py"

        sudo cp $FREEZER_WEB_UI_FILES/_50_freezer.py.template $dashboard_enabled_50_freezer_py

        sudo sed -e "
            s|%FREEZER_WEB_UI_DIR%|$FREEZER_WEB_UI_DIR|g
        " -i $dashboard_enabled_50_freezer_py

    fi
}


# executed during: stack extra
function start_freezer_web_ui {
    restart_apache_server
}


# executed during: stop
function stop_freezer_web_ui {
    :
}


function install_freezer_client {
    sudo -H -E pip install -e git+${FREEZER_CLIENT_REPO}@${FREEZER_CLIENT_BRANCH}#egg=freezer-origin_${FREEZER_CLIENT_BRANCH}
}


# Restore xtrace
$XTRACE