#!/bin/bash

# Copyright: 2023 Virtuozzo International GmbH

# 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.



inherit default os envinfo;
include output ;
include "$balancer_lib" ;
inherit extendperm;

DEFAULT_ACTION="Usage";
DESCRIPTION="Balancer module";
VERSION="1";

nb_path="/etc/nginx/neighbors/";
nb_file="common";

function doUsage() {
    showUsageMessage
}

function describeSetNeighbors(){
    echo "Add neighbors balancers"
}

function doSetNeighbors(){
    if [ ! -d "$nb_path" ]; then
        mkdir -p "$nb_path" ;
    fi
    echo "$@" > "${nb_path}/$nb_file"

    writeJSONResponseOut "result=>0" "message=>Ok";
}


[ "x${COMPUTE_TYPE}" == "xcartridge" ] && {
          [ -f "${CARTRIDGE_HOME}/jelastic/scripts/balancer_manager.sh" ] && source "${CARTRIDGE_HOME}/jelastic/scripts/balancer_manager.sh"; 
} ;

#-------------------------#
function doAddCommonHost(){
    while [[ "$1" != "" ]]; do
        case "$1" in
            -h|--host )
                    shift;
                    host="$1";
                ;;
        esac
        shift
    done
    [ -z "$host" ] && { writeJSONResponseErr "result=>4099" "message=>Missing param" ; return 99; }
    laddCommonHostConfig="addCommonHostConfig" #added for backwards compatibility with Openshift cartridge templates
    isFunction "_add_common_host" && laddCommonHostConfig="_add_common_host" #must be removed after the cartridge support is finished
    "$laddCommonHostConfig" "$host" && writeJSONResponseOut "result=>0" "message=>Configuration applied!";
    return 0;
}

function doRemoveCommonHost(){
    while [[ "$1" != "" ]]; do
        case "$1" in
            -h|--host )
                    shift;
                    host="$1";
                ;;
        esac
        shift
    done
    [ -z "$host" ] && { writeJSONResponseErr "result=>4099" "message=>Missing param" ; return 99; }
    lremoveCommonHostConfig="removeCommonHostConfig" #added for backwards compatibility with Openshift cartridge templates
    isFunction "_remove_common_host" && lremoveCommonHostConfig="_remove_common_host" #must be removed after the cartridge support is finished
    "$lremoveCommonHostConfig" "$host" && writeJSONResponseOut "result=>0" "message=>Configuration applied!";
    return 0;
}

function doRebuildCommon(){
    lrebuildCommon="rebuildCommon" #added for backwards compatibility with Openshift cartridge templates
    isFunction "_rebuild_common" && lrebuildCommon="_rebuild_common" #must be removed after the cartridge support is finished
    "$lrebuildCommon" ;
    [ "$?" -eq 0 ] && {
        msg=$(doAction "firewall" "fwreconfigure");
        writeJSONResponseOut "result=>0" "message=>Configuration applied!"; return 0;
    }|| { writeJSONResponseErr "result=>4099" "message=>Missing param" ; return 99; }
}

function doAddCluster(){
    while [[ "$1" != "" ]]; do
        case "$1" in
            -h|--host ) 
                    shift;
                    host="$1";
                ;;
            -g|--group ) 
                    shift;
                    group="$1";
                ;;
        esac
        shift
    done
    [ -z "$host" -o -z "$group" ] && { writeJSONResponseErr "result=>4099" "message=>Missing param" ; return 99; } 
    addHostToGroup "$host" "$group"
    writeJSONResponseOut "result=>0" "message=>Configuration applied!"; 
    return 0;
}

function doBuildCluster(){
    while [[ "$1" != "" ]]; do
        case "$1" in
            -g|--group ) 
                    shift;
                    group="$1";
                ;;
        esac
        shift
    done
    [ -z "$group" ] && { writeJSONResponseErr "result=>4099" "message=>Missing param" ; return 99; } 
    buildCluster "$group" ;
    [ "$?" -eq 0 ] && {
        writeJSONResponseOut "result=>0" "message=>Configuration applied!"; return 0;  
    } || { writeJSONResponseErr "result=>4090" "message=>Configuration not applied!" ; return 1; }
}

function doUnbuildCluster(){
    while [[ "$1" != "" ]]; do
        case "$1" in
            -g|--group ) 
                    shift;
                    group="$1";
                ;;
        esac
        shift
    done
    [ -z "$group" ] && { writeJSONResponseErr "result=>4099" "message=>Missing param" ; return 99; } 
    unbuildCluster "$group" ;
    [ "$?" -eq 0 ] && {
        writeJSONResponseOut "result=>0" "message=>Configuration applied!"; return 0; 
    } || { writeJSONResponseErr "result=>4099" "message=>Missing param" ; return 99; }
}

function doClear(){
    isFunction "clearHosts" && clearHosts ; 
    writeJSONResponseOut "result=>0" "message=>Configuration applied!"; 
    return 0; 
}

function doReload(){
    isFunction "reloadConfigs" && reloadConfigs ;
    writeJSONResponseOut "result=>0" "message=>Configuration applied!"; 
    return 0; 
}

function doRebuildConfig(){
    WEIGHT_CHANGED_FLAG=1;
    rebuildCommonTrafD && echo "Configuration has been applied!";
}

function doRefreshWeight(){
    WEIGHT_CHANGED_FLAG=1;
    weightChanged;
    writeJSONResponseOut "result=>0" "message=>Ok";
}

function doUpdatePortMapping(){
    rebuildTCPPortMappings ;
}

function doSetVariables(){
    setConfigVariables "$@";
    writeJSONResponseOut "result=>0" "message=>Ok";
}

#--host HOST
function doRemoveHost(){
    removeHostHTD "$@";
}

#--source HOST --destination HOST
function doReplaceHost(){
    swapHostsHTD "$@";
}

function doAddNewHost(){
    addNodeHTD "$@";
}
function doRemoveSticky(){
    removeStickyHTD ;
}

function doAddSticky(){
    addNginxSticky ; 
}

function doEnableProxySSL(){
    swapNginxProxyProtocol "https" ;
}

function doDisableProxySSL(){
    swapNginxProxyProtocol "http" ;
}

function doSetProxyTimeout(){
    setNginxProxyTimeout "$@";
}

function postRebuildCommonCallback(){
    ExtendPerm ;
}

function postAddClusterCallback(){
    ExtendPerm ;
}

function postBuildClusterCallback(){
    ExtendPerm ;
}

function postUnbuildClusterCallback(){
    ExtendPerm ;
}

function postClearCallback(){
    ExtendPerm ;
}

function postReloadCallback(){
    ExtendPerm ;
}

#--------- describe ------------#
function describeAddCommonHost(){
    echo "Add host to common group";
}

function describeAddCommonHostParameters(){
    echo "-h <host>";
}

function describeAddCommonHostOptions(){
    echo "-h: host name or IP";
}

function describeRemoveCommonHost(){
    echo "Remove host from common group";
}

function describeRemoveCommonHostParameters(){
    echo "-h <host>";
}

function describeRemoveCommonHostOptions(){
    echo "-h: host name or IP";
}

function describeRebuildCommon(){
    echo "Rebuild with common config";
}

function describeAddCluster(){
    echo "Add hosts to upstream group_name"
}

function describeAddClusterParameters(){
    echo "-h <host> -g <group name>";
}

function describeAddClusterOptions(){
    echo "-h: host name or IP";
    echo "-g: group name";
}

function describeBuildCluster(){
    echo "Add group 'group_name' to cluster"
}

function describeBuildClusterParameters(){
    echo "-g <group name>";
}

function describeBuildClusterOptions(){
    echo "-g: group name";
}

function describeUnbuildCluster(){
    echo "Remove group 'group_name' from cluster";
}

function describeUnbuildClusterParameters(){
    echo "-g <group name>";
}

function describeUnbuildClusterOptions(){
    echo "-g: group name";
}

function describeReload(){
    echo "Load clean configs"
}

function describeClear(){
    echo "Clear upsteams hosts"
}

function describeRebuildConfig(){
    echo "Rebuild nginx configuration";
}

function describeRefreshWeight(){
    echo "Update weight value in nginx configuration";
}

function describeRemoveHost(){
    echo "Remove host from nginx configuration";
}

function describeReplaceHost(){
    echo "Replace host in nginx configuration";
}

function describeAddNewHost(){
    echo "Add new host to nginx configuration";
}

function describeRemoveHostParameters(){
    echo "-h|--host <host>";
}

function describeReplaceHostParameters(){
    echo "-s|--source <host> -d|--destination <host>";    
}

function describeAddNewHostParameters(){
    echo "-h|--host <host>";
}

function describeRemoveHostOptions(){
    echo "-h|--host: host name or IP";
}

function describeReplaceHostOptions(){
    echo "-s|--source: host name or IP";
    echo "-d|--destination: host name or IPt";    
}

function describeAddNewHostOptions(){
    echo "-h|--host: host name or IP";
}
